首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >yii\rbac\DbManager安装

yii\rbac\DbManager安装
EN

Stack Overflow用户
提问于 2014-09-23 20:49:48
回答 1查看 4.5K关注 0票数 3

试图为DbManager设置Yii2。关于php版本有很多线程,但是DB版本的线程不多。

我知道的是:

步骤1:迁移脚本

代码语言:javascript
复制
./yii migrate --migrationPath=@yii/rbac/migrations/

步骤2:配置

代码语言:javascript
复制
...
'authManager' => [
    'class' => 'yii\rbac\DbManager',
    'defaultRoles' => ['admin', 'user', 'guest'],
],
...

步骤3:设置角色/规则

代码语言:javascript
复制
????
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-06 11:29:55

代码语言:javascript
复制
    $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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26004497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档