johnitvn / yii2-rbac-plus

Database role base access control manager for yii2
49 stars 52 forks source link

a role should have 1 or more roles as its children #12

Open michaelnguyen2021 opened 9 years ago

michaelnguyen2021 commented 9 years ago

this is straight from Yii2 RBAC documentation ( https://github.com/yiisoft/yii2/blob/master/docs/guide/images/rbac-hierarchy-1.png )

There is "author" and "admin" role.

"Admin" role is a parent of "author" role which mean "Admin" can have all "author" permission. This will reduce the duplication. Without this setup, you need to assign all "author" permission to admin.

Sample code to link a role to another role

<?php
namespace app\commands;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
    public function actionInit()
    {
        $auth = Yii::$app->authManager;

        // add "createPost" permission
        $createPost = $auth->createPermission('createPost');
        $createPost->description = 'Create a post';
        $auth->add($createPost);

        // add "updatePost" permission
        $updatePost = $auth->createPermission('updatePost');
        $updatePost->description = 'Update post';
        $auth->add($updatePost);

        // add "author" role and give this role the "createPost" permission
        $author = $auth->createRole('author');
        $auth->add($author);
        $auth->addChild($author, $createPost);

        // add "admin" role and give this role the "updatePost" permission
        // as well as the permissions of the "author" role
        $admin = $auth->createRole('admin');
        $auth->add($admin);
        $auth->addChild($admin, $updatePost);
        $auth->addChild($admin, $author);

        // Assign roles to users. 1 and 2 are IDs returned by IdentityInterface::getId()
        // usually implemented in your User model.
        $auth->assign($author, 2);
        $auth->assign($admin, 1);
    }
}
mcki0127 commented 8 years ago

Do you have plans to add this as a feature?

ahmadasjad commented 7 years ago

Hello johnitvn, are you alive?