Skip to content

Commit

Permalink
Making AuthComponent::user static, so user data can be fetched from a…
Browse files Browse the repository at this point in the history
…nywhere.
  • Loading branch information
markstory committed Feb 4, 2011
1 parent 35864c2 commit b207ee8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
20 changes: 10 additions & 10 deletions cake/libs/controller/components/auth.php
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -445,7 +446,7 @@ public function constructAuthorize() {
*
* 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.
Expand Down Expand Up @@ -475,7 +476,7 @@ public function allow() {
*
* 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
Expand Down Expand Up @@ -534,7 +535,7 @@ public function login($user = null) {
$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;
Expand All @@ -550,7 +551,7 @@ public function login($user = null) {
*/
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);
Expand All @@ -563,16 +564,15 @@ public function logout() {
* @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];
}
Expand Down
7 changes: 2 additions & 5 deletions cake/tests/cases/libs/controller/components/auth.test.php
Expand Up @@ -1178,8 +1178,7 @@ function testComponentSettings() {
'Auth' => array(
'fields' => array('username' => 'email', 'password' => 'password'),
'loginAction' => array('controller' => 'people', 'action' => 'login'),
'userModel' => 'AuthUserCustomField',
'sessionKey' => 'AltAuth.AuthUserCustomField'
'userModel' => 'AuthUserCustomField'
),
'Session'
);
Expand Down Expand Up @@ -1212,14 +1211,12 @@ function testComponentSettings() {
'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);
}

/**
Expand Down

0 comments on commit b207ee8

Please sign in to comment.