How to create and add Rules to the db in DbManager rbac?

Hi,

I want create and add a Rule in {{%auth_rule}} table of my db in yii2 app.

I can’t find any document or guide for do this operation in yii2. but i found addRule($rule) method in yii\rbac\DbManager and i think, i should use this method for do this but i don’t know how?

Whether i should create a class for any Rules in @app\rbac or this method just used for PhpManager?

Maybe this will help:

create console/controllers/RbacController.php




namespace console\controllers;


use Yii;

use yii\console\Controller;

use console\models\OwnRule;


class RbacController extends Controller {


    public function actionInit() {


        $auth = Yii::$app->authManager;

        $auth->removeAll();


        //add the rule

        $rule = new OwnRule();

        $auth->add($rule);


        // add the "updateOwn" permission and associate the rule with it.

        $updateOwn = $auth->createPermission('updateOwn');

        $updateOwn->description = 'update own';

        $updateOwn->ruleName = $rule->name;

        $auth->add($updateOwn);


        // add "admin" role and give this role

        $admin = $auth->createRole('admin');

        $auth->add($admin);


        // Assign roles to admin. 1 is ID returned by IdentityInterface::getId()

        // usually implemented in your User model.

        $auth->assign($admin, 1);


    }

}



Then run in the terminal: php yii rbac/init

thank you for reply but this is not useful :(

Im new to Yii

After installation of Yii advanced template I performed this below steps

in cmd

  1. composer require mdmsoft/yii2-admin "~2.0"

  2. changed my common/config/main.php as below

‘modules’ => [

'admin' => [


    'class' => 'mdm\admin\Module',


    ...


]


...

],

‘components’ => [

...


'authManager' => [


    'class' => 'yii\rbac\PhpManager', // or use 'yii\rbac\DbManager'


]

],

  1. After that in cmd

yii migrate --migrationPath=@mdm/admin/migrations

  1. then changed common/config/main.php as below

‘components’ => [

...


'user' => [


    'identityClass' => 'mdm\admin\models\User',


    'loginUrl' => ['admin/user/login'],


]

]

  1. Then changed below things

‘class’ => ‘yii\rbac\DbManager’

‘class’ => ‘mdm\admin\models\User’,

  1. In cmd

yii migrate --migrationPath=@yii/rbac/migrations

and removed the below thing in main.php

‘class’ => ‘mdm\admin\models\User’

what else I have to do to create rule.php or what are all the next steps for using rbac.

please anyone answer me

Facing the same issue. Can someone[size=2] please explain ? [/size]

An RBAC rule must be written as a class that extends yii\rbac\Rule and stored in an ordinary php file. Then you can add it to the authManager, which can be either DbManager or PhpManager. It doesn’t matter.




        $auth = Yii::$app->authManager;


        //add the rule

        $rule = new OwnRule();

        $auth->add($rule);


        // add the "updateOwn" permission and associate the rule with it.

        $updateOwn = $auth->createPermission('updateOwn');

        $updateOwn->description = 'update own';

        $updateOwn->ruleName = $rule->name;

        $auth->add($updateOwn);


        // add "admin" role and give this role

        $admin = $auth->createRole('admin');

        $auth->add($admin);



The code above comes from the post of Armand. This is what you have to do. And note that you have to write "OwnRule.php" somewhere in your source directory before you do this.

Guide > Authorization > Using Rules

http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#using-rules

BTW, I prefer to do this using migrations while Armand writes a console command to do this. :)