RBAC help needed

Dear Friends thank you for your detailed discussion.

I have one doubt.I read most of the docs .But i couldn’t understand may be because of i’m new to yii2

Suppose we have 3 types of logged in users Admin,Creator and Editor

I need to restrict creator to edit and delete

I need to restrict editor to create and delete

Admin can create ,edit and delete

so these are my actions

public function actionCreate(){ }

public function actionUpdate($id) { }

public function actionDelete($id) { }

Do i add anything on behaviour or beforeaction or call function like Can() ?

if so how :(

Best regards :)

I’m just using the behaviors() and it is working for me…

I did not use the can() function yet…

from what you said:




public function behaviors()

    {

        return [

            'access' => [

                'class' => AccessControl::className(),

                'only' => ['create', 'update', 'delete'],

                'rules' => [

                    [

                        'allow' => false,

                        'actions' => ['update', 'delete'],

                        'roles' => ['Creator'], // roles affected by the rules

                    ],

                    [

                        'allow' => false,

                        'actions' => ['create', 'delete'],

                        'roles' => ['Editor'], 

                    ],

                    [

                        'allow' => true,

                        'actions' => ['create', 'update', 'delete'],

                        'roles' => ['Admin'], // admin can access delete action

                    ],

                ],

            ],

        ];

    }



if I’m wrong in anything I’m sorry, but I can bet that you can copy/past this :P

Dear Friend thanks for your kind reply

Suppose i have to create dynamic roles and permissions what will we do here

How we dynamically populate roles and actions here ?

:)

that is what this topic was for, try to understand it if you have any doubt we can help you :)

there are a lot of links to the right pages in the documentation

Ya i’m trying for this past two weeks and posted on many sites but couldn’t get the solution.

Need to know how to add it dynamically then only i got exact results

Let’s hope someone answer this :blink:

I don’t get what is your question, please rephrase.

Sorry for the late response… :)

Im following the RBAC Guide, and there seems to be something missing. Namely, when I run the "yii rbac/init" command, I get the following error:


PHP Warning 'yii\base\ErrorException' with message 'Invalid argument supplied for foreach()'


in /var/www/site/vendor/yiisoft/yii2/rbac/PhpManager.php:641


Stack trace:

#0 /var/www/site/vendor/yiisoft/yii2/rbac/PhpManager.php(641): yii\base\ErrorHandler->handleError(2, 'Invalid argumen...', '/var/www/site/v...', 641, Array)

This looks like it happens when PhpManager tries to load $items. My backend/rbac/items.php file is blank, since I havent run this migration yet.

Has anyone else run into this error? Ive pretty much followed the guide exactly as it is written. What am I missing? What needs to go in items.php for PhpManager to work? What information is missing from the guide?

------- update -----------

after reading all the source code for everything rbac (w/ PhpManager), i realized there are some steps to the guide that are a bit misleading. i had already created a blank items.php, assignments.php, and roles.php before i created the console command (RbacController). once i deleted these files, the console command i made worked as expected.