2011-08-22 102 views
0

我已禁用autoRedirect,所以我可以在我的用户控制器的登录方法中做一些额外的爵士乐,并使用会话将它们发回他们来自的地方。CakePHP登录方法读会话如果存在重定向

class UsersController extends AppController 
{ 
    var $name = 'Users'; 

    var $components = array('Session'); 

    function beforeFilter() 
    { 
     parent::beforeFilter(); 

     $this->Auth->allow(array('login','logout','admin_logout','admin_login')); 

     $this->Session->write('back_to', $this->referer()); 
    } 

    /** 
    * Log in 
    */ 

    function admin_login() 
    { 
     $this->set('title_for_layout', 'Log in – Admin —'); 

     if(!(empty($this->data)) && $this->Auth->user()) 
     { 

      $back_to = $this->Session->read('back_to'); 

      if($back_to) 
      { 
       $this->redirect($back_to, null, true); 
      } 
      else 
      { 
       $this->redirect($this->Auth->redirect(), null, true); 
      } 
     } 

     if($this->Auth->user()) 
     { 
      $this->redirect($this->Auth->redirect(), null, true); 
     } 

    } 

这样的想法是,如果一个用户会话(时间99%),然后提交表单的它会向用户发送到前一页,如果没有则发送到默认loginRedirect。

注意:通过将autoRedirect设置为false,CakePHP不再使用Auth.Redirect会话!所以存储在那里的值不再被应用程序使用,并且是有意的!

我遇到的问题是我的应用总是将用户发送到仪表板,因为上面的代码中'This one'注释下面的功能。如果我删除了那个函数,那么用户只是一直发送回登录表单,但他们会被登录!

任何人都可以帮忙吗?

感谢

更新:这里是我的AppController代码:

class AppController extends Controller 
{ 
    var $components = array('Auth','Session'); 

     public function beforeFilter() 
     { 

      parent::beforeFilter(); 

      $this->Auth->authorize = 'controller'; 

      $this->Auth->autoRedirect = false; 

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

      $this->Auth->loginRedirect = array('admin'=>true,'controller' => 'dashboard', 'action' => 'index'); 

      $this->Auth->logoutRedirect = array('admin'=>false,'controller' => 'pages', 'action' => 'display','home'); 
     } 

    function isAuthorized() 
    { 
     return true; 
    } 
} 
+0

根据这一页:http://book.cakephp.org/view/1270/loginRedirect你应该使用默认的重定向机制来做你想做的事情。除非你想要重定向,即使最后一次不需要登录... –

回答

1

您在重定向后不存在。试着改变你的重定向签名:

$this->redirect($back_to, null, true); 

的第二个参数是一个状态码,三是是否停止处理当前的行动。这应该防止你下降到最后的重定向,我猜是正在执行的。

鉴于我们长期的评论 “讨论” 下面,请尝试调整您的admin_login()方法是这样的:

if(!(empty($this->data)) && $this->Auth->user()) { 
    $back_to = $this->Session->read('back_to'); 

    if($back_to) { 
    $this->redirect($back_to, null, true); 
    } 
    else { 
    $this->redirect($this->Auth->redirect(), null, true); 
    } 
} 
else { 
    # Only write back_to when you arrive at the login page without data 
    $this->Session->write('back_to', $this->referer()); 
} 
+0

虽然用户仍未被发送到正确的位置。它们要么被发送到仪表板,要么回到登录表单,而不是存储在会话“back_to”中的位置。 – Cameron

+0

有没有更新?谢谢 – Cameron

+0

我的第一个问题是你确定'back_to'在会话中?没有太多细节,但是如果登录成功的话,代码似乎会落入你的'else'条件。 –

0
function login(){ 
    if(!empty($this->data) && !$this->Auth->user()){ 
    $this->Session->write('back_to', $this->referer()); 
    } 

删除您的beforeFilter该会话级>写。并且您不需要$this->Auth->allow(array('login','logout'));

+0

仍然无法正常工作,并仍然将用户发送到仪表板。此外,会话永远不会更新新的值,直到表单提交,因此它在beforefilter。 – Cameron

0
if(!(empty($this->data)) && $this->Auth->user()) 
    { 
     $back_to = $this->Session->read('back_to'); 

     $auth_redirect = $this->Session->read('Auth.redirect'); 

     if($auth_redirect) 
     { 
      $this->redirect($auth_redirect, null, true); 
     } 
     else if($back_to) 
     { 
      $this->redirect($back_to, null, true); 
     } 
     else 
     { 
      $this->redirect($this->Auth->redirect(), null, true); 
     } 
    } 
    else 
    { 
     $this->Session->write('back_to', $this->referer()); 
    }