denchotsanov / yii2-user-rbac
Package info
github.com/denchotsanov/yii2-user-rbac
Type:yii2-extension
pkg:composer/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
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Yii2 RBAC USER Extension
Installation
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist denchotsanov/yii2-user-rbac "*"
or add
"denchotsanov/yii2-user-rbac ": "*"
to the require section of your composer.json.
Usage
Once the extension is installed, simply modify your application configuration as follows:
return [ 'modules' => [ 'rbac' => [ 'class' => 'denchotsanov\rbac\Module', ], ], 'components' => [ 'authManager' => [ 'class' => 'yii\rbac\DbManager', ], ], ];
After you downloaded and configured Yii2-rbac, the last thing you need to do is updating your database schema by applying the migration:
$ php yii migrate/up --migrationPath=@yii/rbac/migrations
or add in console config file
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => [
...
'@yii/rbac/migrations',
...
],
],
],
You can then access Auth manager through the following URL:
[SERVER]/rbac/
[SERVER]/rbac/route
[SERVER]/rbac/permission
[SERVER]/rbac/role
[SERVER]/rbac/assignment
Applying rules:
- For applying rules only for
controlleradd the following code:
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. ] ], ]; } }
- For applying rules for
moduleadd the following code:
use Yii; use denchotsanov\rbac\filters\AccessControl; /** * Class Module */ class Module extends \yii\base\Module { /** * @return array */ public function behaviors() { return [ AccessControl::class ]; } }
- Also you can apply rules via main configuration:
// 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. ] ],