yii2mod / yii2-rbac
Yii2的RBAC管理模块
Requires
- php: >=7.0.0
- 2amigos/yii2-arrayquery-component: ~1.0
- yiisoft/yii2: ~2.0.13
- yiisoft/yii2-jui: ~2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- phpunit/phpunit: ~6.0
README
Yii2 RBAC扩展
Yii2-RBAC提供了一个用于高级访问控制的Web界面,并包括以下特性:
- 允许对角色、权限、规则进行CRUD操作
- 允许将多个角色或权限分配给用户
- 允许创建控制台迁移
- 与yii2mod/base集成
支持我们
您的业务是否依赖于我们的贡献?通过Patreon联系我们并支持我们。所有的承诺都将用于分配人力以维护和新奇的功能。
安装
安装此扩展的首选方式是通过composer。
运行
php composer.phar require --prefer-dist yii2mod/yii2-rbac "*"
或添加
"yii2mod/yii2-rbac": "*"
到您的composer.json文件的require部分。
使用方法
安装扩展后,只需按以下方式修改您的应用程序配置:
return [ 'modules' => [ 'rbac' => [ 'class' => 'yii2mod\rbac\Module', ], ], 'components' => [ 'authManager' => [ 'class' => 'yii\rbac\DbManager', 'defaultRoles' => ['guest', 'user'], ], ], ];
下载并配置好Yii2-rbac后,您需要通过应用迁移来更新您的数据库模式。
$ php yii migrate/up --migrationPath=@yii/rbac/migrations
您可以通过以下URL访问Auth管理器:
http://localhost/path/to/index.php?r=rbac/
http://localhost/path/to/index.php?r=rbac/route
http://localhost/path/to/index.php?r=rbac/permission
http://localhost/path/to/index.php?r=rbac/role
http://localhost/path/to/index.php?r=rbac/assignment
或如果您启用了漂亮URL,您可以使用以下URL:
http://localhost/path/to/index.php/rbac
http://localhost/path/to/index.php/rbac/route
http://localhost/path/to/index.php/rbac/permission
http://localhost/path/to/index.php/rbac/role
http://localhost/path/to/index.php/rbac/assignment
应用规则
- 仅对
controller
应用规则,请添加以下代码:
use yii2mod\rbac\filters\AccessControl; class ExampleController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => AccessControl::class, 'allowActions' => [ 'index', // The actions listed here will be allowed to everyone including guests. ] ], ]; } }
- 对
module
应用规则,请添加以下代码:
use Yii; use yii2mod\rbac\filters\AccessControl; /** * Class Module */ class Module extends \yii\base\Module { /** * @return array */ public function behaviors() { return [ AccessControl::class ]; } }
- 您也可以通过主要配置应用规则
// apply for single module 'modules' => [ 'rbac' => [ 'class' => 'yii2mod\rbac\Module', 'as access' => [ 'class' => yii2mod\rbac\filters\AccessControl::class ], ] ] // or apply globally for whole application 'modules' => [ ... ], 'components' => [ ... ], 'as access' => [ 'class' => yii2mod\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. ] ],
国际化
此扩展中引入的所有文本和信息都可在'yii2mod.rbac'类别下进行翻译。您可以使用以下应用程序配置使用此扩展中提供的翻译:
return [ 'components' => [ 'i18n' => [ 'translations' => [ 'yii2mod.rbac' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@yii2mod/rbac/messages', ], // ... ], ], // ... ], // ... ];
迁移
您可以创建控制台迁移来创建/更新RBAC项。
模块设置
要能够创建迁移,您需要将以下代码添加到您的控制台应用程序配置中:
// console.php 'modules' => [ 'rbac' => [ 'class' => 'yii2mod\rbac\ConsoleModule' ] ]
方法
createPermission()
:创建权限updatePermission()
:更新权限removePermission()
:删除权限createRole()
:创建角色updateRole()
:更新角色removeRole()
:删除角色createRule()
:创建规则updateRule()
:更新规则removeRule()
:删除规则addChild()
:创建子节点removeChild()
:删除子节点assign()
:将角色分配给用户
创建迁移
要创建新的迁移,请运行以下命令:
$ php yii rbac/migrate/create <name>
必需的name
参数给出了新迁移的简要描述。例如,如果迁移是关于创建名为admin的新角色,您可以使用名称create_role_admin
并运行以下命令:
$ php yii rbac/migrate/create create_role_admin
上述命令将在@app/rbac/migrations目录中创建一个名为m160817_085702_create_role_admin.php的新PHP类文件。该文件包含以下代码,它主要声明一个名为m160817_085702_create_role_admin的迁移类,并包含骨架代码:
<?php use yii2mod\rbac\migrations\Migration; class m160817_085702_create_role_admin extends Migration { public function safeUp() { } public function safeDown() { echo "m160817_085702_create_role_admin cannot be reverted.\n"; return false; } }
以下代码显示了如何实现迁移类以创建一个admin
角色:
<?php use yii2mod\rbac\migrations\Migration; class m160817_085702_create_role_admin extends Migration { public function safeUp() { $this->createRole('admin', 'admin has all available permissions.'); } public function safeDown() { $this->removeRole('admin'); } }
您可以在这里看到一个复杂的迁移示例。
应用迁移
要将数据库升级到最新结构,您应该使用以下命令应用所有可用的新迁移:
$ php yii rbac/migrate
回滚迁移
要回滚(撤销)之前已应用的某个或多个迁移,您可以运行以下命令
$ php yii rbac/migrate/down # revert the most recently applied migration $ php yii rbac/migrate/down 3 # revert the most 3 recently applied migrations
重新执行迁移
重新执行迁移意味着首先回滚指定的迁移,然后再重新应用。这可以通过以下方式完成
$ php yii rbac/migrate/redo # redo the last applied migration $ php yii rbac/migrate/redo 3 # redo the last 3 applied migrations