CakePHP2.x中的用户管理系统行为奇怪

CakePHP2.x中的用户管理系统行为奇怪,cakephp,authentication,authorization,Cakephp,Authentication,Authorization,我使用的是CakePHP2.3.6。在一个项目中,我必须为多种类型的用户(用户、管理员等)实施用户管理系统)。目前,我只关心管理面板(目前只有1名管理员)和用户面板。我希望管理员可以访问所有区域(包括用户面板的所有页面),并且用户必须像往常一样登录才能访问某些区域(页面) 我为管理面板和用户面板创建了2个控制器,而不是为不同的用户面板创建不同的插件。以下是完整的项目代码: AppController.php : public $components=array('Session','Reque

我使用的是CakePHP2.3.6。在一个项目中,我必须为多种类型的用户(
用户、管理员等)实施
用户管理系统
)。目前,我只关心
管理面板(目前只有1名管理员)
用户面板
。我希望
管理员
可以访问所有区域(包括
用户面板
的所有页面),并且
用户
必须像往常一样登录
才能访问某些区域(页面)

我为
管理面板
用户面板
创建了2个
控制器
,而不是为不同的
用户面板
创建不同的
插件。以下是完整的项目代码:

AppController.php :

public $components=array('Session','RequestHandler','Auth');
public function isAuthorized($user){
    if(isset($user['role']) && $user['role']==='admin')
        return true;
    return false;
}

UsersController.php :

public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->loginRedirect=array('action'=>'editProfile');
    $this->Auth->logoutRedirect=array('action'=>'index');
    $this->Auth->authenticate=array('Form'=>array('scope'=>array('User.role'=>"user"),'userModel'=>'User','fields'=>array('username','password')));
    $this->Auth->unauthorizedRedirect=array('action'=>'login');
    $this->Auth->loginAction=array('action'=>'login');
    $this->Auth->deny('editCv','logout');
    $this->layout='user_layout';
}
public function login(){
    if($this->request->is('post'))
        if($this->Auth->login()){
            $this->Session->setFlash('Welcome '.$this->User->field('name',array('User.id'=>$this->Auth->user('id'))));
            $this->redirect($this->Auth->redirect());
        }else{
            $this->Session->setFlash('Invalid username or password, please try again');
            $this->set('title_for_layout','Error - Login');
        }
}
public function logout(){
    $this->redirect($this->Auth->logout());
}

AdminsController.php

public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->loginRedirect=array('action'=>'myJobs');
    $this->Auth->logoutRedirect=array('action'=>'index');
    $this->Auth->authenticate=array('Form'=>array('scope'=>array('User.role'=>"admin"),'userModel'=>'User','fields'=>array('username','password')));
    $this->Auth->authError='Did you really think you are allowed to see that ?';
    $this->Auth->unauthorizedRedirect=array('action'=>'index');
    $this->Auth->loginAction=array('action'=>'index');
    $this->Auth->deny('myUsers','deleteUser','logout');
    $this->layout='admin_layout';
}
public function index(){
    if($this->request->is('post'))
        if($this->Auth->login()){
            $this->Session->setFlash("<p style='margin-left:20px;color:#366;'><strong>Welcome Admin, You have successfully entered to your Admin Panel!</strong></p>");
            $this->redirect($this->Auth->redirect());
        }else{
            $this->Session->setFlash('Invalid username or password, please try again');
            $this->set('title_for_layout','Error - Login');
        }
    else
        $this->set('title_for_layout','Admin');
    $this->layout=false;
}
public function logout(){
    $this->redirect($this->Auth->logout());
}
AppController.php:
public$components=array('Session','RequestHandler','Auth');
公共功能已授权($user){
如果(isset($user['role'])&&$user['role']=='admin')
返回true;
返回false;
}
UsersController.php:
公共函数beforeFilter(){
父::beforeFilter();
$this->Auth->loginRedirect=array('action'=>editProfile');
$this->Auth->logoutRedirect=array('action'=>'index');
$this->Auth->authenticate=array('Form'=>array('scope'=>array('User.role'=>“User”),'userModel'=>'User','fields'=>array('username','password'));
$this->Auth->unauthorizedRedirect=array('action'=>'login');
$this->Auth->loginAction=array('action'=>login');
$this->Auth->deny('editCv','logout');
$this->layout='user_layout';
}
公共函数登录(){
如果($this->request->is('post'))
如果($this->Auth->login()){
$this->Session->setFlash('Welcome')。$this->User->field('name',array('User.id'=>$this->Auth->User('id'));
$this->redirect($this->Auth->redirect());
}否则{
$this->Session->setFlash(“无效的用户名或密码,请重试”);
$this->set('title_for_layout','Error-Login');
}
}
公共功能注销(){
$this->redirect($this->Auth->logout());
}
AdminsController.php
公共函数beforeFilter(){
父::beforeFilter();
$this->Auth->loginRedirect=array('action'=>'myJobs');
$this->Auth->logoutRedirect=array('action'=>'index');
$this->Auth->authenticate=array('Form'=>array('scope'=>array('User.role'=>“admin”)、'userModel'=>'User'、'fields'=>array('username','password'));
$this->Auth->authror='你真的认为你可以看到那个吗?';
$this->Auth->unauthorizedRedirect=array('action'=>'index');
$this->Auth->loginAction=array('action'=>'index');
$this->Auth->deny('myUsers','deleteUser','logout');
$this->layout='admin_layout';
}
公共职能指数(){
如果($this->request->is('post'))
如果($this->Auth->login()){
$this->Session->setFlash(

欢迎管理员,您已成功进入管理员面板!”; $this->redirect($this->Auth->redirect()); }否则{ $this->Session->setFlash(“无效的用户名或密码,请重试”); $this->set('title_for_layout','Error-Login'); } 其他的 $this->set('title_for_layout','Admin'); $this->layout=false; } 公共功能注销(){ $this->redirect($this->Auth->logout()); }

在这里,我分别在两个面板中实现
login
,因为我有两个不同的
login页面
。这就是为什么我分别在两个面板中配置了
AuthComponent

现在,在
用户面板(UsersController)
中,用户不登录就无法访问任何页面,但我希望我的用户不登录就可以看到
索引
页面

而且,当我从
管理面板
(AdminsController)登录时,它会让我进入
用户面板(UsersController)
登录
页面,说明我成功进入了管理面板,但我仍然无法访问管理面板

我尝试了
$this->Auth->authorize('Controller')
,但结果相同。我以为
allow()
deny()
函数在这里就足够了,但不知道发生了什么,什么是我的错

在此之前,我使用
会话
手动实现了
登录/注销
系统,并且运行良好。然后我想使用
AuthComponent
,但这让我抓狂

有人能帮我吗


谢谢

首先要做的事情是尝试遵循最好的实践,如不要将内联css与html混合使用等。这将使查看代码和理解代码变得容易得多

现在谈谈你的问题
AuthenticationComponent
假定所有内容都被拒绝,除非首先传入
Auth::allow()
。因此,
Auth::deny()
只是让代码拒绝已经被拒绝的方法,而不允许其他方法。你可以说“代码> Auth::允许(‘*’)< /代码>先,然后否认某事,但是我认为最好明确地允许动作而不是明确地拒绝它们(然后忘记拒绝一个新的动作并将其暴露给世界)。 执行
$this->redirect
时,请在其前面加一条return语句。通过这种方式,您可以在该点停止代码的执行,否则它可能会继续执行到您意想不到的路径。由于您的问题似乎是错误的重定向,请不要重定向到
$this->Auth->redirect()
尝试
$this->redirect(数组('controller=>'admin','action'=>'index'))


最后,AppController::isAuthorized()的目的是什么?是从什么地方打来的吗?

谢谢,看来这会解决我的问题。实际上,我也尝试过使用allow(),但结果相同。而且,isAuthorized()没有在任何地方使用,我只是根据CakePHP文档中的示例将其保存在这里。好吧,我会再次尝试删除它@user221931,我是否应该使用:$this->Auth->authorize('Controller')?工作得更好,但仍然表现得更好