CakePHP 2.x中的用户管理系统表现奇怪

问题描述:

我正在使用CakePHP 2.3.6。在一个项目中,我必须为多种用户实现User Management SystemUsers, Admins, etc.)。目前,我只关注Admin Panel(only 1 Admin for now)User Panel。我希望Admin可以像平常一样访问所有区域(包括User Panel的所有页面),并且Users必须login才能访问某些区域(页面)。CakePHP 2.x中的用户管理系统表现奇怪

我创建2 controllersAdmin PanelUser Panel,而不是为不同user panels具有不同plugins。以下是完整的项目代码:

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()); 
} 

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

现在发生了什么事,在User panel(UsersController),用户不能访问任何页面而无需登录,但我希望我的用户将看到index页面而无需登录。

而且,当我从Admin panel(AdminsController)登录,它让我的User panel(UsersController)login页,说我成功地进入到我的管理员控制台,但我仍然无法访问管理面板。

我试过$this->Auth->authorize('Controller'),但结果相同。我以为allow()deny()功能就够了,但不知道发生了什么事,是我的错。

在我手动实施login/logout系统使用Session之前,它工作正常。然后我想用AuthComponent,但它让我疯狂。

任何人都可以帮助我吗?

感谢

第一件事首先尝试按照coding standards和最佳实践如不混合内嵌CSS和HTML等,这将使其更容易地看看你的代码并理解它。

现在就去解决您的问题。 AuthenticationComponent假设一切都被拒绝,除非它首先通过Auth::allow()。所以Auth::deny()只是让你的代码拒绝你已经被拒绝的方法,而不允许休息。你可以先说Auth::allow('*')然后否认某些事情,但我认为明确允许行动比明确否认它们更好(然后忘记拒绝新的行为并将其暴露给世界)。

当您在$this->redirect之前执行return语句时。这样你就可以在那时停止你的代码的执行,否则它可能会继续你没有想到的路径。由于您的问题似乎是错误的重定向,而不是重定向到$this->Auth->redirect()尝试$this->redirect(array('controller => 'admin', 'action' => 'index'));

最后是什么目的AppController::isAuthorized()?它是从某个地方叫来的吗?

+0

谢谢,看起来它会解决我的问题。其实,我也尝试过使用allow(),但结果相同。而且,isAuthorized()不会在任何地方使用,根据CakePHP文档中的示例,我只是将它保留在此处。那么,我会再次尝试删除它。@ user221931 – 2014-09-27 12:28:49

+0

而且,我应该使用这个:$ this-> Auth-> authorize('Controller')? – 2014-09-27 12:32:37

+0

工作更好,但仍然表现奇怪。我试图在“管理”面板和“用户”面板中使用相同的浏览器,相同的选项卡逐个登录。那是问题吗 ?有没有'饼干'的问题? @ user221931 – 2014-09-27 12:42:07