我正在使用CakePHP 2.3.6。在一個項目中,我必須爲多種用戶實現User Management System
(Users, Admins, etc.
)。目前,我只關注Admin Panel(only 1 Admin for now)
和User Panel
。我希望Admin
可以像平常一樣訪問所有區域(包括User Panel
的所有頁面),並且Users
必須login
才能訪問某些區域(頁面)。CakePHP 2.x中的用戶管理系統表現奇怪
我創建2 controllers
爲Admin Panel
和User 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
,但它讓我瘋狂。
任何人都可以幫助我嗎?
感謝
謝謝,看起來它會解決我的問題。其實,我也嘗試過使用allow(),但結果相同。而且,isAuthorized()不會在任何地方使用,根據CakePHP文檔中的示例,我只是將它保留在此處。那麼,我會再次嘗試刪除它。@ user221931 – 2014-09-27 12:28:49
而且,我應該使用這個:$ this-> Auth-> authorize('Controller')? – 2014-09-27 12:32:37
工作更好,但仍然表現奇怪。我試圖在「管理」面板和「用戶」面板中使用相同的瀏覽器,相同的選項卡逐個登錄。那是問題嗎 ?有沒有'餅乾'的問題? @ user221931 – 2014-09-27 12:42:07