2012-12-06 56 views
1

我正在使用cakePHP 2.2。CakePHP Auth-> loginRedirect不工作

在我的我的應用程序控制器的前過濾器,我有以下:

// Admin 
    if($this->Auth->user('group_id') == '12') { 
      $this->Auth->allow('admin_index'); 
      $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false); 
      $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE); 
      $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false); 

      $this->set("group", "admin"); 

    // Staff 
    } elseif($this->Auth->user('group_id') == '13') { 
      $this->Auth->allow('admin_index'); 
      $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false); 
      $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE); 
      $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false); 

      $this->set("group", "staff"); 

    // Users 
    } else { 

      $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false); 
      $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => false); 
      $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false); 

      $this->set("group", "user"); 
     } 

第一和第二,如果公司正常工作和用戶重定向到登錄相應頁面,但是第三重定向不起作用在所有情況下,它只是讓你登錄頁面,但它確實顯示登錄函數的flash_success消息?

任何人都可以指向正確的方向嗎?

在此先感謝

*蒂姆requrested添加信息*

過濾器和登錄功能之前,用戶控制器:

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('logout'); 
    $this->Auth->allow('login'); 
    $this->Auth->allow('forgetpwd'); 
    $this->Auth->allow('reset'); 
    $this->Auth->allow('initDB'); 
    $this->Auth->allow('register'); 
    //$this->Auth->allow('admin_importOldUsers'); 
    //$this->Auth->allow('*'); 
} 

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

     $userStatusCheck = $this->User->find('first',array('conditions'=>array('User.username'=>$this->request->data['User']['username']))); 

     // If user is not deleted 
     if ($userStatusCheck['User']['live'] == 1) { 

      // Users status. 
      /* 1 = Disabled, 2 = Enabled, 3 = Waiting for Auth, 4 = refer to Darren */ 
      $us = $userStatusCheck['User']['user_status_id']; 

      // If account is disabled 
      if ($us == 1) { 
       $this->Session->setFlash('Your account has been disabled, please conntact our support team.', 'flash_failure'); 
      } 

      // If account is waiting for authorisation 
      else if ($us == 3) { 
       $this->Session->setFlash('Your account is waiting to be authorised', 'flash_failure'); 
      } 

      // If accoutn is referred to Darren 
      else if ($us == 4) { 
       $this->Session->setFlash('Your account is waiting to be authorised', 'flash_failure'); 
      } 

      // Account is enabled, proceed to login 
      else if ($us == 2) { 

       if ($this->Auth->login()) { 
        if ($this->Session->read('Auth.User')) { 
         $this->Session->setFlash('You are logged in!', 'flash_success'); 
         //$this->redirect($this->Auth->redirect()); 

         if($this->Auth->user('group_id') == '12' OR $this->Auth->user('group_id') == '13'){ 
          $this->redirect('/admin/'); 
         } else { 
          $this->redirect($this->Auth->redirect()); 
         } 
        }    
       } 
       else { 
        $this->Session->setFlash('Your username or password was incorrect.', 'flash_failure'); 
       } 
      } 

     } else { 

      $this->Session->setFlash('Your account has been disabled, please conntact our support team.', 'flash_failure'); 

     } 

    } 
+0

只是一個延伸,但你沒有在頁面控制器上拒絕用戶的'isAuthorized()'覆蓋?重定向如何被調用?你可以在登錄函數中丟失諸如'$ this-> redirect($ this-> Auth-> redirect())'的調用嗎? – Grambot

+0

默認情況下不是''admin'=> false'?你需要那些額外的參數嗎? –

+0

讓我們在用戶控制器中看到您的登錄功能和beforeFilter –

回答

1

雖然我還沒有得到AppController beforefilter auth重定向代碼爲用戶組工作,我已經設法在日誌中對它進行欺騙在使用此代碼的usersController的()功能可按:

if($this->Auth->user('group_id') == '12' OR $this->Auth->user('group_id') == '13'){ 
$this->redirect('/admin/'); 
} else {  
    $this->redirect(array('controller' => 'pages', 'action' => 'index', 'admin' => false)); #this does not work... 
    //$this->redirect($this->Auth->redirect()); # Does not work 
} 

但關鍵來獲取別人的工作是該行

$this->Auth->allow(array('controller' => 'pages', 'action' => 'index', 'admin' => false)); 

到AppController中beforeFeature使其:

if($this->Auth->user('group_id') == '14') { 
     $this->Auth->allow(array('controller' => 'pages', 'action' => 'index', 'admin' => false)); 
     $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false); 
     $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => false); 
     $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false); 

     $this->set("group", "user"); 
    } 

這感覺就像是一個徹頭徹尾的騙局,但花了幾天閱讀SO和各種其他網站和蛋糕文檔後,我看不到其他方式使得auth組件在登錄時重定向到不同的頁面在用戶組上。

如果有人找到更好的方法來做到這一點,我很想知道!