Download the PHP package erickskrauch/yii2-rbac-migration without Composer
On this page you can find all versions of the php package erickskrauch/yii2-rbac-migration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download erickskrauch/yii2-rbac-migration
More information about erickskrauch/yii2-rbac-migration
Files in erickskrauch/yii2-rbac-migration
Package yii2-rbac-migration
Short Description Trait for Yii2, that adds to the migrations methods for managing RBAC
License MIT
Homepage https://github.com/erickskrauch/yii2-rbac-migration
Informations about the package yii2-rbac-migration
Yii2 RBAC Migration
Simple and useful extension for migrations in Yii2. It allows you to perform operations on the Roles and Permission directly within the database migrations. This can be useful if you assign the Roles and Permissions for users in the application and not during the generation of RBAC.
Extension allows you to create, update and remove Roles, Permissions and Rules, and also contains useful builder to easy roles creation.
A warning: if you using constants for Roles, Permissions or Rules names, you should not to use it inside migration classes! Use simple strings with it names. Constants values during application development can change, but you old migrations should stay persistent.
Install
Via Composer
Usage
First of all we must use trait. You can do it within the migration file:
But it's somewhat uncomfortable due to the fact, that every time you will have to implement a getAuthManager()
method.
Therefore, a more convenient way is to create your own base migration class, which already is use this trait
and implemented the required method. For Yii2 Advanced Template this
can be done by creating \console\models\Migration
class with the following contents:
...and extending the migration class from the newly created:
In search of the ideal, you can override in the console application configuration migration path to the you own template file, which will be immediately with necessary base class.
In the future, you can use the methods as follows:
Available methods
RbacMigrateTrait
createPermission($name, $description = null, $ruleName = null): ItemBuilder
Create new permission, add it to authManager and return ItemBuilder object.
createRole($name, $description = null): ItemBuilder
Create new role, add it to authManager and return ItemBuilder object.
addRule($className, $name): void
Add new rule to auth manager. If class no more exists by provided $className, then it will be created and added to authManager, so you old migrations will not fail if you change or delete original rule.
updatePermission($oldName, $newName, $newDescription = false, $newRule = false): void
Method allows you to change signature of exists permission. If $newDescription
passed as (bool)false, then description
will not be changed. The same behavior with $newRule.
updateRole($oldName, $newName, $newDescription = false): void
Method allows you to change signature of exists role. If $newDescription
passed as (bool)false, then description
will not be changed.
removePermission($name): void
Remove permission by passed name.
removeRole($name): void
Remove role by passed name.
removeRule($name): void
Removing rule from auth manager. If class no more exists by provided $name, then it will be created and removed from authManager, so you old migrations will not fail if you change or delete original rule
getRole($role): ItemBuilder
Return ItemBuilder object for passed role name.
getPermission($permission): ItemBuilder
Return ItemBuilder object for passed permission name.
initRbacStructure(): void
Initialize RBAC structure. This is alternative to execute yii migrate --migrationPath=@yii/rbac/migrations/
from console
rollbackRbacStructure(): void
Call down method of RBAC migration. This is alternative to execute
yii migrate/down --migrationPath=@yii/rbac/migrations/
from console
ItemBuilder
addPermission($permission): ItemBuilder
Add child permission by passed permission object or it's name. $this
will be returned.
addRole($role): ItemBuilder
Add child role by passed role object or it's name. $this
will be returned.
removePermission($permission): ItemBuilder
Remove child permission by passed permission object or it's name. $this
will be returned.
removeRole($role): ItemBuilder
Remove child role by passed role object or it's name. $this
will be returned.
Change log
Until the 1.0.0
version will be released, backwards compatibility is guaranteed only in patch releases.
Minor updates may include breaking changes, so check CHANGELOG when updating the minor version.
Testing
For now I have not figured out how to test it and whether it should be tested. If you have any ideas, contributes are welcome.
Contributing
Please see CONTRIBUTING for details.
Credits
- ErickSkrauch
- All Contributors
License
The MIT License (MIT). Please see License File for more information.