mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
parent
e7330fa585
commit
676872d623
2 changed files with 47 additions and 9 deletions
|
@ -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);
|
||||||
$default = '/';
|
if ($this->unauthorizedRedirect === true) {
|
||||||
if (!empty($this->loginRedirect)) {
|
$default = '/';
|
||||||
$default = $this->loginRedirect;
|
if (!empty($this->loginRedirect)) {
|
||||||
|
$default = $this->loginRedirect;
|
||||||
|
}
|
||||||
|
$url = $controller->referer($default, true);
|
||||||
|
} else {
|
||||||
|
$url = $this->unauthorizedRedirect;
|
||||||
}
|
}
|
||||||
$controller->redirect($controller->referer($default, true), null, true);
|
$controller->redirect($url, null, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue