浏览代码

Fix tests and improve documentation.

euromark 12 年之前
父节点
当前提交
a2e9ce8291

+ 43 - 27
Lib/Auth.php

@@ -10,11 +10,19 @@ App::uses('CakeSession', 'Model/Datasource');
 
 /**
  * Convenience wrapper to access Auth data and check on rights/roles.
+ *
+ * It can be used anywhere in the application due to static access.
+ * So in the view we can use this shortcut to check if a user is logged in:
+ *
+ *   if (Auth::id()) {
+ *     // Display element
+ *   }
+ *
  * Expects the Role session infos to be either
- * 	`Auth.User.role_id` (single) or
- * 	`Auth.User.Role` (multi - flat array of roles, or array role data)
- * and can be adjusted via defined().
- * Same for Right.
+ * 	- `Auth.User.role_id` (single) or
+ * 	- `Auth.User.Role` (multi - flat array of roles, or array role data)
+ * and can be adjusted via constants and defined().
+ * Same goes for Right data.
  *
  * @author Mark Scherer
  * @license MIT
@@ -40,7 +48,7 @@ class Auth {
 	 * It will return the single role for single role setup, and a flat
 	 * list of roles for multi role setup.
 	 *
-	 * @return mixed String or array of roles or null if inexistent
+	 * @return mixed String or array of roles or null if inexistent.
 	 */
 	public static function roles() {
 		$roles = CakeSession::read('Auth.User.' . USER_ROLE_KEY);
@@ -56,7 +64,7 @@ class Auth {
 	/**
 	 * Get the user data of the current session.
 	 *
-	 * @param string $key (dot syntax)
+	 * @param string $key Key in dot syntax.
 	 * @return mixed Data
 	 */
 	public static function user($key = null) {
@@ -67,26 +75,6 @@ class Auth {
 	}
 
 	/**
-	 * Check if the current session has this right.
-	 *
-	 * @param mixed $role
-	 * @param mixed $providedRights
-	 * @return boolean Success
-	 */
-	public static function hasRight($ownRight, $providedRights = null) {
-		if ($providedRights !== null) {
-			$rights = $providedRights;
-		} else {
-			$rights = CakeSession::read('Auth.User.' . USER_RIGHT_KEY);
-		}
-		$rights = (array)$rights;
-		if (array_key_exists($ownRight, $rights) && !empty($rights[$ownRight])) {
-			return true;
-		}
-		return false;
-	}
-
-	/**
 	 * Check if the current session has this role.
 	 *
 	 * @param mixed $role
@@ -112,7 +100,10 @@ class Auth {
 	}
 
 	/**
-	 * Check if the current session has oen of these roles.
+	 * Check if the current session has one of these roles.
+	 *
+	 * You can either require one of the roles (default), or you can require all
+	 * roles to match.
 	 *
 	 * @param mixed $roles
 	 * @param boolean $oneRoleIsEnough (if all $roles have to match instead of just one)
@@ -149,4 +140,29 @@ class Auth {
 		return false;
 	}
 
+	/**
+	 * Check if the current session has this right.
+	 *
+	 * Rights can be an additional element to give permissions, e.g.
+	 * the right to send messages/emails, to friend request other users,...
+	 * This can be set via Right model and stored in the Auth array upon login
+	 * the same way the roles are.
+	 *
+	 * @param mixed $role
+	 * @param mixed $providedRights
+	 * @return boolean Success
+	 */
+	public static function hasRight($ownRight, $providedRights = null) {
+		if ($providedRights !== null) {
+			$rights = $providedRights;
+		} else {
+			$rights = CakeSession::read('Auth.User.' . USER_RIGHT_KEY);
+		}
+		$rights = (array)$rights;
+		if (array_key_exists($ownRight, $rights) && !empty($rights[$ownRight])) {
+			return true;
+		}
+		return false;
+	}
+
 }

+ 15 - 13
Lib/Utility/TextLib.php

@@ -8,9 +8,7 @@ App::uses('String', 'Utility');
  */
 class TextLib extends String {
 
-	protected $text, $length, $char, $letter, $space, $word, $rWord, $sen, $rSen, $para,
-
-		$rPara, $beautified;
+	public $text, $length, $char, $letter, $space, $word, $rWord, $sen, $rSen, $para, $rPara, $beautified;
 
 	public function __construct($text = null) {
 		$this->text = $text;
@@ -92,13 +90,19 @@ class TextLib extends String {
 	 * @return string The abbreviated string, if longer than $length.
 	 */
 	public static function abbreviate($text, $length = 20, $ending = '...') {
-		return (mb_strlen($text) > $length)
-			? rtrim(mb_substr($text, 0, round(($length - 3) / 2))) . $ending . ltrim(mb_substr($text, (($length - 3) / 2) * -1))
-			: $text;
+		if (mb_strlen($text) <= $length) {
+			return $text;
+		}
+		return rtrim(mb_substr($text, 0, round(($length - 3) / 2))) . $ending . ltrim(mb_substr($text, (($length - 3) / 2) * -1));
 	}
 
-/* other */
-
+	/**
+	 * TextLib::convertToOrd()
+	 *
+	 * @param string $str
+	 * @param string $separator
+	 * @return string
+	 */
 	public function convertToOrd($str = null, $separator = '-') {
 		/*
 		if (!class_exists('UnicodeLib')) {
@@ -272,7 +276,7 @@ class TextLib extends String {
 		if (trim($value) === '') {
 			return '';
 		}
-		preg_match('/^\s*+(?:\S++\s*+) {1,' . $words . '}/u', $value, $matches);
+		preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $value, $matches);
 
 		$end = $options['ellipsis'];
 		if (mb_strlen($value) === mb_strlen($matches[0])) {
@@ -289,7 +293,7 @@ class TextLib extends String {
 	 * @param string
 	 * @return string
 	 */
-	public function ascii_to_entities($str) {
+	public function asciiToEntities($str) {
 		$count = 1;
 		$out = '';
 		$temp = array();
@@ -328,8 +332,6 @@ class TextLib extends String {
 		return $out;
 	}
 
-	// ------------------------------------------------------------------------
-
 	/**
 	 * Entities to ASCII
 	 *
@@ -339,7 +341,7 @@ class TextLib extends String {
 	 * @param boolean
 	 * @return string
 	 */
-	public function entities_to_ascii($str, $all = true) {
+	public function EntitiesToAscii($str, $all = true) {
 		if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
 			for ($i = 0, $s = count($matches['0']); $i < $s; $i++) {
 				$digits = $matches['1'][$i];

+ 3 - 0
Test/Case/Controller/Component/AutoLoginComponentTest.php

@@ -117,6 +117,7 @@ class AutoLoginTestController extends Controller {
 	 * @var array
 	 */
 	public $testHeaders = array();
+
 	/**
 	 * Fail method
 	 *
@@ -125,6 +126,7 @@ class AutoLoginTestController extends Controller {
 	public function fail() {
 		$this->failed = true;
 	}
+
 	/**
 	 * Redirect method
 	 *
@@ -136,6 +138,7 @@ class AutoLoginTestController extends Controller {
 	public function redirect($url, $status = null, $exit = true) {
 		return $status;
 	}
+
 	/**
 	 * Conveinence method for header()
 	 *

+ 1 - 0
Test/Case/View/Helper/QrCodeHelperTest.php

@@ -18,6 +18,7 @@ App::uses('MyCakeTestCase', 'Tools.TestSuite');
  *
  */
 class QrCodeHelperTest extends MyCakeTestCase {
+
 	/**
 	 * SetUp method
 	 *

+ 46 - 0
View/Helper/MyHelper.php

@@ -320,4 +320,50 @@ class MyHelper extends Helper {
 		return $routerUrl;
 	}
 
+	/**
+	 * Generate url for given asset file. Depending on options passed provides full url with domain name.
+	 * Also calls Helper::assetTimestamp() to add timestamp to local files.
+	 * Uses Configure::read('App.assetBaseUrl') for CDN setup.
+	 *
+	 * @param string|array Path string or url array
+	 * @param array $options Options array. Possible keys:
+	 *   `fullBase` Return full url with domain name
+	 *   `pathPrefix` Path prefix for relative URLs
+	 *   `ext` Asset extension to append
+	 *   `plugin` False value will prevent parsing path as a plugin
+	 * @return string Generated url
+	 */
+	public function assetUrl($path, $options = array()) {
+		if (!Configure::read('App.assetBaseUrl')) {
+			return parent::assetUrl($path, $options);
+		}
+		if (is_array($path)) {
+			return $this->url($path, !empty($options['fullBase']));
+		}
+		if (strpos($path, '://') !== false) {
+			return $path;
+		}
+		if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
+			list($plugin, $path) = $this->_View->pluginSplit($path, false);
+		}
+		if (!empty($options['pathPrefix']) && $path[0] !== '/') {
+			$path = $options['pathPrefix'] . $path;
+		}
+		if (
+			!empty($options['ext']) &&
+			strpos($path, '?') === false &&
+			substr($path, -strlen($options['ext'])) !== $options['ext']
+		) {
+			$path .= $options['ext'];
+		}
+		if (isset($plugin)) {
+			$path = Inflector::underscore($plugin) . '/' . $path;
+		}
+
+		$path = $this->_encodeUrl($this->assetTimestamp($this->webroot($path)));
+		$path = rtrim(Configure::read('App.assetBaseUrl'), '/') . '/' . ltrim($path, '/');
+
+		return $path;
+	}
+
 }