Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get users of a specefic role in Yii2 and DbManager?

Tags:

php

yii

yii2

rbac

How to get users of a specefic role in Yii2 and DbManager in RBAC?

Please introduce some API for user management and role management.

I searched and read Yii2 guide but I didn't find any solution.

like image 335
b24 Avatar asked Aug 11 '14 16:08

b24


2 Answers

Since Yii version 2.0.7, DbManager and ManagerInterface which it implements have getUserIdsByRole($roleName) which does what you want without custom code.

like image 144
mmonem Avatar answered Nov 04 '22 21:11

mmonem


I wrote this function which can be added to an User class.

 /**
 * Finds all users by assignment role
 *
 * @param  \yii\rbac\Role $role
 * @return static|null
 */
public static function findByRole($role)
{
    return static::find()
        ->join('LEFT JOIN','auth_assignment','auth_assignment.user_id = id')
        ->where(['auth_assignment.item_name' => $role->name])
        ->all();
}
like image 36
Paul van Schayck Avatar answered Nov 04 '22 22:11

Paul van Schayck