Skip to content

Commit

Permalink
Allow AuthComponent::$unauthorizedRedirect to be an url.
Browse files Browse the repository at this point in the history
Closes #3494
  • Loading branch information
ADmad committed Jan 12, 2013
1 parent e7330fa commit 676872d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/Cake/Controller/Component/AuthComponent.php
Expand Up @@ -215,11 +215,13 @@ class AuthComponent extends Component {
public $authError = null;

/**
* Controls handling of unauthorized access. By default unauthorized user is
* redirected to the referrer url or AuthComponent::$loginRedirect or '/'.
* If set to false a ForbiddenException exception is thrown instead of redirecting.
* Controls handling of unauthorized access.
* - For default value `true` unauthorized user is redirected to the referrer url
* or AuthComponent::$loginRedirect or '/'.
* - If set to a string or array the value is used as an url to redirect to.
* - If set to false a ForbiddenException exception is thrown instead of redirecting.
*
* @var boolean
* @var mixed
*/
public $unauthorizedRedirect = true;

Expand Down Expand Up @@ -345,16 +347,21 @@ public function startup(Controller $controller) {
* @throws ForbiddenException
*/
protected function _unauthorized(Controller $controller) {
if (!$this->unauthorizedRedirect) {
if ($this->unauthorizedRedirect === false) {
throw new ForbiddenException($this->authError);
}

$this->flash($this->authError);
$default = '/';
if (!empty($this->loginRedirect)) {
$default = $this->loginRedirect;
if ($this->unauthorizedRedirect === true) {
$default = '/';
if (!empty($this->loginRedirect)) {
$default = $this->loginRedirect;

This comment has been minimized.

Copy link
@SimonEast

SimonEast May 24, 2013

Shouldn't the 2 lines above refer to ->loginAction, not ->loginRedirect...?

This comment has been minimized.

Copy link
@ADmad

ADmad May 24, 2013

Author Member

No. It's been like this historically.

This comment has been minimized.

Copy link
@SimonEast

SimonEast May 26, 2013

OK, fair enough, it was like that before your edit.

I guess my question is: is it logical to send an not-yet-authorised user to loginRedirect? From the class comments it appears that loginAction is used to typically display a login form, while loginRedirect is where to send a user post-login, such as a member area, dashboard, admin area or whatsoever. Does it make sense to send unauthorised users there?

This comment has been minimized.

Copy link
@markstory

markstory May 26, 2013

Member

Changing things around with where redirects go often results in more issues being opened, as some new edge case is created. Being sent to loginRedirect will most likely result in another redirect, but the proper session state.

This comment has been minimized.

Copy link
@ADmad

ADmad May 26, 2013

Author Member

Given the fact that redirection url is customizable using unauthorizedRedirect I don't see any need to change the default redirection and potentially cause more issues.

}
$url = $controller->referer($default, true);
} else {
$url = $this->unauthorizedRedirect;
}
$controller->redirect($controller->referer($default, true), null, true);
$controller->redirect($url, null, true);
return false;
}

Expand Down
31 changes: 31 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php
Expand Up @@ -907,6 +907,37 @@ public function testDefaultToLoginRedirect() {
$this->Auth->startup($Controller);
}

/**
* testRedirectToUnauthorizedRedirect
*
* @return void
*/
public function testRedirectToUnauthorizedRedirect() {
$url = '/party/on';
$this->Auth->request = $CakeRequest = new CakeRequest($url);
$this->Auth->request->addParams(Router::parse($url));
$this->Auth->authorize = array('Controller');
$this->Auth->login(array('username' => 'admad', 'password' => 'cake'));
$this->Auth->unauthorizedRedirect = array(
'controller' => 'no_can_do', 'action' => 'jack'
);

$CakeResponse = new CakeResponse();
$Controller = $this->getMock(
'Controller',
array('on', 'redirect'),
array($CakeRequest, $CakeResponse)
);

$expected = array(
'controller' => 'no_can_do', 'action' => 'jack'
);
$Controller->expects($this->once())
->method('redirect')
->with($this->equalTo($expected));
$this->Auth->startup($Controller);
}

/**
* Throw ForbiddenException if AuthComponent::$unauthorizedRedirect set to false
* @expectedException ForbiddenException
Expand Down

0 comments on commit 676872d

Please sign in to comment.