CakePHP 3“登录身份"具有多个身份验证会话 [英] CakePHP 3 "Login As" wtih Multiple Auth Sessions

查看:27
本文介绍了CakePHP 3“登录身份"具有多个身份验证会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用前缀,我对管理员和用户有不同的会话和登录.例如 AppController.php 有:

Using prefixes, I have separate sessions and logins for admins versus users. For example the AppController.php has:

    if ($this->request->prefix == 'admin') {

        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'userModel' => 'Admins',
                    'fields' => ['username' => 'email', 'password' => 'password']
                ],
            ],
            'loginAction' => [
                'controller' => 'Admins',
                'action' => 'login'
            ],
            'loginRedirect' => [
                'controller' => 'Admins',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Admins',
                'action' => 'login',
            ],
            'storage' => [
                'className' => 'Session',
                'key' => 'Auth.Admin',              
            ],
        ]);

    } else {

        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'userModel' => 'Users',
                    'fields' => ['username' => 'email', 'password' => 'password']
                ],
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ],
            'loginRedirect' => [
                'controller' => 'pages',
                'action' => 'home'
            ],
            'logoutRedirect' => [
                'controller' => 'Users',
                'action' => 'login',
            ],
            'storage' => [
                'className' => 'Session',
                'key' => 'Auth.User',               
            ],
        ]);

    }

这是正常的,访问 example.com/admin 的用户被重定向到管理员登录区域,访问 example.com 的用户被重定向到用户登录区域,并且用户可以登录到另一个,或两者同时进行,互不干扰.

This is working fine in that users who visit example.com/admin get redirected to the admin login area, users who visit example.com get redirect to the user login area, and users can be logged into one, the other, or both simultaneously without interfering with each other.

当我希望管理员能够以其他用户的身份登录"时,问题就出现了.在 CakePHP2 中,我能够做到这一点:

The problem comes when I want admins to be able to "login as" another user. In CakePHP2 I was able to do this:

    AuthComponent::$sessionKey = 'Auth.User'; // solution from http://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session
    $this->Auth->loginAction = array('admin'=>false,'controller'=>'accounts','action'=>'login');
    $this->Auth->loginRedirect = array('admin'=>false,'controller'=>'pages','action'=>'home');
    $this->Auth->logoutRedirect = array('admin'=>false,'controller'=>'accounts','action'=>'login');
    $this->Auth->authenticate = array(
        'Custom' => array(
            'userModel' => 'Account',
            'fields' => array('username' => 'number'),
        )
    );
    if (!$this->Auth->login($account['Account'])) {
        throw new NotFoundException(__('Could not login to account'));
    }

    return $this->redirect(array('admin' => false, 'controller' => 'getting_started', 'action' => 'index'));

一切正常.但是在 CakePHP3 中 AuthComponent::$sessionKey 属性似乎不可访问,相反,我认为我应该使用 $this->Auth->config.但是当我使用这段代码时:

And everything worked fine. But in CakePHP3 the AuthComponent::$sessionKey property doesn't appear to be accessible, instead I think I'm meant to use $this->Auth->config. But when I use this code:

public function loginas($id = null)
{

    $user = $this->Users->get($id, [
        'contain' => []
    ]);

    $this->Auth->config([
        'authenticate' => [
            'Form' => [
                'userModel' => 'Users',
                'fields' => ['username' => 'email', 'password' => 'password']
            ],
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'loginRedirect' => [
            'controller' => 'pages',
            'action' => 'home'
        ],
        'logoutRedirect' => [
            'controller' => 'Users',
            'action' => 'login',
        ],
        'storage' => [
            'className' => 'Session',
            'key' => 'Auth.User',               
        ],
    ]);

    $this->Auth->setUser($user->toArray());
    return $this->redirect([
        'prefix' => false,
        'controller' => 'pages',
        'action' => 'home',
    ]);     
}

我可以成功地登录为",但它也会用普通用户详细信息覆盖现有管理会话的用户信息.

I can successfully "login as", however it ALSO overwrites the user information for the existing admin session with the normal user details.

如何让 CakePHP 3 完全不使用 Auth.Admin 会话,并针对 Auth.User 会话密钥(碰巧在新选项卡中打开)设置新的 Auth 会话?

How can I get CakePHP 3 to leave the Auth.Admin session completely alone, and set up a new Auth session against the Auth.User session key (which happens to open in a new tab)?

推荐答案

好吧我想我已经想通了,我需要使用 $this->Auth->__set('sessionKey', 'Auth.User');在调用 $this->Auth->config() 之前.

OK I think I have this figured out, I needed to use $this->Auth->__set('sessionKey', 'Auth.User'); before calling $this->Auth->config().

public function loginas($id = null)
{

    $user = $this->Users->get($id, [
        'contain' => []
    ]);

    $this->Auth->__set('sessionKey', 'Auth.User');

    $this->Auth->config([
        'authenticate' => [
            'Form' => [
                'userModel' => 'Users',
                'fields' => ['username' => 'email', 'password' => 'password']
            ],
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'loginRedirect' => [
            'controller' => 'Pages',
            'action' => 'home'
        ],
        'logoutRedirect' => [
            'controller' => 'Users',
            'action' => 'login',
        ],
        'storage' => [
            'className' => 'Session',
            'key' => 'Auth.User',               
        ],
    ]);

    $this->Auth->setUser($user->toArray());
    return $this->redirect([
        'prefix' => false,
        'controller' => 'Pages',
        'action' => 'home',
    ]);     

这篇关于CakePHP 3“登录身份"具有多个身份验证会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆