2014-09-27 103 views
0

我正在使用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,但它讓我瘋狂。

任何人都可以幫助我嗎?

感謝

回答

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()?它是從某個地方叫來的嗎?

+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