Starting to remove magic around userModel, and deprecating/removing userModel from AuthComponent.

This commit is contained in:
mark_story 2011-01-19 16:53:58 -05:00
parent b59d0e8bb1
commit d8f2cf9395
2 changed files with 20 additions and 41 deletions

View file

@ -154,12 +154,12 @@ class AuthComponent extends Component {
/**
* The session key name where the record of the current user is stored. If
* unspecified, it will be "Auth.{$userModel name}".
* unspecified, it will be "Auth.User".
*
* @var string
* @link http://book.cakephp.org/view/1276/sessionKey
*/
public $sessionKey = null;
public $sessionKey = 'Auth.User';
/**
* If using action-based access control, this defines how the paths to action
@ -174,12 +174,16 @@ class AuthComponent extends Component {
/**
* A URL (defined as a string or array) to the controller action that handles
* logins.
* logins. Defaults to `/users/login`
*
* @var mixed
* @link http://book.cakephp.org/view/1269/loginAction
*/
public $loginAction = null;
public $loginAction = array(
'controller' => 'users',
'action' => 'login',
'plugin' => null
);
/**
* Normally, if a user is redirected to the $loginAction page, the location they
@ -268,8 +272,6 @@ class AuthComponent extends Component {
*/
public function initialize($controller) {
$this->request = $controller->request;
$this->params = $this->request;
$this->_methods = $controller->methods;
if (Configure::read('debug') > 0) {
@ -399,18 +401,7 @@ class AuthComponent extends Component {
* @access private
*/
function __setDefaults() {
if (empty($this->userModel)) {
trigger_error(__("Could not find \$userModel. Please set AuthComponent::\$userModel in beforeFilter()."), E_USER_WARNING);
return false;
}
list($plugin, $model) = pluginSplit($this->userModel);
$defaults = array(
'loginAction' => array(
'controller' => Inflector::underscore(Inflector::pluralize($model)),
'action' => 'login',
'plugin' => Inflector::underscore($plugin),
),
'sessionKey' => 'Auth.' . $model,
'logoutRedirect' => $this->loginAction,
'loginError' => __('Login failed. Invalid username or password.'),
'authError' => __('You are not authorized to access that location.')
@ -597,8 +588,7 @@ class AuthComponent extends Component {
}
if ($key == null) {
$model = $this->getModel();
return array($model->alias => $this->Session->read($this->sessionKey));
return $this->Session->read($this->sessionKey);
} else {
$user = $this->Session->read($this->sessionKey);
if (isset($user[$key])) {

View file

@ -582,12 +582,12 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->startup($this->Controller);
$user = $this->Controller->Auth->user();
$expected = array('AuthUser' => array(
$expected = array(
'id' => 1,
'username' => 'mariano',
'created' => '2007-03-17 01:16:23',
'updated' => date('Y-m-d H:i:s')
));
);
$this->assertEqual($user, $expected);
$this->Controller->Session->delete('Auth');
@ -660,7 +660,7 @@ class AuthTest extends CakeTestCase {
function testAuthorizeFalse() {
$this->AuthUser = new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
$this->Controller->Session->write('Auth.User', $user['AuthUser']);
$this->Controller->Auth->userModel = 'AuthUser';
$this->Controller->Auth->authorize = false;
$this->Controller->request->addParams(Router::parse('auth_test/add'));
@ -1008,7 +1008,7 @@ class AuthTest extends CakeTestCase {
);
$this->Controller->Auth->userModel = 'AuthUser';
$this->Controller->Auth->startup($this->Controller);
$expected = Router::normalize('/');
$expected = Router::normalize('/AuthTest/login');
$this->assertEqual($expected, $this->Controller->testUrl);
$this->Controller->Session->delete('Auth');
@ -1292,25 +1292,14 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->startup($this->Controller);
$user = $this->Controller->Auth->user();
$expected = array(
'TestPluginAuthUser' => array(
'id' => 1,
'username' => 'gwoo',
'created' => '2007-03-17 01:16:23',
'updated' => date('Y-m-d H:i:s')
));
'id' => 1,
'username' => 'gwoo',
'created' => '2007-03-17 01:16:23',
'updated' => date('Y-m-d H:i:s')
);
$this->assertEqual($user, $expected);
$sessionKey = $this->Controller->Auth->sessionKey;
$this->assertEqual('Auth.TestPluginAuthUser', $sessionKey);
$this->Controller->Auth->loginAction = null;
$this->Controller->Auth->__setDefaults();
$loginAction = $this->Controller->Auth->loginAction;
$expected = array(
'controller' => 'test_plugin_auth_users',
'action' => 'login',
'plugin' => 'test_plugin'
);
$this->assertEqual($loginAction, $expected);
$this->assertEqual('Auth.User', $sessionKey);
// Reverting changes
Cache::delete('object_map', '_cake_core_');
@ -1529,7 +1518,7 @@ class AuthTest extends CakeTestCase {
* @return void
*/
function testFlashSettings() {
$this->Controller->Auth->Session = $this->getMock('SessionComponent', array(), array(), '', zfalse);
$this->Controller->Auth->Session = $this->getMock('SessionComponent', array(), array(), '', false);
$this->Controller->Auth->Session->expects($this->once())
->method('setFlash')
->with('Auth failure', 'custom', array(1), 'auth-key');