Allow AuthComponent::$unauthorizedRedirect to be an url.

Closes #3494
This commit is contained in:
ADmad 2012-12-30 15:43:11 +05:30
parent e7330fa585
commit 676872d623
2 changed files with 47 additions and 9 deletions

View file

@ -215,11 +215,13 @@ class AuthComponent extends Component {
public $authError = null; public $authError = null;
/** /**
* Controls handling of unauthorized access. By default unauthorized user is * Controls handling of unauthorized access.
* redirected to the referrer url or AuthComponent::$loginRedirect or '/'. * - For default value `true` unauthorized user is redirected to the referrer url
* If set to false a ForbiddenException exception is thrown instead of redirecting. * 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; public $unauthorizedRedirect = true;
@ -345,16 +347,21 @@ class AuthComponent extends Component {
* @throws ForbiddenException * @throws ForbiddenException
*/ */
protected function _unauthorized(Controller $controller) { protected function _unauthorized(Controller $controller) {
if (!$this->unauthorizedRedirect) { if ($this->unauthorizedRedirect === false) {
throw new ForbiddenException($this->authError); throw new ForbiddenException($this->authError);
} }
$this->flash($this->authError); $this->flash($this->authError);
if ($this->unauthorizedRedirect === true) {
$default = '/'; $default = '/';
if (!empty($this->loginRedirect)) { if (!empty($this->loginRedirect)) {
$default = $this->loginRedirect; $default = $this->loginRedirect;
} }
$controller->redirect($controller->referer($default, true), null, true); $url = $controller->referer($default, true);
} else {
$url = $this->unauthorizedRedirect;
}
$controller->redirect($url, null, true);
return false; return false;
} }

View file

@ -907,6 +907,37 @@ class AuthComponentTest extends CakeTestCase {
$this->Auth->startup($Controller); $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 * Throw ForbiddenException if AuthComponent::$unauthorizedRedirect set to false
* @expectedException ForbiddenException * @expectedException ForbiddenException