euromark 12 years ago
parent
commit
6fd76f78b4

+ 33 - 68
Controller/Component/AuthExtComponent.php

@@ -3,12 +3,6 @@
 if (!defined('USER_ROLE_KEY')) {
 	define('USER_ROLE_KEY', 'Role');
 }
-if (!defined('USER_INFO_KEY')) {
-	define('USER_INFO_KEY', 'Info');
-}
-if (!defined('USER_RIGHT_KEY')) {
-	define('USER_RIGHT_KEY', 'Right');
-}
 if (!defined('CLASS_USER')) {
 	define('CLASS_USER', 'User');
 }
@@ -87,9 +81,9 @@ class AuthExtComponent extends AuthComponent {
 
 		if (empty($user)) {
 			$user = $this->identify($this->Controller->request, $this->Controller->response);
-		} elseif (!is_array($user)) {
-			$user = $this->completeAuth($user);
 		}
+		$user = $this->completeAuth($user);
+
 		if (empty($user)) {
 			$this->loginError = __('invalidLoginCredentials');
 			return false;
@@ -152,23 +146,25 @@ class AuthExtComponent extends AuthComponent {
 
 			$this->Session->renew();
 			$this->Session->write(self::$sessionKey, $user);
-			$this->Session->write(self::$sessionKey, $this->completeAuth($user));
 		}
 		return $this->loggedIn();
 	}
 
 	/**
-	 * @return array $user or bool false on failure
+	 * Gather session data.
+	 *
+	 * @return array User
 	 * 2011-11-03 ms
 	 */
 	public function completeAuth($user) {
 		$Model = $this->getModel();
-		if (!is_array($user)) {
+		$userArray = $user;
+		if (!is_array($userArray)) {
 			$user = $Model->get($user);
 			if (!$user) {
-				return false;
+				return array();
 			}
-			$user = array_shift($user);
+			$userArray = array_shift($user);
 		}
 
 		if (isset($Model->hasAndBelongsToMany[$this->roleModel]['className'])) {
@@ -180,11 +176,9 @@ class AuthExtComponent extends AuthComponent {
 		}
 		if (empty($with) && $this->settings['parentModelAlias'] !== false) {
 			trigger_error('No relation from user to role found');
-			return false;
+			return $user;
 		}
 
-		$completeAuth = array($this->settings['userModel'] => $user);
-
 		# roles
 		if (!empty($with)) {
 			list($plugin, $withModel) = pluginSplit($with);
@@ -192,9 +186,9 @@ class AuthExtComponent extends AuthComponent {
 				$this->{$withModel} = ClassRegistry::init($with);
 			}
 			# only for multi
-			if ($this->settings['multi'] || !isset($completeAuth[$this->settings['userModel']]['role_id'])) {
+			if ($this->settings['multi'] || !isset($userArray['role_id'])) {
 				$parentModelAlias = $this->settings['parentModelAlias'];
-				$completeAuth[$this->settings['userModel']][$parentModelAlias] = array(); # default: no roles!
+				$userArray[$parentModelAlias] = array(); # default: no roles!
 				$roles = $this->{$withModel}->find('list', array('fields' => array($withModel.'.role_id'), 'conditions' => array($withModel.'.user_id' => $user['id'])));
 				if (!empty($roles)) {
 					//$primaryRole = $this->user($this->fieldKey);
@@ -204,52 +198,18 @@ class AuthExtComponent extends AuthComponent {
 					//$roles = set::extract('/'.$with.'['.$this->fieldKey.'!='.$primaryRole.']/'.$this->fieldKey, $roles);
 
 					// add the suplemental roles id under the Auth session key
-					$completeAuth[$this->settings['userModel']][$parentModelAlias] = $roles; // or USER_ROLE_KEY
+					$userArray[$parentModelAlias] = $roles;
 					//pr($completeAuth);
 				}
 			} else {
-				//$completeAuth[$this->settings['userModel']][$parentModelAlias][] = $completeAuth[$this->settings['userModel']]['role_id'];
-			}
-		}
-
-		# deprecated!
-		if (isset($Model->hasOne['UserInfo'])) {
-			$with = $Model->hasOne['UserInfo']['className'];
-			list($plugin, $withModel) = pluginSplit($with);
-			if (!isset($this->{$withModel})) {
-				$this->{$withModel} = ClassRegistry::init($with);
-			}
-			$infos = $this->{$withModel}->find('first', array('fields' => array(), 'conditions' => array($withModel.'.id' => $user['id'])));
-
-			$completeAuth[$this->settings['userModel']][USER_INFO_KEY] = array(); # default: no rights!
-			if (!empty($infos)) {
-				$completeAuth[$this->settings['userModel']][USER_INFO_KEY] = $infos[$with];
-				//pr($completeAuth);
-			}
-		}
-
-		# deprecated!
-		if (isset($Model->hasOne['UserRight'])) {
-			$with = $Model->hasOne['UserRight']['className'];
-			list($plugin, $withModel) = pluginSplit($with);
-			if (!isset($this->{$withModel})) {
-				$this->{$withModel} = ClassRegistry::init($with);
-			}
-			$rights = $this->{$withModel}->find('first', array('fields' => array(), 'conditions' => array($withModel.'.id' => $user['id'])));
-
-			$completeAuth[$this->settings['userModel']][USER_RIGHT_KEY] = array(); # default: no rights!
-			if (!empty($rights)) {
-				// add the suplemental roles id under the Auth session key
-				$completeAuth[$this->settings['userModel']][USER_RIGHT_KEY] = $rights[$with];
-				//pr($completeAuth);
+				//$userArray[$parentModelAlias][] = $userArray['role_id'];
 			}
 		}
 
 		if (method_exists($Model, 'completeAuth')) {
-			$completeAuth = $Model->completeAuth($completeAuth);
-			return $completeAuth[$this->settings['userModel']];
+			$userArray = $Model->completeAuth($userArray);
 		}
-		return $completeAuth[$this->settings['userModel']];
+		return $userArray;
 	}
 
 	/**
@@ -286,21 +246,35 @@ class AuthExtComponent extends AuthComponent {
 			return true;
 		}
 
+		if (!$this->_getUser()) {
+			return $this->_unauthenticated($controller);
+		}
+
 		if (empty($this->authorize) || $this->isAuthorized($this->user())) {
 			return true;
 		}
 
-		$this->_unauthorized($controller);
+		return $this->_unauthorized($controller);
+	}
+
+	/**
+	 * Returns the current User model
+	 *
+	 * @return object $User
+	 */
+	public function getModel() {
+		$model = $this->settings['userModel'];
+		return ClassRegistry::init($model);
 	}
 
 	/**
 	 * @deprecated
-	 * @return bool $success
+	 * @return bool Success
 	 */
 	public function verifyUser($id, $pwd) {
 		trigger_error('deprecated - use Authenticate class');
 		$options = array(
-			'conditions' => array('id'=>$id, 'password'=>$this->password($pwd)),
+			'conditions' => array('id' => $id, 'password' => $this->password($pwd)),
 		);
 		return $this->getModel()->find('first', $options);
 
@@ -309,13 +283,4 @@ class AuthExtComponent extends AuthComponent {
 		return $this->identify($this->request, $this->response);
 	}
 
-	/**
-	 * returns the current User model
-	 * @return object $User
-	 */
-	public function getModel() {
-		$model = $this->settings['userModel'];
-		return ClassRegistry::init($model);
-	}
-
 }

+ 2 - 1
Model/Behavior/JsonableBehavior.php

@@ -37,7 +37,7 @@ class JsonableBehavior extends ModelBehavior {
 	 * @var array
 	 */
 	public $_defaultSettings = array(
-		'fields' => array(), # empty => only works with array!!!
+		'fields' => array(), # empty => autodetect - only works with array!
 		'input' => 'array', # json, array, param, list (param/list only works with specific fields)
 		'output' => 'array', # json, array, param, list (param/list only works with specific fields)
 		'separator' => '|', # only for param or list
@@ -248,4 +248,5 @@ class JsonableBehavior extends ModelBehavior {
 		}
 		return false;
 	}
+
 }

+ 11 - 13
Test/Case/Model/Behavior/JsonableBehaviorTest.php

@@ -4,7 +4,6 @@ App::uses('JsonableBehavior', 'Tools.Model/Behavior');
 App::uses('AppModel', 'Model');
 App::uses('MyCakeTestCase', 'Tools.TestSuite');
 
-
 class JsonableBehaviorTest extends MyCakeTestCase {
 
 	public $fixtures = array(
@@ -34,7 +33,7 @@ class JsonableBehaviorTest extends MyCakeTestCase {
 		$res = $this->Comment->save($data);
 		$this->assertTrue((bool)$res);
 
-		$this->assertSame($res['JsonableComment']['details'], '{"x":"y"}');
+		$this->assertSame('{"x":"y"}', $res['JsonableComment']['details']);
 	}
 
 	public function testFieldsWithList() {
@@ -51,7 +50,7 @@ class JsonableBehaviorTest extends MyCakeTestCase {
 		$res = $this->Comment->save($data);
 		$this->assertTrue((bool)$res);
 
-		$this->assertSame($res['JsonableComment']['details'], '["z","y","x"]');
+		$this->assertSame('["z","y","x"]', $res['JsonableComment']['details']);
 
 		# with sort and unique
 		$data = array(
@@ -66,7 +65,7 @@ class JsonableBehaviorTest extends MyCakeTestCase {
 		$res = $this->Comment->save($data);
 		$this->assertTrue((bool)$res);
 
-		$this->assertSame($res['JsonableComment']['details'], '["x","y","z"]');
+		$this->assertSame('["x","y","z"]', $res['JsonableComment']['details']);
 	}
 
 	public function testFieldsWithParam() {
@@ -83,7 +82,7 @@ class JsonableBehaviorTest extends MyCakeTestCase {
 		$res = $this->Comment->save($data);
 		$this->assertTrue((bool)$res);
 
-		$this->assertSame($res['JsonableComment']['details'], '{"z":"vz","y":"yz","x":"xz"}');
+		$this->assertSame('{"z":"vz","y":"yz","x":"xz"}', $res['JsonableComment']['details']);
 	}
 
 
@@ -107,18 +106,18 @@ class JsonableBehaviorTest extends MyCakeTestCase {
 		$this->assertTrue((bool)$res);
 
 		$res = $this->Comment->find('first', array('conditions' => array('title' => 'param')));
-		$this->assertEquals($res['JsonableComment']['details'], array('x'=>'y'));
+		$this->assertEquals(array('x'=>'y'), $res['JsonableComment']['details']);
 
 		// param
 		$this->Comment->Behaviors->unload('Jsonable');
 
 		$res = $this->Comment->find('first', array('conditions' => array('title' => 'param')));
-		$this->assertEquals($res['JsonableComment']['details'], '{"x":"y"}');
+		$this->assertEquals('{"x":"y"}', $res['JsonableComment']['details']);
 
 		$this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'param', 'fields'=>array('details')));
 
 		$res = $this->Comment->find('first', array('conditions' => array('title' => 'param')));
-		$this->assertEquals($res['JsonableComment']['details'], 'x:y');
+		$this->assertEquals('x:y', $res['JsonableComment']['details']);
 
 		// list
 		$this->Comment->Behaviors->unload('Jsonable');
@@ -137,12 +136,12 @@ class JsonableBehaviorTest extends MyCakeTestCase {
 		$this->Comment->Behaviors->unload('Jsonable');
 
 		$res = $this->Comment->find('first', array('conditions' => array('title' => 'list')));
-		$this->assertEquals($res['JsonableComment']['details'], '["z","y","x"]');
+		$this->assertEquals('["z","y","x"]', $res['JsonableComment']['details']);
 
 		$this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'list', 'fields'=>array('details')));
 
 		$res = $this->Comment->find('first', array('conditions' => array('title' => 'list')));
-		$this->assertEquals($res['JsonableComment']['details'], 'z|y|x');
+		$this->assertEquals('z|y|x', $res['JsonableComment']['details']);
 
 		// custom separator
 		$this->Comment->Behaviors->unload('Jsonable');
@@ -150,12 +149,11 @@ class JsonableBehaviorTest extends MyCakeTestCase {
 
 		// find first
 		$res = $this->Comment->find('first', array('conditions' => array('title' => 'list')));
-		$this->assertEquals($res['JsonableComment']['details'], 'z, y, x');
+		$this->assertEquals('z, y, x', $res['JsonableComment']['details']);
 
 		// find all
 		$res = $this->Comment->find('all', array('order' => array('title' => 'ASC')));
-		$this->assertEquals($res[0]['JsonableComment']['details'], 'z, y, x');
+		$this->assertEquals('z, y, x', $res[0]['JsonableComment']['details']);
 	}
 
 }
-