代码之家  ›  专栏  ›  技术社区  ›  Addi

如何将Yeesoft/Yii2 cms RBAC(基于角色的访问控制)与Yii2前端RBAC集成?

  •  0
  • Addi  · 技术社区  · 8 年前

    我一直在使用yii2高级模板,现在我想实现 一些RBAC进入我的前端项目的控制器。

    我对Yeesoft的/Yii2 cms RBAC控制面板印象深刻 https://github.com/yeesoft/yii2-yee-cms 虽然我可能不会使用他们的很多内容管理功能。然而,我对它的控制面板印象深刻,我想用它来控制前端访问,给我的员工一定的权限。

    我已将此代码包含在其前端\配置\ main中。php的组件部分。

    'components' => [
    
        'authManager' => [
                'class' => 'yii\rbac\DbManager'
        ],
    ]
    

    这使我能够在前端控制器中包含如下代码

    if (!\Yii::$app->user->can('createEmployee')) {
            throw new \yii\web\ForbiddenHttpException('You do not have permission to create an employee.');
        }  
    

    控制访问。

    我正在使用yeesoft的数据库,并正在考虑将我的所有数据从前端数据库迁移到yeesoft的cms数据库,因为我可以使用控制面板在其下创建权限并访问权限数据,而无需使用

    Yii::$app->authManager;
    

    以及其他类似以下的复杂代码:

    $auth = Yii::$app->authManager;
        //create the permission
        $manageCleansbutnotusers = $auth->createPermission('manageCleansbutnotusers');
        $manageCleansbutnotusers->description = 'Manage Cleans but not Users';
        //add the permission 
        $auth->add($manageCleansbutnotusers);
    
        //create the permission
        $manageCleansandusers = $auth->createPermission('manageCleansandusers');
        $manageCleansandusers->description = 'Manage Cleans and Users';
        //add the permission
        $auth->add($manageCleansandusers);
    
        //create the role
        $moderator = $auth->createRole('moderator');
        $moderator->description = 'Moderator';
        //add the role
        $auth->add($moderator);
        //attach the permissions to the role
        $auth->addChild($moderator, $manageCleansbutnotusers);
    
        //create the role
        $admin = $auth->createRole('admin');
        $admin->description = 'Administrator';
        //add the role 
        $auth->add($admin);
        //attach both permissions to the admin role
        $auth->addChild($admin, $moderator);
        $auth->addChild($admin, $manageCleansandusers);
    

    我过去曾将其用于迁移目的。

    有人能告诉我什么是更好的方法吗?我确信有人使用Yeesoft cms控制面板来控制对前端的访问,而无需采取以下措施:

     'components' => [
    
        'authManager' => [
                'class' => 'yii\rbac\DbManager'
        ],
    ]
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Jairus    8 年前

    在Yii2高级设置中:前端和后端应用程序都有自己的配置。对于前端和后端具有共性的实例,可以利用公共配置。例如,数据库、AD登录或挂钩扩展的配置。

    • 后端-后端web应用程序。
    • common—所有应用程序通用的文件。
    • 控制台-控制台应用程序。
    • 环境—环境配置。
    • 前端-前端web应用程序。

    看见 Yii-App-Advanced