2014-11-04 107 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')); 
     } 
    }