2013-04-08 51 views
0

我做了一個簡單的cakephp應用程序。目前我只是使用授權組件 來根據用戶發送用戶到他們各自的頁面。例如,如果角色= 1發送到管理頁面,否則如果角色= 2發送到主持人頁面。我使用會話和身份驗證組件來查看它們如何工作並將數據保存在其中。下面是UserController的login動作Cakephp Auth-> loginredirect問題

public function login(){ 

$this->Session->setFlash($this->Auth->user('role'));//checks for data in auth component if any 



    if($this->request->is('post')){ 


     $results = $this->User->findByEmail($this->request->data['User']['username']); 
     if($results &&$results['User']['password']== md5($this->request->data['User']['password'])) 
     { 
      $this->Session->write('user',$results['User']); 
      $this->Auth->login($results['User']); 
      $this->Session->setFlash('User logged in successfully'.$this->Auth->user('role')); 
      return $this->redirect($this->Auth->redirect()); 
     } 
     else 
     { 
      $this->Session->setFlash('Login is incorrect'); 
     } 
    } 


} 

問題的代碼登錄工精細的所有數據都存儲在會話,並權威性變量,但loginredirect行爲怪異。在我的Chrome瀏覽器中。無論角色是什麼,它總是重定向到管理頁面,但它閃爍着我在閃存中設置的正確信息。 beforefilter在AppController的

public function beforeFilter(){ 

    $this->Auth->allow('display'); 

    $this->Auth->loginAction = array('controller' => 'Users', 'action' => 'login'); 

    $this->Auth->logoutRedirect = array('controller' => 'Users', 'action' => 'login'); 

    if($this->Auth->user('role') == '1'){ 
      $this->Session->setFlash($this->Auth->user('role').'adminnnnnnnnnnnnnnnnnnnnn'); 
    $this->Auth->loginRedirect = '/admins/index'; 
    } 

    if($this->Auth->user('role') == '2'){ 
     $this->Session->setFlash('moderatorrrrrrrrrrrrrrrrr'); 
     $this->Auth->loginRedirect = '/users/index'; 
    } 
} 

的代碼,所以這個問題是在循環之前過濾器運行正常,在setflash顯示用戶是否是管理員或版主,但由於某種原因它重定向到只有一個頁面要麼管理員/索引頁或用戶/索引頁面,無論誰登錄。這是Chrome瀏覽器的行爲。 在Firefox上,loginredirectcts將用戶發送到webroot/index頁面,但是再次顯示flash消息是正確的。

我不確定我在做什麼錯在我的代碼或cakephp 2.0驗證組件中有一個問題驗證組件有措施錯誤。

+0

暫時我使用loginredirect到一個稱爲儀表板的功能,負責路由 所以在用戶登錄後,它通過Auth-> loginRedirect重定向到儀表板(),在這裏我檢查用戶角色並使用重定向將特定用戶發送到確切位置。 我不確定是否正確的做法,但它的工作原理。任何人都可以告訴這個解決方案是否足夠安全,因爲我沒有使用auth重定向? 由於 – 2013-04-08 07:27:00

+0

這是代碼 功能的儀表板(){ \t // $角色= $這 - >會話級>讀('用戶。角色'); \t \t $ role = $ this-> Auth-> user('role'); \t \t \t這裏 \t \t //用戶選擇邏輯如果($角色== '1'){ \t \t \t $這 - >重定向(陣列( '控制器'=> '管理員', '動作'=> 'index','admin'=> true)); \t \t \t \t \t } \t \t 其他\t如果($角色== '2'){ \t \t \t $這 - >重定向(陣列( '控制器'=> '用戶', '動作'=> 'index','admin'=> true)); \t \t \t \t } \t \t 其他\t如果($角色== '9'){ \t \t \t $這 - >重定向(陣列( '控制器'=> '用戶', '動作'=>' index','admin'=> true)); \t \t \t $ this-> Session-> setFlash('3'); \t \t} } – 2013-04-08 07:27:52

回答

1

用戶登錄時它就會通過Auth-> loginRedirect重定向到儀表板(),在這裏我檢查用戶角色並使用重定向到特定的用戶發送到確切位置

function dashboard() { 
    //get user's group (role) 
    //$role = $this->Session->read('user.role'); 
    $role=$this->Auth->user('role'); 
     //user selection logic here 
    if($role== '1'){ 
     $this->redirect(array('controller' => 'users','action' => 'admin_index','admin' => false)); 

    } 
    else if($role == '2'){ 
     $this->redirect(array('controller' => 'users','action' => 'admin_index', 'admin' => false)); 

    } 
    else if($role == '9'){ 
     $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => false)); 
     $this->Session->setFlash('3'); 
    } 

    } 

這之後就是另一種方式處理事情我包括在我的用戶控制器的儀表板功能,並沒有auth登錄重定向到這個功能從appcontroller。 希望它解決了面臨問題的其他人的問題。謝謝