Download the PHP package arogachev/yii2-rbac without Composer
On this page you can find all versions of the php package arogachev/yii2-rbac. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download arogachev/yii2-rbac
More information about arogachev/yii2-rbac
Files in arogachev/yii2-rbac
Package yii2-rbac
Short Description RBAC management for Yii 2 framework
License BSD-3-Clause
Homepage https://github.com/arogachev/yii2-rbac
Informations about the package yii2-rbac
Yii 2 RBAC
RBAC management for Yii 2 framework.
The main purpose of this extension is to provide management of RBAC roles, permissions, rules and relations between them through configuration arrays.
- Installation
- Features
- Configuration arrays
- Data synchronization
- Rules
- GUI
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json file.
Features
- Adding new roles and permissions with descriptions
- Updating permissions names, descriptions and rules
- Deleting permissions, rules
- Updating roles descriptions
- Assigning rules to permissions
- Assigning permissions to roles
Configuration arrays
First of all, you need to create three files for storing RBAC data:
roles.php. Used for storing roles.permissions.php. Used for storing permissions and relations between permissions and rules.children.php. Used for storing relations between roles and permissions.
You can place it anywhere you want. If you are using advanced application, it's recommended to place them in
common/rbac/data folder.
Example of roles.php content:
Both name and description are required for filling.
default is not required, but most of the times is needed because some permissions require check without assigning.
In this case make sure you have include it in your application config:
Example of permissions.php content:
Both name and description are required for filling, rule is optional.
Example of children.php content:
Data synchronization
To synchronize actual RBAC data with configuration arrays data add this to your console application config
(config/console.php for basic application and console/config/main.php for advanced application):
Then you need to run command:
List of available options in parserOptions:
$configPath- full path to folder with config files. Aliases are supported. Required for filling.
Rules
Extension provides arogachev\rbac\rules\CorrespondingUserRule that can be used to only allow
user to edit his own posts, etc. It's similar to AuthorRule described in official docs here.
You can attach it to permission as shown above, and use it in action as follows:
Use the related permission after the model was found.
Available params:
$model- Model used for checking. Required for filling.attribute- The attribute name containing user id. Defaults toauthor_id.
In case of using advanced application it's recommended to place common rules like that in common/rbac/rules.
More specific rules can be placed inside of according modules.
GUI
You can use AssignRoleToUserForm for assigning role to user.
Example of action (you can place it in UsersController):
There are also assign-role and _assign-role-form (partial) views that you can use. It's for Bootstrap,
if it don't fit your needs you can copy it and modify how you want, it's just a template.
To create a link for that action, most of the times, extending GridView ActionColumn is enough: