简体   繁体   English

在 Yii2 Advanced 中使用多用户身份 Class 登录后面临身份 object 的问题

[英]Facing issue with identity object after login while using Multiple User Identity Class in Yii2 Advanced

I have setup/created 2 user identity classes for 2 different login under config/main.php components:我在 config/main.php 组件下为 2 个不同的登录设置/创建了 2 个用户身份类:

'user' => [
            'class'=>'yii\web\User',
            'identityClass' => 'frontend\models\CustomerUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['customer/login'],
            'identityCookie' => [
                'name' => '_panelCustomer',
                'httpOnly' => true,
            ],
        ],
        'franchise'=>[
            'class'=>'yii\web\Franchise',
            'identityClass' => 'frontend\models\FranchiseUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['franchise/login'],
            'identityCookie' => [
                'name' => '_panelFranchise',
                'httpOnly' => true,
            ],
        ],

When i logged in using franchise, after login if i check Yii::$app->user->identity it gives me details for 1st record in database (vice versa for user login).当我使用特许经营登录时,登录后如果我检查Yii::$app->user->identity它会为我提供数据库中第一条记录的详细信息(反之亦然用于用户登录)。 I want to get null for Yii::$app->user->identity when i logged in as franchise.当我以特许经营权登录时,我想为Yii::$app->user->identity获取 null。

You select 1'st components of the user, Check with this:您 select 用户的第一个组件,请检查:

$user = Yii::$app->get('franchise');
$user->identity

But, The best solution for this purpose using an advanced template with a separated configuration for users.但是,为此目的最好的解决方案是使用为用户单独配置的高级模板。

https://github.com/yiisoft/yii2-app-advanced

Or you can use module and change configuration in runtime, Inside of Module.php:或者您可以在运行时使用模块并更改配置,在 Module.php 内部:

public function init() {
    parent::init();
    Yii::$app->setComponents([
        'user' => [
            'class'=>'yii\web\Franchise',
            'identityClass' => 'frontend\models\FranchiseUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['franchise/login'],
            'identityCookie' => [
                'name' => '_panelFranchise',
                'httpOnly' => true,
            ],
        ],
    ]);
}

And repeat this for another user module.并对另一个用户模块重复此操作。

When we add multiple identity into configuration, please change its idParam parameter.当我们在配置中添加多个身份时,请更改其idParam参数。

'user' => [
            'class'=>'yii\web\User',
            'identityClass' => 'frontend\models\CustomerUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['customer/login'],
            'idParam' => '__cid',
            'identityCookie' => [
                'name' => '_panelCustomer',
                'httpOnly' => true,
            ],
        ],
        'franchise' => [
            'class'=>'yii\web\User',
            'identityClass' => 'frontend\models\FranchiseUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['franchise/login'],
            'idParam' => '__fid',
            'identityCookie' => [
                'name' => '_panelFranchise',
                'httpOnly' => true,
            ],
        ],

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

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