Skip to content

Commit

Permalink
Breaking down AuthComponent::startup() into multiple methods for easi…
Browse files Browse the repository at this point in the history
…er management and extension.
  • Loading branch information
ADmad committed Feb 10, 2013
1 parent 13029cc commit 49157d8
Showing 1 changed file with 50 additions and 24 deletions.
74 changes: 50 additions & 24 deletions lib/Cake/Controller/Component/AuthComponent.php
Expand Up @@ -294,12 +294,36 @@ public function startup(Controller $controller) {
if (!$this->_setDefaults()) {
return false;
}
$request = $controller->request;

$url = '';
if ($this->_isAllowed($controller)) {
return true;
}

if (!$this->_getUser()) {
return $this->_unauthenticated($controller);
}

if (empty($this->authorize) || $this->isAuthorized($this->user())) {
return true;
}

return $this->_unauthorized($controller);
}

/**
* Checks whether current action is accessible without authentication.
* If current action is login action referrer url is saved in session which is
* later accessible using AuthComponent::redirectUrl().
*
* @param Controller $controller A reference to the instantiating controller object
* @return boolean True if action is accessible without authentication else false
*/
protected function _isAllowed(Controller $controller) {
$action = strtolower($controller->request->params['action']);

if (isset($request->url)) {
$url = $request->url;
$url = '';
if (isset($controller->request->url)) {
$url = $controller->request->url;
}
$url = Router::normalize($url);
$loginAction = Router::normalize($this->loginAction);
Expand All @@ -309,35 +333,37 @@ public function startup(Controller $controller) {
}

if ($loginAction == $url) {
if (empty($request->data)) {
if (empty($controller->request->data)) {
if (!$this->Session->check('Auth.redirect') && !$this->loginRedirect && env('HTTP_REFERER')) {
$this->Session->write('Auth.redirect', $controller->referer(null, true));
}
}
return true;
}
return false;
}

if (!$this->_getUser()) {
if (!$request->is('ajax')) {
$this->flash($this->authError);
$this->Session->write('Auth.redirect', $request->here());
$controller->redirect($loginAction);
return false;
}
if (!empty($this->ajaxLogin)) {
$controller->viewPath = 'Elements';
echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
$this->_stop();
return false;
}
$controller->redirect(null, 403);
/**
* Handle unauthenticated access attempt.
*
* @param Controller $controller A reference to the controller object
* @return boolean Returns false
*/
protected function _unauthenticated(Controller $controller) {
if (!$controller->request->is('ajax')) {
$this->flash($this->authError);
$this->Session->write('Auth.redirect', $controller->request->here());
$controller->redirect($this->loginAction);
return false;
}

if (empty($this->authorize) || $this->isAuthorized($this->user())) {
return true;
if (!empty($this->ajaxLogin)) {
$controller->viewPath = 'Elements';
echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
$this->_stop();
return false;
}

return $this->_unauthorized($controller);
$controller->redirect(null, 403);
return false;
}

/**
Expand Down

0 comments on commit 49157d8

Please sign in to comment.