S
S
Sergey2015-03-15 22:42:59
Yii
Sergey, 2015-03-15 22:42:59

How to bind RBAC rules in yii2 to users based on the given roles in the database?

In general, in the config I connect the module:

'authManager' => [
            'class' => 'yii\rbac\PhpManager',
            'authFile' => '@app/config/rbac.php'
        ],

In rbac.php I prescribe roles, well, for example, let there be one admin role:
<?php

return array (
    'items' =>array(
        'admin'=>array (
            'type'=>1, //где вообще почитать про все доступные типы этих ролей? Или что эта за единица?
            'name'=>'admin',
            'ruleName' => 'admin'
        )
    )
);

But then I find it difficult to implement. How to assign this role to certain users who have admin written in the database in the role column, so that later you can work and check this condition:
if (Yii::$app->user->can('admin')) {
     //делаем что-то, доступное только админу
}

Actually, how? I read the documentation, but I'm not catching up with a few points:
1. How to do this? I can take data from the database, for example, into the $role variable. What's next?
if ($role == 'admin') {
    //присвоить этому юзеру админские права раз и на всегда, но какими API это делается? 
}

2. Where to assign it and when? Not when you start the same application every time you do it or in the controller.
3. And what will happen if I want the user to decide whether everything needs to be reassigned or is there any functionality that allows you to do this automatically if there is a different value in the database?
In general, is there any way to do everything according to Feng Shui, to set the necessary roles for users based on the data in the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Semenko, 2015-03-15
@abler98

You need to create the right for the roles, here you can read more about this, and not only,
https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question