panix/mod-rbac

RBAC管理模块

安装次数: 235

依赖关系: 4

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:pixelion-module

dev-master 2023-02-13 22:21 UTC

This package is auto-updated.

Last update: 2024-09-14 01:52:45 UTC


README

模块RBAC提供了一个用于高级访问控制的Web界面,并包括以下功能:

  • 允许对角色、权限、规则进行CRUD操作
  • 允许为用户分配多个角色或权限
  • 允许创建控制台迁移

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads License

安装

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

运行以下命令

php composer require --prefer-dist panix/mod-rbac "*"

或将以下内容添加到您的composer.json文件的require部分:

"panix/mod-rbac": "*"

用法

扩展安装后,只需按如下方式修改您的应用程序配置:

return [
    'modules' => [
        'rbac' => [
            'class' => 'panix\mod\rbac\Module',
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' => ['guest', 'user'],
        ],
    ],
];

在下载并配置了Yii2-rbac之后,您需要做的最后一件事是应用迁移来更新您的数据库架构。

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

您可以通过以下URL访问认证管理器:

https:///path/to/index.php?r=rbac/
https:///path/to/index.php?r=rbac/route
https:///path/to/index.php?r=rbac/permission
https:///path/to/index.php?r=rbac/role
https:///path/to/index.php?r=rbac/assignment

如果已启用美观的URL,您还可以使用以下URL:

https:///path/to/index.php/rbac
https:///path/to/index.php/rbac/route
https:///path/to/index.php/rbac/permission
https:///path/to/index.php/rbac/role
https:///path/to/index.php/rbac/assignment

应用规则

  1. 仅对controller应用规则,请添加以下代码
use panix\mod\rbac\filters\AccessControl;

class AdminController extends Controller 
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'allowActions' => [
                    'index',
                    // The actions listed here will be allowed to everyone including guests.
                ]
            ],
        ];
    }
}
  1. module应用规则,请添加以下代码
use Yii;
use panix\mod\rbac\filters\AccessControl;

/**
 * Class Module
 */
class Module extends \yii\base\Module
{
    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            AccessControl::class
        ];
    }
}
  1. 您也可以通过主配置应用规则
// apply for single module

'modules' => [
    'rbac' => [
        'class' => 'panix\mod\rbac\Module',
        'as access' => [
            'class' => panix\mod\rbac\filters\AccessControl::class
        ],
    ]
]

// or apply globally for whole application

'modules' => [
    ...
],
'components' => [
    ...
],
'as access' => [
    'class' => panix\mod\rbac\filters\AccessControl::class,
    'allowActions' => [
        'site/*',
        'admin/*',
        // The actions listed here will be allowed to everyone including guests.
        // So, 'admin/*' should not appear here in the production, of course.
        // But in the earlier stages of your development, you may probably want to
        // add a lot of actions here until you finally completed setting up rbac,
        // otherwise you may not even take a first step.
    ]
 ],

迁移

您可以为创建/更新RBAC项目创建控制台迁移

模块设置

要能够创建迁移,您需要将以下代码添加到您的控制台应用程序配置中

// console.php
'modules' => [
    'rbac' => [
        'class' => 'panix\mod\rbac\ConsoleModule'
    ]
]

方法

  1. createPermission():创建权限
  2. updatePermission():更新权限
  3. removePermission():删除权限
  4. createRole():创建角色
  5. updateRole():更新角色
  6. removeRole():删除角色
  7. createRule():创建规则
  8. updateRule():更新规则
  9. removeRule():删除规则
  10. addChild():创建子项
  11. removeChild():删除子项
  12. assign():将角色分配给用户

您可以在以下位置查看迁移的复杂示例:这里。

应用迁移

要升级数据库到最新结构,您应该应用所有可用的新迁移,使用以下命令

$ php cmd rbac/migrate

回滚迁移

要回滚(撤销)之前应用的一个或多个迁移,您可以运行以下命令

$ php cmd rbac/migrate/down     # revert the most recently applied migration
$ php cmd rbac/migrate/down 3   # revert the most 3 recently applied migrations

重做迁移

重做迁移意味着首先回滚指定的迁移,然后再次应用。这可以按以下方式完成

$ php cmd rbac/migrate/redo     # redo the last applied migration
$ php cmd rbac/migrate/redo 3   # redo the last 3 applied migrations