denchotsanov / yii2-user-rbac
v0.3.7
2019-11-18 20:02 UTC
Requires
- php: ^7.0
- 2amigos/yii2-arrayquery-component: ~1.0
- denchotsanov/yii2-user: *
- fxp/composer-asset-plugin: ^1.4.0
- yiisoft/yii2: ^2.0.18
- yiisoft/yii2-jui: ~2.0
README
Yii2 RBAC 用户扩展
安装
安装此扩展的首选方式是通过 Composer。
运行以下命令:
composer require --prefer-dist denchotsanov/yii2-user-rbac "*"
或在您的 composer.json 文件的 require 部分添加以下内容:
"denchotsanov/yii2-user-rbac ": "*"
安装
使用
安装扩展后,只需修改您的应用程序配置,如下所示
return [ 'modules' => [ 'rbac' => [ 'class' => 'denchotsanov\rbac\Module', ], ], 'components' => [ 'authManager' => [ 'class' => 'yii\rbac\DbManager', ], ], ];
下载并配置 Yii2-rbac 后,您需要执行以下操作更新您的数据库模式:
$ php yii migrate/up --migrationPath=@yii/rbac/migrations
或在控制台配置文件中添加
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => [
...
'@yii/rbac/migrations',
...
],
],
],
然后您可以通过以下 URL 访问 Auth 管理员
[SERVER]/rbac/
[SERVER]/rbac/route
[SERVER]/rbac/permission
[SERVER]/rbac/role
[SERVER]/rbac/assignment
应用规则
- 仅对
controller应用规则,请添加以下代码:
use denchotsanov\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 denchotsanov\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' => 'denchotsanov\rbac\Module', 'as access' => [ 'class' => denchotsanov\rbac\filters\AccessControl::class ], ] ] // or apply globally for whole application 'modules' => [ ... ], 'components' => [ ... ], 'as access' => [ 'class' => denchotsanov\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. ] ],