phpyii2rbacyii2-rbac

How to integrate Yeesoft/Yii2 cms RBAC (Role Based Access Control) with YII2 frontend RBAC?


I have been working with the yii2 advanced template and I am now wanting to implement some RBAC into my frontend project's controllers.

I am very impressed with Yeesoft's/Yii2 cms RBAC control panel from https://github.com/yeesoft/yii2-yee-cms although I will probably not use a lot of their content management functionality. However I am impressed with its control panel and would like to use it to control frontend access giving certain permissions to my employees.

I have included this code in its frontend\config\main.php under its components section.

'components' => [

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

This enables me to include code like the following in the frontend controllers

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

to control access.

I am using yeesoft's database and am contemplating migrating all my data across from my frontend database to yeesoft's cms database because I can create permissions under it using the control panel and access the permission data without having to write extensive console migration code using

Yii::$app->authManager;

and other complex code like the following:

$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);

which I have used in the past for migration purposes.

Can someone advise me on what a better approach is? I am sure someone has used the Yeesoft cms control panel to control access to the frontend without having to resort to the following:

 'components' => [

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

Solution

  • In the Yii2 advanced setup: the frontend and backend applications each have their own configurations. The common configuration can be leveraged for instances where the frontend and backend have commonalities. For instance, configurations for databases, AD logins or hooked extensions.

    See Yii-App-Advanced.