配置 Yii2 中的多个用户身份

multiple user identity in config Yii2

我开发了一个有两个不同注册的站点,我有 2 个不同的 table,我正在使用 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,
    ],

当我这样做时,显示此错误 enter image description here

谢谢!

您必须为第二个身份创建一个网络用户class

namespace app\components;

class UserTwo extends \yii\web\User{
}

而不是在配置中指定 class 名称

'user-two' => [
        'class'=> 'app\components\UserTwo'
        'identityClass' => 'app\models\SecondUser',
        'enableAutoLogin' => true,
    ],

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

'user-two' => [
    'class' => 'yii\web\User'
    'identityClass' => 'app\models\SecondUser',
    'enableAutoLogin' => true,
],

或创建新的 class 继承自 yii\web\User class 并设置如下:

'user-two' => [
    'class' => 'app\models\NewClassInheritedFromUserClass'
    ....
]

也许这会对你有所帮助。

我已经了解了 yii2 框架的内部结构。据我了解,您可以按照以下技术制作 N 个身份;


  • 以上解决方案仅是部分答案和一些有用的建议。请按照我下面的深度更改,您可以根据需要创建 N 个身份。
  • 当您不想实施复杂的 RBAC(基于角色的访问控制)并且只想根据控制器的请求过滤访问时,N 身份非常有用。

  • 让我们假设我必须创建另一个名为 'Franchise' 的身份,而不是在 Yii2 框架内很好地耦合的现有用户。


数据库迁移

  1. 使用命令创建新的迁移文件

    yii migrate/create create_franchise
    
  2. 将已经可用的迁移文件的内容复制粘贴到位置 PROJECT_NAME\console\migrations 类似 'm170311_105858_create_user.php' 并将 table 名称从 'user' 重命名为 'franchise'.

  3. 现在,运行迁移命令

    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)
      
  4. 检查数据库是否创建了数据库。 (我假设您已经在 PROJECT_NAME\common\config\main-local.php 中进行了 DB 设置)

  5. 请注意,无论身份 class 是什么,现在都应使用 'Franchise' Table.

创建特许经营模式

  1. 只需转到 'Gii' 模块并为新创建的特许经营权创建模型 table。

  2. 模型位置必须是PROJECT_NAME\common\models\Franchise.php

  3. 确保模型 class 实现了 IdentityInterface 并且还实现了 IdentityInterface[ 的强制方法=16=]


身份Class

  1. 如果你去位置PROJECT_NAME\vendor\yiisoft\yii2\web\User。php。这是 class,在你的项目中到处都被引用为 Yii::$app->user。复制粘贴此 class 的内容并创建一个名为 PROJECT_NAME\vendor\yiisoft\yii2\web\Franchise.php 的新文件并将内容粘贴到其中。在文件中进行以下更改。

    • 找到'user'并将其替换为'franchise'。
    • 找到'User'并将其替换为'Franchise'。
    • 找到$loginUrl = ['site/login'];替换为$loginUrl = ['franchise/login']; 因为您将有不同的控制器来处理与特许经营相关的操作。
    • 找到 $identityCookie = ['name' => '_identity', 'httpOnly' => true]; 并替换 ' name' as '_fidentity' (可以看出区别,identity cookie 必须是唯一的)
    • 找到$authTimeoutParam = '__expire';替换为$authTimeoutParam = '_f_expire';

PROJECT_NAME\vendor\yiisoft\yii2\web\Application.php

  1. Application.php中添加下面的方法,

    public function getFranchise()
    {
        return $this->get('franchise');
    }
    
  2. 也找到方法coreComponents(),再添加一项如下,

    'Franchise' => ['class' => 'yii\web\Franchise'],
    

PROJECT_NAME\frontend\config\main.php

  1. components 中,在 'user' 条目之后添加以下条目,

     'franchise' => [
            'identityClass' => 'common\models\Franchise',
            'enableAutoLogin' => true,
            'class' => 'yii\web\Franchise',
            'identityCookie' => ['name' => '_fidentity-frontend', 'httpOnly' => true],
        ],