2015-02-07 52 views
1

應用程序控制器不同的登錄重定向:如何需要不同的角色

class AppController extends Controller { 

    public $components= array(
    'Session', 
    'Auth' => array(
    'loginRedirect' =>array('controller' => 'Item' , 'action' => 'index'), 
    'logoutRedirect' =>array('controller' => 'Item' , 'action' => 'index'), 
    'authError' => 'Login Error', 
    'authorize' => array('Controller') 
    ) 
    ); 

    public function isAuthorized($user) 
    { 
     return true; 
    } 
    public function beforeFilter(){ 
     $this->Auth->allow('login'); 

    } 
} 

用戶控制器:

class UsersController extends AppController{ 
    public $name= 'Users'; 
    public function login(){ 
     if($this->request->is('post')){ 
      if($this->Auth->login()){ 
       $this->redirect($this->Auth->Redirect()); 
      } 
      else{ 

       $this->Session->setFlash('error'); 
      } 
     } 

    } 

    public function logout(){ 
     $this->redirect($this->Auth->logout()); 
    } 

    public function index(){ 
     $this->User->recursive=0; 
     $this->set('users',$this->User->find('all')); 

    } 
} 

存在用戶表管理和客戶兩種類型的角色, 如果管理員登錄在loginredirect中是'loginRedirect' =>array('controller' => 'Item' , 'action' => 'index')
如果客戶登錄'loginRedirect' =>array('controller' => 'customer' , 'action' => 'view') .

用戶登錄取決於角色。

如何基於角色給loginredirect

回答

2

嘗試在UsersController使用此:

public function beforeFilter() { 

parent::beforeFilter(); 

    if($this->Acl->check('role','A1')){ 

     $this->Auth->logoutRedirect = array(
     'controller' => 'customer', 
     'action' => 'view' 
     ); 

    }elseif($this->Acl->check('role','A2')){ 
     $this->Auth->logoutRedirect = array(
     'controller' => 'users', 
     'action' => 'login' 
     ); 
    } 


} 
+0

錯誤:調用一個成員函數檢查()非對象。你能寫完整的代碼爲AppController和UserController。 – 2015-02-07 12:09:51

+0

你如何指定你的角色? – Amed 2015-02-07 19:39:37

+0

角色正在從角色列中取出用戶表。 – 2015-02-09 04:53:07

相關問題