文件版rbac(简单)

适用于简单少量功能的权限系统

配置

common/config/main.php

  1. <?php
  2. return [
  3. // ...
  4. 'components' => [
  5. 'authManager' => [
  6. 'class' => 'yii\rbac\PhpManager',
  7. ],
  8. // ...
  9. ],
  10. ];

新建rbac目录

yii\rbac\PhpManager 默认将 RBAC 数据保存在 @app/rbac 目录下的文件中。

assignments.php(分配角色)

  1. <?php
  2. /**
  3. * 给user 分配角色
  4. */
  5. return [
  6. // 100,101 是user_id
  7. 100 => ['admin'],
  8. 101 => ['basic']
  9. ];

items.php(角色与权限)

  1. <?php
  2. return [
  3. 'system/system/index' => [
  4. 'type' => \yii\rbac\Item::TYPE_PERMISSION,
  5. 'description' => '系统配置',
  6. ],
  7. 'system/job/list' => [
  8. 'type' => \yii\rbac\Item::TYPE_PERMISSION,
  9. 'description' => '任务列表',
  10. ],
  11. 'admin' => [
  12. 'type' => \yii\rbac\Item::TYPE_ROLE,
  13. 'description' => '超级管理员',
  14. // 权限组
  15. 'children' => [
  16. 'system/system/index',
  17. 'system/job/list',
  18. ]
  19. ],
  20. 'basic' => [
  21. 'type' => \yii\rbac\Item::TYPE_ROLE,
  22. 'description' => '平台管理员',
  23. 'children' => [
  24. 'system/system/index',
  25. ]
  26. ]
  27. ];

校验

  1. <?php
  2. if (\Yii::$app->user->can('system/system/index')) {
  3. // 更新帖子
  4. }

数据库版rbac(复杂)

配置

common/config/main.php anvance版只需要在此配置一次

  1. <?php
  2. return [
  3. 'components' => [
  4. 'authManager' => [
  5. 'class' => 'yii\rbac\DbManager'
  6. // uncomment if you want to cache RBAC items hierarchy
  7. // 'cache' => 'cache',
  8. ]
  9. ]
  10. ];

执行migration

切换到根目录下 windows的cmd下执行 此操作会在mysql中新建相关的表,一定要提前配置好数据库

  1. yii migrate --migrationPath=@yii/rbac/migrations // 输入yes
  2. // 生成的表数据介绍
  3. itemTable 角色与权限表,其中权限可以理解为url路径,角色就是管理员,财务管理员之类的。
  4. itemChildTable 该表存放授权条目的层次关系。默认表名为 "auth_item_child"
  5. assignmentTable 该表存放授权条目对用户的指派情况。默认表名为 "auth_assignment"
  6. ruleTable 该表存放规则。默认表名为 "auth_rule"

核心 authManager

通过配置一项,可以在yii中直接使用

  1. 可以通过 \Yii::$app->authManager 访问 authManager

增加权限

所谓权限可以理解为url路径,比如 site/index,用户是否可以允许访问此路径

  1. <?php
  2. public function add(){
  3. $auth = Yii::$app->authManager;
  4. // 添加一个权限
  5. $pSiteIndex = $auth->createPermission('site/index');
  6. $pSiteIndex->description = '后台首页';
  7. $auth->add($pSiteIndex);
  8. // 添加一个角色
  9. $rAdmin = $auth->createRole('admin');
  10. $rAdmin->description = '超级管理员';
  11. $auth->add($rAdmin);
  12. // 将此权限赋值给此角色
  13. $auth->addChild($rAdmin, $pSiteIndex);
  14. // 为某个管理员赋予admin角色
  15. $auth->assign($rAdmin, 1);
  16. }

使用案例

适用于两种版本

需要所有的控制器都继承一个AuthController之类的,在其 beforeAction 中判断是否可以

  1. <?php
  2. // 授权控制
  3. public function beforeAction ($action)
  4. {
  5. // 父级的action是否已经阻止了请求
  6. if(! parent::beforeAction($action))
  7. {
  8. return false;
  9. }
  10. // Yii::$app->controller->action->uniqueId; 是获取包括模块在内的所有的路径
  11. // 如 version/version/add
  12. $permission_name = Yii::$app->controller->action->uniqueId;
  13. // 游客则允许跳转登录页 和 允许访问验证码
  14. $is_guest = Yii::$app->user->isGuest;
  15. if($is_guest && ($permission_name == 'site/login' || $permission_name == 'site/captcha'))
  16. {
  17. return true;
  18. }
  19. // 还需要判断是否为登录页
  20. // 超级管理员 - 目前超级管理是全部的权限集合,没有设定all是全部
  21. if(! can($permission_name))
  22. {
  23. // todo something
  24. // 提示无权限
  25. throw new UserException("对不起,您现在还没获此操作的权限");
  26. }
  27. return true;
  28. }