From b207ee8cbe2716f241137ba000e2c91132b740f2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 21 Jan 2011 16:55:02 -0500 Subject: [PATCH] Making AuthComponent::user static, so user data can be fetched from anywhere. --- cake/libs/controller/components/auth.php | 20 +++++++++---------- .../libs/controller/components/auth.test.php | 7 ++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/cake/libs/controller/components/auth.php b/cake/libs/controller/components/auth.php index 6e02e13d3..7dd94840e 100644 --- a/cake/libs/controller/components/auth.php +++ b/cake/libs/controller/components/auth.php @@ -21,6 +21,7 @@ App::import('Core', 'Router', false); App::import('Core', 'Security', false); +App::import('Core', 'CakeSession', false); App::import('Component', 'auth/base_authorize'); /** @@ -159,7 +160,7 @@ class AuthComponent extends Component { * @var string * @link http://book.cakephp.org/view/1276/sessionKey */ - public $sessionKey = 'Auth.User'; + public static $sessionKey = 'Auth.User'; /** * If using action-based access control, this defines how the paths to action @@ -445,7 +446,7 @@ class AuthComponent extends Component { * * You can use allow with either an array, or var args. * - * `$this->Auth->allow(array('edit', 'add'));` + * `$this->Auth->allow(array('edit', 'add'));` or * `$this->Auth->allow('edit', 'add');` * * allow() also supports '*' as a wildcard to mean all actions. @@ -475,7 +476,7 @@ class AuthComponent extends Component { * * You can use deny with either an array, or var args. * - * `$this->Auth->deny(array('edit', 'add'));` + * `$this->Auth->deny(array('edit', 'add'));` or * `$this->Auth->deny('edit', 'add');` * * @param mixed $action Controller action name or array of actions @@ -534,7 +535,7 @@ class AuthComponent extends Component { $user = $this->identify($this->request); } if ($user) { - $this->Session->write($this->sessionKey, $user); + $this->Session->write(self::$sessionKey, $user); $this->_loggedIn = true; } return $this->_loggedIn; @@ -550,7 +551,7 @@ class AuthComponent extends Component { */ public function logout() { $this->__setDefaults(); - $this->Session->delete($this->sessionKey); + $this->Session->delete(self::$sessionKey); $this->Session->delete('Auth.redirect'); $this->_loggedIn = false; return Router::normalize($this->logoutRedirect); @@ -563,16 +564,15 @@ class AuthComponent extends Component { * @return mixed User record. or null if no user is logged in. * @link http://book.cakephp.org/view/1264/user */ - public function user($key = null) { - $this->__setDefaults(); - if (!$this->Session->check($this->sessionKey)) { + public static function user($key = null) { + if (!CakeSession::check(self::$sessionKey)) { return null; } if ($key == null) { - return $this->Session->read($this->sessionKey); + return CakeSession::read(self::$sessionKey); } else { - $user = $this->Session->read($this->sessionKey); + $user = CakeSession::read(self::$sessionKey); if (isset($user[$key])) { return $user[$key]; } diff --git a/cake/tests/cases/libs/controller/components/auth.test.php b/cake/tests/cases/libs/controller/components/auth.test.php index 94f3b8d7b..1f3a2a49b 100644 --- a/cake/tests/cases/libs/controller/components/auth.test.php +++ b/cake/tests/cases/libs/controller/components/auth.test.php @@ -1178,8 +1178,7 @@ class AuthTest extends CakeTestCase { 'Auth' => array( 'fields' => array('username' => 'email', 'password' => 'password'), 'loginAction' => array('controller' => 'people', 'action' => 'login'), - 'userModel' => 'AuthUserCustomField', - 'sessionKey' => 'AltAuth.AuthUserCustomField' + 'userModel' => 'AuthUserCustomField' ), 'Session' ); @@ -1212,14 +1211,12 @@ class AuthTest extends CakeTestCase { 'fields' => array('username' => 'email', 'password' => 'password'), 'loginAction' => array('controller' => 'people', 'action' => 'login'), 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'), - 'userModel' => 'AuthUserCustomField', - 'sessionKey' => 'AltAuth.AuthUserCustomField' + 'userModel' => 'AuthUserCustomField' ); $this->assertEqual($expected['fields'], $this->Controller->Auth->fields); $this->assertEqual($expected['loginAction'], $this->Controller->Auth->loginAction); $this->assertEqual($expected['logoutRedirect'], $this->Controller->Auth->logoutRedirect); $this->assertEqual($expected['userModel'], $this->Controller->Auth->userModel); - $this->assertEqual($expected['sessionKey'], $this->Controller->Auth->sessionKey); } /**