Yii2 how using RBAC to bring users with a specific role?

Use RBAC for access rights and roles. You have a table "user" with records of all users. And standard table RBAC "auth_assignment"assign roles to each user. In this case, there are several roles: admin, user, director, superadmin.

In some tables the GridView, when you query from the database, I want to display from the database of all user which has the role user. How it can be done in queries? Yet I see it like this:

  1. Looking for all user in the table "auth_assignment" who have the role user and get the user_id of all user s
  2. Looking for all user whose id is equal to user_id from the previous paragraph and send it all in GridView


I think this is all very difficult. Is there a more simple solution or write more optimal use. Thank you!
June 10th 19 at 15:40
1 answer
June 10th 19 at 15:42
Solution
do join c table auth_assignment and add the condition
$query = User::find()
->select('user.*')
 ->leftJoin('auth_assignment', "auth_assignment`.`user_id` = `user`.`id`)
 ->andWhere(['auth_assignment.item_name'=>'superadmin'])

well, to feed this query DataProvider
PS: wrote from my sleepy head, we need to check. And probably will be the caveat for several roles to 1 user, but the principle I think is clear
Class! What you need)) Now too. Will check tomorrow. If it doesn't feel so let down as a new answer with the correct code. But it looks all logical and it seems like there should not be errors. - jacquelyn_Nikolaus84 commented on June 10th 19 at 15:45
Works perfectly), lacking only in the end ; But that's for "Dummies and Blondes"))) - jacquelyn_Nikolaus84 commented on June 10th 19 at 15:48
, ))) - Henderson_Beat commented on June 10th 19 at 15:51

Find more questions by tags Yii