配置Yii2中的多用户标识
我开发了一个具有两个不同注册的站点,我有两个不同的表,Im使用RbacDB,在components部分的web配置中,我有用户配置,根据这一点,我想知道如何使用配置文件中的两个不同字段 配置:配置Yii2中的多用户标识,yii2,yii2-rbac,Yii2,Yii2 Rbac,我开发了一个具有两个不同注册的站点,我有两个不同的表,Im使用RbacDB,在components部分的web配置中,我有用户配置,根据这一点,我想知道如何使用配置文件中的两个不同字段 配置: 'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'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,
],
当我这样做时,会显示此错误
谢谢 您必须为第二个身份创建一个web用户类
namespace app\components;
class UserTwo extends \yii\web\User{
}
然后在配置中指定类名
'user-two' => [
'class'=> 'app\components\UserTwo'
'identityClass' => 'app\models\SecondUser',
'enableAutoLogin' => true,
],
尝试在user two组件中设置类属性:
'user-two' => [
'class' => 'yii\web\User'
'identityClass' => 'app\models\SecondUser',
'enableAutoLogin' => true,
],
或者创建从yii\web\User类继承的新类,并设置如下:
'user-two' => [
'class' => 'app\models\NewClassInheritedFromUserClass'
....
]
也许这会对您有所帮助。我已经介绍了yii2框架的内部结构。据我所知,您可以使用下面的技术制作N个标识
- 以上解决方案只是部分答案和一些有用的建议。请按照我下面的更改进行深入了解,您可以根据需要创建N个标识李>
- 当您不想实现复杂的RBAC(基于角色的访问控制)而只想根据控制器的请求过滤访问时,N标识非常有用
- 让我们假设我必须创建另一个名为“特许经营”的身份,而不是现有的用户,该用户在Yii2框架中很好地耦合
数据库迁移
yii migrate/create create_franchise
yii/migrate
- 您必须在命令提示符下获得类似的内容
Apply the above migrations? (yes|no) [no]:yes applying m170311_105950_create_franchise create table {{%franchise}} ... done (time: 1.304s) applied m170311_105950_create_franchise (time: 1.350s)
创建特许经营模式
身份类别
- 找到“用户”,并将其替换为“特许经营””
- 找到“用户”,并将其替换为“特许经营””
- 查找$loginUrl=['site/login']并将其替换为$loginUrl=['特许经营/登录']因为您将有不同的控制器来处理特许经营相关的操作
- 查找$identityCookie=['name'=>'\u identity','httpOnly'=>true]并将“名称”替换为“\u-fidenty”(您可以看到区别,标识cookie必须是唯一的)
- 查找$authTimeoutParam='\uuu expire'并将其替换为$authTimeoutParam=''u f_expire'强>
PROJECT\u NAME\vendor\yiisoft\yii2\web\Application.php
public function getFranchise()
{
return $this->get('franchise');
}
'Franchise' => ['class' => 'yii\web\Franchise'],
PROJECT\u NAME\frontend\config\main.php
'franchise' => [
'identityClass' => 'common\models\Franchise',
'enableAutoLogin' => true,
'class' => 'yii\web\Franchise',
'identityCookie' => ['name' => '_fidentity-frontend', 'httpOnly' => true],
],
我尝试了两种变体,错误消失了,Tnx!但是用户仍然没有登录。更好的方法是创建从yii\web\user类继承的新类。在这个类中,您应该重写
public$identityCookie=['name'=>'\u user\u two\u identity','httpOnly'=>true]代码>,因为用户和用户两个应该有不同的cookie进行授权。输入登录名和密码后,查看cookies。您是否看到_user_two_标识cookie?顺便说一句,在代码的某个地方(我认为是在LoginForm中),您应该调用\Yii::$app->user two->login($this->getUser(),$this->rememberMe?3600*24*30:0)代码>(不是Yii::$app->user->login(…)有人能提供一个使用两个身份的代码的具体示例吗?我正在尝试这个,但我似乎无法解决它。第二个yii\web\User
子类似乎可以工作,但当我尝试为AdminController操作创建用户访问规则时,它失败了,它不理解用户已经登录,而是使用了另一个标识类而不是默认的标识类。如果修改PROJECT\u NAME\vendor\yiisoft\yii2\web\Application.php,然后在下一次composer更新时
您将丢失此文件中所做的任何更改。