PHP code example of erickskrauch / yii2-rbac-migration

1. Go to this page and download the library: Download erickskrauch/yii2-rbac-migration library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

erickskrauch / yii2-rbac-migration example snippets



namespace console\models;

use ErickSkrauch\Yii2\RbacMigrateTrait;
use yii\db\Migration as YiiMigration;

class Migration extends YiiMigration
{
    use RbacMigrateTrait;

    protected function getAuthManager()
    {
        return \Yii::$app->authManager;
    }
}
 php


class m160705_120827_init extends \yii\db\Migration
{
    use \ErickSkrauch\Yii2\RbacMigrateTrait;

    public function safeUp()
    {
        $this->createPermission('upload_data');
    }

    public function safeDown()
    {
        $this->removePermission('upload_data');
    }

    protected function getAuthManager()
    {
        return Yii::$app->authManager;
    }
}
 php


use console\models\Migration;

class m160705_120827_init extends Migration
{
    public function safeUp()
    {
        $this->createPermission('upload_data');
    }

    public function safeDown()
    {
        $this->removePermission('upload_data');
    }
}

// Init db structure, create few permissions and one role and assing permissions to that role
public function safeUp()
{
    $this->initRbacStructure();
    $this->createPermission('view_invoices');
    $this->createPermission('data_analysis');
    $this->createPermission('upload_data');
    $this->createRole('accountant')
         ->addPermission('view_invoices')
         ->addPermission('data_analysis')
         ->addPermission('upload_data');
}

public function safeDown()
{
    $this->removePermission('view_invoices');
    $this->removePermission('data_analysis');
    $this->removePermission('upload_data');
    $this->removeRole('accountant');
    $this->rollbackRbacStructure();
}