CakePHP 3“登录为"通过多个Auth会话 [英] CakePHP 3 "Login As" wtih Multiple Auth Sessions

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

问题描述

使用前缀,管理员和用户分别拥有不同的会话和登录名.例如,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的用户被重定向到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“登录为"通过多个Auth会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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