memclutter/yii2-rbac-migration

This package is abandoned and no longer maintained. No replacement package was suggested.

Migration helper trait for Yii2

1.0.0 2015-10-07 07:30 UTC

This package is not auto-updated.

Last update: 2017-08-11 14:16:21 UTC


README

Migration helper trait for Yii2

Installation

PHP 5.4 or higher is required to use it.

Installation is recommended to be done via composer by running:

composer require memclutter/yii2-rbac-migration "*"

Alternatively you can add the following to the require section in your composer.json manually:

"memclutter/yii2-rbac-migration": "*"

Run composer update afterwards.

Usage

Here are some examples:

class mXXXXXX_YYYYYY_rbac extends Migration
{
    use \memclutter\rbac\MigrationTrait;

    public function up()
    {
        // create 'user' and 'admin' role, make 'user' child role of 'admin'
        $this->createRole('user', ['description' => 'Simple user']);
        $this->createRole('admin', ['description' => 'Administrator']);
        $this->addChildRole('admin', 'user');

        // create permission 'updatePost' and make child of 'admin' role.
        $this->createPermission('updatePost', ['description' => 'Update blog post']);
        $this->addPermissionForRole('admin', 'updatePost');
        
        // create permission 'updateOwnPost' and make child of 'user' role.
        $this->createPermission('updateOwnPost', ['description' => 'Update own post']);
        $this->addPermissionForRole('user', 'updateOwnPost');
    }

    public function down()
    {
        // drop all permissions and roles
        $this->dropPermissionForRole('user', 'updateOwnPost');
        $this->dropPermission('updateOwnPost');

        $this->dropPermissionForRole('admin', 'updatePost');
        $this->dropPermission('updatePost');
  
        $this->dropChildRole('admin', 'user');
        $this->dropRole('admin');
        $this->dropRole('user');
    }
}