简体   繁体   English

CakePHP登录方法读取会话(如果存在)以进行重定向

[英]CakePHP login method read session if exists for redirect

I have disabled autoRedirect so I can do some extra jazz in the login method of my users controller and use a Session to send them back to where they came from. 我禁用了autoRedirect因此我可以在用户控制器的登录方法中做一些额外的爵士工作,并使用Session将其发送回它们的来源。

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

    }

So the idea is that if a user has the session (99.% of the time) then on submit of form it will send the user TO the previous page, if not then send to the default loginRedirect. 因此,想法是,如果用户拥有会话(99.%的时间),则在提交表单时它将把用户发送到上一页,如果没有,则发送到默认的loginRedirect。

NOTE: by setting autoRedirect to false, CakePHP no longer uses the Auth.Redirect session! 注意:通过将autoRedirect设置为false,CakePHP将不再使用Auth.Redirect会话! So the value stored there is not used by the app anymore and is intentional! 因此,存储在此处的值已不再被应用程序使用,这是故意的!

The problem I am having is that my app is ALWAYS sending the user to the dashboard because of the function below 'This one' comment in the code above. 我遇到的问题是,由于上面代码中“此人”注释下面的功能,我的应用程序始终会将用户发送到仪表板。 If I remove that function then the user is just sent BACK to the login form all the time BUT they will be logged in! 如果我删除该功能,那么用户将一直被返回到登录表单,但他们将一直登录!

Can anyone help? 有人可以帮忙吗?

Thanks 谢谢

UPDATE: here is my appcontroller code: 更新:这是我的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;
    }
}

You're not existing after redirection. 重定向后您不存在。 Try changing your redirection signature to: 尝试将重定向签名更改为:

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

The 2nd argument is a status code and the third is whether to stop processing the current action. 第二个参数是状态码,第三个参数是是否停止处理当前动作。 This should prevent you from dropping down to the last redirection which I'm guessing is the one being executed. 这应该可以防止您下降到我猜测是正在执行的最后一个重定向。

Given our long comment "discussion" below, try tweaking your admin_login() method like this: 鉴于下面我们的长长的注释“讨论”,请尝试调整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());
}
 function login (){ if(!empty($this->data) && !$this->Auth->user()){ $this->Session->write('back_to', $this->referer()); } 

remove that Session->write in your beforeFilter. 删除该Session-> write在beforeFilter中。 And you don't need $this->Auth->allow(array('login','logout')); 而且您不需要$this->Auth->allow(array('login','logout'));

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM