项目作者: yiichina

项目描述 :
基于 Yii2 的 RBAC 通用管理模块
高级语言: PHP
项目地址: git://github.com/yiichina/yii2-rbac-module.git
创建时间: 2018-11-16T11:59:22Z
项目社区:https://github.com/yiichina/yii2-rbac-module

开源协议:Apache License 2.0

下载


Yii 2 RBAC Module

Latest Stable Version
Total Downloads
Yii2

安装

安装这个扩展的首选方法是通过 composer

可以运行

  1. composer require --prefer-dist yiichina/yii2-rbac-module "*"

也可以添加

  1. "yiichina/yii2-rbac-module": "*"

到你的 composer.json 文件的包含部分。

安装方法

配置模块

  1. 'modules' => [
  2. 'rbac' => [
  3. 'class' => 'yiichina\modules\rbac\Module',
  4. ],
  5. ],

配置 RBAC

  1. 'authManager' => [
  2. 'class' => 'yii\rbac\DbManager',
  3. 'defaultRoles' => ['user'],
  4. ],

初始化数据库

  1. php yii migrate/up --migrationPath=@yii/rbac/migrations

也可以找到 @yii/rbac/migrations 目录下的数据库脚本去执行。

使用方法

  1. /index.php?r=rbac/assignment
  2. /index.php?r=rbac/permission
  3. /index.php?r=rbac/role
  4. /index.php?r=rbac/rule

如果配置了 URL 美化,您可以使用如下 URL:

  1. /rbac/assignment
  2. /rbac/permission
  3. /rbac/role
  4. /rbac/rule

在需要权限判断的时候,使用如下方式:

  1. if (Yii::$app->user->can('admin')) {
  2. // 可以执行的语句
  3. }

使用控制器行为:

  1. public function behaviors()
  2. {
  3. return [
  4. 'access' => [
  5. 'class' => AccessControl::className(),
  6. 'only' => ['delete'],
  7. 'rules' => [
  8. 'actions' => ['delete'],
  9. 'allow' => true,
  10. 'roles' => ['admin'], // 在这里指定可以操作此动作的角色
  11. 'verbs' => ['POST'],
  12. ],
  13. ],
  14. ];
  15. }

延伸阅读

如果需要针对动作的权限管理,请参考:
https://github.com/yii2mod/yii2-rbac

同时包含 DbManagerPhpManager 的权限管理:
https://github.com/dektrium/yii2-rbac