简体   繁体   English

yii \\ rbac \\ DbManager设置

[英]yii\rbac\DbManager setup

Trying to set up the DbManager for Yii2. 尝试为Yii2设置DbManager。 there are lots of threads about the php version, however, not much about the DB version. 有很多关于php版本的线程,但是,关于数据库版本并不多。

What I do know: 我所知道的:

Step 1: migrate script 第1步:迁移脚本

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

Step 2: configuration 第2步:配置

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

Step 3: set up roles / rules 第3步:设置角色/规则

????
    $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 http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM