Yii \ rbac \ DbManager configuration

Trying to configure DbManager for Yii2. there are many threads about PHP version, however a little about DB version.

What I know:

Step 1: transition script

./yii migrate --migrationPath=@yii/rbac/migrations/

      

Step 2: configuration

...
'authManager' => [
    'class' => 'yii\rbac\DbManager',
    'defaultRoles' => ['admin', 'user', 'guest'],
],
...

      

Step 3: configuring roles / rules

????

      

+3


source to share


1 answer


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

      



http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

+2


source







All Articles