2012-04-09 15 views
1

Ich habe zwei Arten von Benutzern und jetzt möchte ich nicht ACL verwenden. Mit Auth Komponente möchte ich die folgenden erreichenCakePHP Admin Login

login() -> ermöglicht Benutzern die Anmeldung und den Zugriff auf den allgemeinen Teil der Website admin_login -> ermöglicht dem Administrator den Zugriff auf die Admin_ {Aktionen} Teil der Website.

Wenn ich ein Admin Login tun - wenn group_id überprüfen = 1 im Modul Benutzer> ich will und nur damit sie an dem Admin-Bereich der Website einzuloggen.

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

Wie überprüft man, ob die group_id = 1 ist, wenn sich der Benutzer anmeldet?

Antwort

4

Ich würde so etwas tun:

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