簡體   English   中英

在 Yii2 Advanced 中使用多用戶身份 Class 登錄后面臨身份 object 的問題

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

我在 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,
            ],
        ],

當我使用特許經營登錄時,登錄后如果我檢查Yii::$app->user->identity它會為我提供數據庫中第一條記錄的詳細信息(反之亦然用於用戶登錄)。 當我以特許經營權登錄時,我想為Yii::$app->user->identity獲取 null。

您 select 用戶的第一個組件,請檢查:

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

但是,為此目的最好的解決方案是使用為用戶單獨配置的高級模板。

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

或者您可以在運行時使用模塊並更改配置,在 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,
            ],
        ],
    ]);
}

並對另一個用戶模塊重復此操作。

當我們在配置中添加多個身份時,請更改其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