asasem/yii2rbacplus

基于数据库的角色基础访问控制管理器,适用于yii2

安装次数: 0

依赖者: 0

建议者: 0

安全性: 0

星星: 0

分支: 0

类型:yii2-extension

1.0.6 2016-02-16 23:57 UTC

This package is auto-updated.

Last update: 2024-09-22 16:51:33 UTC


README

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads

基于数据库的角色基础访问控制管理器,适用于yii2

功能

  • 角色、权限和规则的操作
  • 允许为用户分配多个角色
  • 集成方便的视图
  • 集成Yii2-user-plus - 灵活的用户管理模块

Yii2 RBAC manager

安装

安装此扩展的首选方式是通过 composer.

运行以下命令:

php composer.phar require --prefer-dist asasem/yii2rbacplus "*"

或将其添加到 composer.json 文件的 require 部分:

"asasem/yii2rbacplus": "*"

用法

  1. 让我们将模块配置添加到主配置文件中
'components' => [
    'authManager' => [
        'class' => 'yii\rbac\DbManager',
    ],
],
'modules' => [
    'rbac' =>  [
        'class' => 'asasem\yii2rbacplus\Module'
    ]       
]

然后,更新数据库模式

$ php yii migrate/up --migrationPath=@yii/rbac/migrations

好的。完成。现在可用的路由有

  • /rbac/rule
  • /rbac/permission
  • /rbac/role
  • /rbac/assignment
  1. 模块配置可用
'modules' => [
    'rbac' =>  [
        'class' => 'asasem\yii2rbacplus\Module',
        'userModelClassName'=>null,
        'userModelIdField'=>'id',
        'userModelLoginField'=>'username',
        'userModelLoginFieldLabel'=>null,
        'userModelExtraDataColumls'=>null,
        'beforeCreateController'=>null,
        'beforeAction'=>null
    ]       
]
  • userModelClassName: 用户模型类。
    如果没有设置或设置为 null,RBAC Plus 将从 Yii::$app->getUser()->identityClass 获取
  • userModelIdField: 用户模型 id 字段。
    默认 id 字段是 'id',如果数据库中用户表的主键不是 'id',则必须设置此配置
  • userModelLoginField 用户模型登录字段。
    默认登录字段是 'username'。也许你使用电子邮件字段或其他字段进行登录。所以你必须更改这个配置
  • userModelLoginFieldLabel 用户模型登录字段标签。
    如果设置为 null,则标签将来自 $userModelClass->attributeLabels()[$userModelLoginField]
  • userModelExtraDataColumls 你想在用户分配视图中显示的额外数据列。
    默认情况下,分配数据网格视图仅显示 id 和登录列数据。如果你想添加 created_at 列,你可以添加
    'userModelExtraDataColumls'=>[
      [
          'attributes'=>'created_at',
          'value'=>function($model){
              return date('m/d/Y', $model->created_at);
          }
      ]
    ]
    
  • beforeCreateControllerRbac Plus 模块的所有控制器创建之前调用的可调用函数。默认为 null。当你想要限制对 Rbac Plus 模块的访问时,需要配置此设置。
    示例
    'beforeCreateController'=>function($route){
      /**
      *@var string $route The route consisting of module, controller and action IDs.
      */    
    }
    
  • beforeActionRbac Plus 模块中所有控制器中执行操作之前调用的可调用函数。
    默认为 null。当你想要限制对 Rbac Plus 模块中某些控制器中任何操作的访问时,需要配置此设置。
    示例
    'beforeAction'=>function($action){
      /**
      *@var yii\base\Action $action the action to be executed.
      */    
    }