2014-11-04 54 views
0

我需要在這裏編輯loginredirect會話參數,以便它將重定向到不同的頁面作爲管理員或用戶......事情是。索引硬編碼,它將始終重定向到正常的用戶索引視圖忽略我的代碼重定向管理員用戶。我希望它這樣,如果當前用戶是管理員,它會重定向到admin_index代替cakephp auth loginRedirect

appControler

public $components = array(
     'Session', 
     'Auth' => array(
      'loginRedirect' => array(
       'controller' => 'users', 
       'action' => 'index' 
      ), 
      'logoutRedirect' => array(
       'controller' => 'users', 
       'action' => 'login' 
       ), 
      'authenticate' => array(
       'Form' => array(
        'passwordHasher' => 'Blowfish' 
       ) 
      ) 
     ) 

    ); 

用戶控制器登錄功能

public function login() { 
    if($this->Auth->user('account_type')=='admin'){ 
     return $this->Auth->loginRedirect = array('controller' => 'users', 
     'action' => 'admin_index'); 
    } 
    elseif($this->Auth->user('account_type')=='user'){ 
     return $this->Auth->loginRedirect = array('controller' => 'users', 
     'action' => 'view'); 
    } 
    else { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
    } 
} 
+2

首先檢查用戶登錄,如果($這個 - > Auth->登錄()){添加代碼的REST} – Salines 2014-11-04 09:52:43

+0

說行......現在我所有的登錄始終重定向到登錄頁面,但是登錄在頁面頂部的鏈接現在註銷*表示我的用戶確實已登錄* – 2014-11-04 09:59:08

回答

0

改變返回$這個 - >重定向( $這 - > Auth->重定向());到

return $this->redirect($this->Auth->redirectUrl()); 

你的問題是,你返回管理員的網址,但從來沒有真正重定向。

或者你可以在一個字符串或數組形式

+0

試過了。仍然沒有變化。我希望使用loginRedirect取決於用戶登錄的類型 – 2014-11-04 10:54:44

+0

好吧,用redirectUrl多玩了一下,最後確實設法讓它正確重定向: ergo return $ this-> redirect(數組('controller'=>'users','action'=>'admin_index')); 成爲返回$ this-> redirect($ this-> redirectUrl(array('controller'=>'users','action'=>'admin_index'))); – 2014-11-04 15:13:22

1

從CakePHP的文件,大約$ loginRedirect添加所需的URL被$ this-> Auth->的redirectUrl(): http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent :: $ loginRedirect

如果用戶在會話中具有Auth.redirect值,則該值將被忽略。

所以你的$this->Auth->loginRedirect被忽略。您可以將此邏輯移動到您的beforeFilter()回調並在那裏設置$this->Auth->loginRedirect,或者您可以手動重定向用戶。

public function login() { 
    if($this->Auth->user('account_type')=='admin'){ 
     return $this->redirect(array('controller' => 'users', 'action' => 'admin_index')); 
    } 
    elseif($this->Auth->user('account_type')=='user'){ 
     return $this->redirect(array('controller' => 'users', 'action' => 'view')); 
    } 
    else { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
    } 
} 
+0

注意。以及rrd的答案。謝謝你的幫助先生 – 2014-11-04 15:14:48

0

我得到相同的問題,如果錯誤的身份驗證它重定向到錯誤的URL。所以我嘗試下面的代碼登錄功能,它爲我工作。

$user = $this->Users->newEntity(); 
    $this->set('user', $user); 

    if ($this->request->is('post')) { 
     $user = $this->Auth->identify(); 
     if ($user){ 
      $this->Auth->setUser($user); 
      return $this->redirect($this->Auth->redirectUrl()); 
     }else{ 
      $this->Flash->error(__('Invalid username or password, try again')); 
     } 
    }