Skip to content

Commit

Permalink
Fixing AuthComponent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 18, 2014
1 parent 346c341 commit eafe1ac
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 103 deletions.
59 changes: 27 additions & 32 deletions src/Controller/Component/AuthComponent.php
Expand Up @@ -105,10 +105,9 @@ class AuthComponent extends Component {
* request is made with an invalid or expired session.
*
* - `flash` - Settings to use when Auth needs to do a flash message with
* SessionComponent::setFlash(). Available keys are:
* Session::flash(). Available keys are:
*
* - `element` - The element to use, defaults to 'default'.
* - `key` - The key to use, defaults to 'auth'
* - `key` - The message domain to use for flashes generated by this component, defaults to 'auth'.
* - `params` - The array of additional params to use, defaults to []
*
* - `loginAction` - A URL (defined as a string or array) to the controller action
Expand Down Expand Up @@ -208,18 +207,18 @@ class AuthComponent extends Component {
public $response;

/**
* Method list for bound controller.
* Instance of the Session object
*
* @var array
* @return void
*/
protected $_methods = array();
public $session;

/**
* Instance of the Session object
* Method list for bound controller.
*
* @return void
* @var array
*/
protected $_session;
protected $_methods = array();

/**
* Initializes AuthComponent for use in the controller.
Expand All @@ -232,7 +231,7 @@ public function initialize(Event $event) {
$this->request = $controller->request;
$this->response = $controller->response;
$this->_methods = $controller->methods;
$this->_session = $controller->request->session();
$this->session = $controller->request->session();

if (Configure::read('debug')) {
Debugger::checkSecurityKeys();
Expand Down Expand Up @@ -331,17 +330,17 @@ protected function _unauthenticated(Controller $controller) {

if ($this->_isLoginAction($controller)) {
if (empty($controller->request->data) &&
!$this->_session->check('Auth.redirect') &&
!$this->session->check('Auth.redirect') &&
$this->request->env('HTTP_REFERER')
) {
$this->_session->write('Auth.redirect', $controller->referer(null, true));
$this->session->write('Auth.redirect', $controller->referer(null, true));
}
return;
}

if (!$controller->request->is('ajax')) {
$this->flash($this->_config['authError']);
$this->_session->write('Auth.redirect', $controller->request->here(false));
$this->session->write('Auth.redirect', $controller->request->here(false));
return $controller->redirect($this->_config['loginAction']);
}

Expand Down Expand Up @@ -583,8 +582,8 @@ public function login($user = null) {
$user = $this->identify($this->request, $this->response);
}
if ($user) {
$this->_session->renew();
$this->_session->write($this->_sessionKey, $user);
$this->session->renew();
$this->session->write($this->sessionKey, $user);
}
return (bool)$this->user();
}
Expand All @@ -610,9 +609,9 @@ public function logout() {
foreach ($this->_authenticateObjects as $auth) {
$auth->logout($user);
}
$this->_session->delete($this->sessionKey);
$this->_session->delete('Auth.redirect');
$this->_session->renew();
$this->session->delete($this->sessionKey);
$this->session->delete('Auth.redirect');
$this->session->renew();
return Router::normalize($this->_config['logoutRedirect']);
}

Expand All @@ -630,8 +629,8 @@ public function logout() {
public function user($key = null) {
if (!empty($this->_user)) {
$user = $this->_user;
} elseif ($this->_sessionKey && $this->_session->check($this->sessionKey)) {
$user = $this->_session->read($this->_sessionKey);
} elseif ($this->sessionKey && $this->session->check($this->sessionKey)) {
$user = $this->session->read($this->sessionKey);
} else {
return null;
}
Expand All @@ -650,7 +649,7 @@ public function user($key = null) {
protected function _getUser() {
$user = $this->user();
if ($user) {
$this->_session->delete('Auth.redirect');
$this->session->delete('Auth.redirect');
return true;
}

Expand Down Expand Up @@ -689,10 +688,10 @@ protected function _getUser() {
public function redirectUrl($url = null) {
if ($url !== null) {
$redir = $url;
$this->_session->write('Auth.redirect', $redir);
} elseif ($this->_session->check('Auth.redirect')) {
$redir = $this->_session->read('Auth.redirect');
$this->_session->delete('Auth.redirect');
$this->session->write('Auth.redirect', $redir);
} elseif ($this->session->check('Auth.redirect')) {
$redir = $this->session->read('Auth.redirect');
$this->session->delete('Auth.redirect');

if (Router::normalize($redir) === Router::normalize($this->_config['loginAction'])) {
$redir = $this->_config['loginRedirect'];
Expand Down Expand Up @@ -770,17 +769,13 @@ public function constructAuthenticate() {
* @param string $message The message to set.
* @return void
*/
public function flash($message) {
public function flash($message, $type = 'error') {
if ($message === false) {
return;
}
$flashConfig = $this->_config['flash'];
$this->_session->setFlash(
$message,
$flashConfig['element'],
$flashConfig['params'],
$flashConfig['key']
);
$key = $flashConfig['key'];
$this->session->flash($message, 'error', $flashConfig['params'] + compact('key'));
}

}
11 changes: 9 additions & 2 deletions src/Network/Request.php
Expand Up @@ -440,10 +440,17 @@ protected function _processFileData(&$post, $path, $data, $field) {
/**
* Returns the instance of the Session object for this request
*
* If a session obkect is passed as first argument it will be set as
* the session to use for this request
*
* @param \Cake\Network\Session $session the session object to use
* @return \Cake\Network\Session
*/
public function session() {
return $this->_session;
public function session(Session $session = null) {
if ($session === null) {
return $this->_session;
}
return $this->_session = $session;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Network/Session.php
Expand Up @@ -486,7 +486,9 @@ public function clear() {
* @return bool
*/
protected function _hasSession() {
return !ini_get('session.use_cookies') || isset($_COOKIE[session_name()]);
return !ini_get('session.use_cookies')
|| isset($_COOKIE[session_name()])
|| php_sapi_name() === 'cli';
}

/**
Expand All @@ -495,7 +497,7 @@ protected function _hasSession() {
* @return void
*/
public function renew() {
if (!$this->_hasSession()) {
if (!$this->_hasSession() || php_sapi_name() === 'cli') {
return;
}

Expand Down

0 comments on commit eafe1ac

Please sign in to comment.