我知道有很多关于这个主题的问题,但是我理解应该有一个错误修正。所以我认为我的问题必须是一个不同的,因为我使用CakePHP 2.5.6,它应该已经有错误修复,对吗?
好吧,我正在尝试将“简单身份验证和授权应用程序”改编为我的项目。只要我不加行 'authorize'=>阵列(“控制器”) 我可以添加用户,登录,注销以及登录和注销重定向工作正常。
只要我添加该行,该应用就会表现得很奇怪: 1.登录和登录重定向工作 2. users / logout也导致Missing-Controller-Error,因为它调用了一个带双基数的url。它还调用登录重定向的重定向URL而不是logout-redirect。当我调用/ users / logout时,应用程序尝试访问localhost / project_xyz / project_xyz / tests
AppController的:
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');
}
}
有人可以帮忙吗?
[编辑:
我将它添加到components-array:
'unauthorizedRedirect' => [
'controller' => 'users',
'action' => 'login',
'prefix' => false ]
效果是,当我现在调用users / logout时,而不是之前的missing-controller-error,我将被重定向到users / login。不幸的是,用户尚未注销。我仍然可以访问所有内容,就像用户仍然登录一样。
[编辑#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());
}
答案 0 :(得分:3)
似乎问题是'退出'必须在beforeFilter中,我错过了:
public function beforeFilter() {
$this->Auth->allow('login','logout');
}
尽管如此,这只适用于我在AppController的组件数组中与此相结合:
'unauthorizedRedirect' => [
'controller' => 'users',
'action' => 'login',
'prefix' => false ]
如果我把它留下并添加一些模型加速的isAuthorized-functions,我仍然会得到带有double-base-url的missing-controller-error。 authorizedRedirect似乎有问题。这种解决方法虽然适用于我...