CakePHP 身份验证:注销重定向错误且具有双 url 基数
CakePHP auth: logout redirect wrong and with double url base
我知道关于这个话题有很多问题,但据我所知应该有一个错误修复。所以我认为我的问题一定是不同的,因为我使用的是 CakePHP 2.5.6,它应该已经修复了错误,对吗?
好吧,我正在努力使 "Simple Authentication and Authorization Application" 适应我的项目。只要我不添加行
'authorize' => 数组('Controller')
我可以添加用户、登录、注销,并且登录和注销重定向工作正常。
只要我添加该行,应用程序就会表现得很奇怪:
1.登录和登录重定向工作
2. users/logout 也会导致 Missing-Controller-Error,因为它调用了双基数的 url。它还调用登录重定向的重定向 url 而不是注销重定向。当我调用 /users/logout 时,应用会尝试访问 localhost/project_xyz/project_xyz/tests
应用程序控制器:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'tests',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display','home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
'authorize' => array('Controller'),
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow('display');
}
}
有人可以帮忙吗?
[编辑:]
我将其添加到组件数组中:
'unauthorizedRedirect' => [
'controller' => 'users',
'action' => 'login',
'prefix' => false ]
效果是,当我现在调用 users/logout 时,我将被重定向到 users/login,而不是之前的 missing-controller-error。不幸的是,用户尚未注销。我仍然可以像用户仍然登录一样访问所有内容。
[编辑 #2:]
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
问题似乎是 'logout' 必须在 beforeFilter 中,我错过了:
public function beforeFilter() {
$this->Auth->allow('login','logout');
}
不过,这仅适用于我在 AppController 的组件数组中与此结合使用:
'unauthorizedRedirect' => [
'controller' => 'users',
'action' => 'login',
'prefix' => false ]
如果我忽略它并添加一些特定于模型的 isAuthorized-functions,我仍然会得到带有双基数的 missing-controller-error-url。 unauthorizedRedirect 似乎有问题。不过,此解决方法对我有用...
我知道关于这个话题有很多问题,但据我所知应该有一个错误修复。所以我认为我的问题一定是不同的,因为我使用的是 CakePHP 2.5.6,它应该已经修复了错误,对吗?
好吧,我正在努力使 "Simple Authentication and Authorization Application" 适应我的项目。只要我不添加行 'authorize' => 数组('Controller') 我可以添加用户、登录、注销,并且登录和注销重定向工作正常。
只要我添加该行,应用程序就会表现得很奇怪: 1.登录和登录重定向工作 2. users/logout 也会导致 Missing-Controller-Error,因为它调用了双基数的 url。它还调用登录重定向的重定向 url 而不是注销重定向。当我调用 /users/logout 时,应用会尝试访问 localhost/project_xyz/project_xyz/tests
应用程序控制器:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'tests',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display','home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
'authorize' => array('Controller'),
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow('display');
}
}
有人可以帮忙吗?
[编辑:]
我将其添加到组件数组中:
'unauthorizedRedirect' => [
'controller' => 'users',
'action' => 'login',
'prefix' => false ]
效果是,当我现在调用 users/logout 时,我将被重定向到 users/login,而不是之前的 missing-controller-error。不幸的是,用户尚未注销。我仍然可以像用户仍然登录一样访问所有内容。
[编辑 #2:]
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
问题似乎是 'logout' 必须在 beforeFilter 中,我错过了:
public function beforeFilter() {
$this->Auth->allow('login','logout');
}
不过,这仅适用于我在 AppController 的组件数组中与此结合使用:
'unauthorizedRedirect' => [
'controller' => 'users',
'action' => 'login',
'prefix' => false ]
如果我忽略它并添加一些特定于模型的 isAuthorized-functions,我仍然会得到带有双基数的 missing-controller-error-url。 unauthorizedRedirect 似乎有问题。不过,此解决方法对我有用...