2012-04-09 167 views
1

我有兩種類型的用戶,現在我不想使用ACL。使用Auth組件我希望實現以下功能CakePHP Admin登錄

login() - >允許用戶登錄和訪問網站的一般部分 admin_login - >允許管理員訪問網站的admin_ {actions}部分。

當我做一個管理員登錄 - >我想檢查用戶模塊中是否group_id = 1,只允許他們登錄到網站的管理部分。

function admin_login(){ 
    $this->layout = 'admin_login'; 

    if($this->request->is('post')) { 
     if($this->Auth->login()) { 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Invalid username and password, try again')); 
     } 
    } 
} 

如何在用戶登錄時檢查group_id = 1?

回答

4

我會做這樣的事情:

function admin_login(){ 
    $this->layout = 'admin_login'; 

    if($this->request->is('post')) { 
     if($this->Auth->login()) { 
      // If here because user is logged in 
      // Check to see if group_id is 1 
      if($this->Auth->user('group_id') == 1){ 
       //$this->redirect($this->Auth->redirect()); 
       $this->redirect('/admin/dashboards'); //Example 
      }else{ 
       // In case a user tries to login thru admin_login 
       // You should log them in anyway and send them to where they belond 
       $this->redirect('/users/account'); 
      } 
     } else { 
      $this->Session->setFlash(__('Invalid username and password, try again')); 
     } 
    } 
}