如果存在重定向,则CakePHP登录方法读取会话

时间:2011-08-22 21:13:13

标签: php cakephp

我已禁用autoRedirect,因此我可以在用户控制器的登录方法中执行一些额外的爵士乐,并使用会话将其发送回他们来自的位置。

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;
    }
}

3 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分:0)

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

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

答案 2 :(得分: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());
    }