ソースを参照

AuthLib fix to support multi session roles

euromark 12 年 前
コミット
422e62b548

+ 29 - 17
Lib/Auth.php

@@ -12,7 +12,7 @@ App::uses('CakeSession', 'Model/Datasource');
  * Convenience wrapper to access Auth data and check on rights/roles.
  * Expects the Role session infos to be either
  * 	`Auth.User.role_id` (single) or
- * 	`Auth.User.Role` (multi)
+ * 	`Auth.User.Role` (multi - flat array of roles, or array role data)
  * and can be adjusted via defined().
  * Same for Right.
  *
@@ -25,28 +25,40 @@ App::uses('CakeSession', 'Model/Datasource');
 class Auth {
 
 	/**
-	 * get the user id of the current session or return empty/null
+	 * Get the user id of the current session.
 	 *
-	 * @return mixed $userId
+	 * This can be used anywhere to check if a user is logged in.
+	 *
+	 * @return mixed User id if existent, null otherwise.
 	 */
 	public static function id() {
 		return CakeSession::read('Auth.User.id');
 	}
 
 	/**
-	 * get the role(s) of the current session or return empty/null
+	 * Get the role(s) of the current session.
+	 *
+	 * It will return the single role for single role setup, and a flat
+	 * list of roles for multi role setup.
 	 *
-	 * @return mixed $roles
+	 * @return mixed String or array of roles or null if inexistent
 	 */
 	public static function roles() {
-		return CakeSession::read('Auth.User.' . USER_ROLE_KEY);
+		$roles = CakeSession::read('Auth.User.' . USER_ROLE_KEY);
+		if (!is_array($roles)) {
+			return $roles;
+		}
+		if (isset($roles[0]['id'])) {
+			$roles = Hash::extract($roles, '{n}.id');
+		}
+		return $roles;
 	}
 
 	/**
-	 * get the user data of the current session or return empty/null
+	 * Get the user data of the current session.
 	 *
 	 * @param string $key (dot syntax)
-	 * @return mixed $data
+	 * @return mixed Data
 	 */
 	public static function user($key = null) {
 		if ($key) {
@@ -56,11 +68,11 @@ class Auth {
 	}
 
 	/**
-	 * check if the current session has this right
+	 * Check if the current session has this right.
 	 *
 	 * @param mixed $role
-	 * @param mixed $existingRolesToCheckAgainst
-	 * @return bool $success
+	 * @param mixed $providedRights
+	 * @return bool Success
 	 */
 	public static function hasRight($ownRight, $providedRights = null) {
 		if ($providedRights !== null) {
@@ -76,11 +88,11 @@ class Auth {
 	}
 
 	/**
-	 * check if the current session has this role
+	 * Check if the current session has this role.
 	 *
 	 * @param mixed $role
-	 * @param mixed $existingRolesToCheckAgainst
-	 * @return bool $success
+	 * @param mixed $providedRoles
+	 * @return bool Success
 	 */
 	public static function hasRole($ownRole, $providedRoles = null) {
 		if ($providedRoles !== null) {
@@ -101,12 +113,12 @@ class Auth {
 	}
 
 	/**
-	 * check if the current session has oen of these roles
+	 * Check if the current session has oen of these roles.
 	 *
 	 * @param mixed $roles
 	 * @param bool $oneRoleIsEnough (if all $roles have to match instead of just one)
-	 * @param mixed $existingRolesToCheckAgainst
-	 * @return bool $success
+	 * @param mixed $providedRoles
+	 * @return bool Success
 	 */
 	public static function hasRoles($ownRoles, $oneRoleIsEnough = true, $providedRoles = null) {
 		if ($providedRoles !== null) {

+ 0 - 1
Lib/RandomLib.php

@@ -282,7 +282,6 @@ class RandomLib {
 	 * @param string a type of pool, or a string of characters to use as the pool
 	 * @param integer length of string to return
 	 * @return string
-	 * @uses UTF8::split
 	 */
 	public static function random($type = 'alnum', $length = 8) {
 		switch ($type) {

+ 1 - 1
Model/Behavior/SluggedBehavior.php

@@ -28,7 +28,7 @@ App::uses('ModelBehavior', 'Model');
 /**
  * SluggedBehavior class
  *
- * @uses ModelBehavior
+ *
  * @version 2.x
  * @modified Mark Scherer
  */

+ 40 - 1
Test/Case/Lib/AuthTest.php

@@ -7,7 +7,7 @@ App::uses('MyCakeTestCase', 'Tools.TestSuite');
  */
 class AuthTest extends MyCakeTestCase {
 
-	public $fixtures = array('core.session');
+	public $fixtures = array('core.cake_session');
 
 	public function setUp() {
 		parent::setUp();
@@ -25,6 +25,11 @@ class AuthTest extends MyCakeTestCase {
 		CakeSession::delete('Auth');
 	}
 
+	/**
+	 * AuthTest::testId()
+	 *
+	 * @return void
+	 */
 	public function testId() {
 		$id = Auth::id();
 		$this->assertNull($id);
@@ -34,6 +39,11 @@ class AuthTest extends MyCakeTestCase {
 		$this->assertEquals(1, $id);
 	}
 
+	/**
+	 * AuthTest::testHasRole()
+	 *
+	 * @return void
+	 */
 	public function testHasRole() {
 		$res = Auth::hasRole(1, array(2, 3, 6));
 		$this->assertFalse($res);
@@ -51,6 +61,35 @@ class AuthTest extends MyCakeTestCase {
 		$this->assertFalse($res);
 	}
 
+	/**
+	 * AuthTest::testHasRoleWithSession()
+	 *
+	 * @return void
+	 */
+	public function testHasRoleWithSession() {
+		if (!defined('USER_ROLE_KEY')) {
+			define('USER_ROLE_KEY', 'Role');
+		}
+		CakeSession::write('Auth.User.id', 1);
+		$roles = array(
+			array('id' => '1', 'name' => 'User', 'alias' => 'user'),
+			array('id' => '2', 'name' => 'Moderator', 'alias' => 'moderator'),
+			array('id' => '3', 'name' => 'Admin', 'alias' => 'admin'),
+		);
+		CakeSession::write('Auth.User.' . USER_ROLE_KEY, $roles);
+
+		$res = Auth::hasRole(4);
+		$this->assertFalse($res);
+
+		$res = Auth::hasRole(3);
+		$this->assertTrue($res);
+	}
+
+	/**
+	 * AuthTest::testHasRoles()
+	 *
+	 * @return void
+	 */
 	public function testHasRoles() {
 		$res = Auth::hasRoles(array(1, 3), true, array(2, 3, 6));
 		$this->assertTrue($res);

+ 0 - 3
Test/Fixture/MessageFixture.php

@@ -24,9 +24,6 @@
 /**
  * MessageFixture class
  *
- * @uses CakeTestFixture
- * @package mi
- * @subpackage mi.tests.fixtures
  */
 class MessageFixture extends CakeTestFixture {