我正在使用CakePHP 2.3.6。在一个项目中,我必须为多种用户(User Management System
)实施Users, Admins, etc.
。目前,我只关注Admin Panel(only 1 Admin for now)
和User Panel
。我希望Admin
可以访问所有区域(包括User Panel
的所有页面),Users
必须login
像往常一样访问某些区域(页面)。
我为controllers
和Admin Panel
创建了2个User Panel
,而不是为不同的plugins
创建了不同的user panels
。这是完整项目的代码:
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)
的{{1}}页面,说我已成功进入管理员面板,但我仍然无法访问管理面板。
我尝试login
,但结果相同。我认为$this->Auth->authorize('Controller')
或allow()
功能在这里已足够,但不知道发生了什么,我的错是什么。
在使用deny()
手动实施login/logout
系统之前,它运行良好。然后我想用Session
,但它让我疯狂。
有人可以帮助我吗?
由于
答案 0 :(得分:2)
首先要尝试遵循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()
的目的是什么?它是从某个地方打来的吗?