X
X
xuxubla2016-11-20 14:47:40
Yii
xuxubla, 2016-11-20 14:47:40

How to link rbac roles and user model?

1. Delivered pure yii2.
2. I stuck dektrium / rbac to it.
3. Created a user table (fk user.role to auth_item.name) and then generated CRUD using gii.
As a result, the model has such a rule:

public function rules()
{
  return [
    ...
    ,
];
}

When creating an entry, he swears that AuthItem does not have a find method. You can remove the check, of course. But this is not safe... Displaying a drop-down list is possible without problems,
but how would I validate this field?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2016-11-20
@webinar

AuthItem has no find method

it means it is inherited from Model and not from ActiveRecords. I don't know why, but if necessary, create a model using gii for a table with AuthItem (it will be inherited from ActiveRecords and will have a find method) and use it to test it.
There is a nuance here, my method and your check will only work when storing RBAC data in the database. There are several options in yii2. Make sure you are using yii\rbac\DbManager and not yii\rbac\PhpManager. Because in the second case, there will be no find method, since the data is not stored in db.

M
Maxim Fedorov, 2016-11-21
@qonand

When creating an entry, he swears that AuthItem does not have a find method.

So it cannot be in a class inherited from \yii\base\Model, it's not ActiveRecord, where does it come from.
Having briefly looked at the component, I can say that the use of \yii\base\Model is due to the fact that in Yii the management of the entire Rbac system is built on the basis of a single manager that has different implementations. For example, in the standard YII package there are two managers yii\rbac\PhpManager (storing data in files) and yii\rbac\DbManager (storing data in the database). In addition, it is possible to implement your own manager based on the yii\rbac\ManagerInterface interface. Accordingly, NOT using ActiveRecord is quite logical, because. it is not known on which project which manager will be used.
Accordingly, you have two options for solving your problem:
1. If you use yii\rbac\DbManager you can create the auth_item model yourself and use it in your rule. 2. Use the in
validator + RBAC manager, for example:
['level', 'in', 'range' => Yii::$app->authManager->getRoles()],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question