配置Yii2中的多个用户标识

roo*_*lka 3 yii2 yii2-rbac

我开发了一个有两个不同注册的站点,我有2个不同的表,我使用RbacDB,并在组件部分的web配置中我有用户配置,根据这个我想知道我怎么可以使用2个不同的字段配置文件?

配置:

'components' => [
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => '213h2i3121h12osiajls',
    ],
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
    'user' => [
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => true,
    ],
    // Here after user I need to add another config user-two
    'user-two' => [
        'identityClass' => 'app\models\SecondUser',
        'enableAutoLogin' => true,
    ],
Run Code Online (Sandbox Code Playgroud)

当我这样做时,显示此错误 在此处输入图像描述

谢谢!

And*_* Bu 7

尝试在user-two组件中设置class属性:

'user-two' => [
    'class' => 'yii\web\User'
    'identityClass' => 'app\models\SecondUser',
    'enableAutoLogin' => true,
],
Run Code Online (Sandbox Code Playgroud)

或者创建从yii\web\User类继承的新类,并设置如下:

'user-two' => [
    'class' => 'app\models\NewClassInheritedFromUserClass'
    ....
]
Run Code Online (Sandbox Code Playgroud)

也许这会对你有所帮助.

  • 更好的方法是创建从yii\web\User类继承的新类.在这个类中,你应该覆盖`public $ identityCookie = ['name'=>'_ user_two_identity','httpOnly'=> true];`,因为用户和用户2应该有不同的cookie用于授权.输入登录名和密码后查看cookie.你看到_user_two_identity cookie了吗?顺便说一下,你的代码中的某个地方(我认为在LoginForm中)你应该调用`\ Yii :: $ app-> user-two-> login($ this-> getUser(),$ this-> rememberMe?3600*24*30:0);`(不是Yii :: $ app-> user-> login(...)) (2认同)