b24 b24 - 1 month ago 14
PHP Question

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

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.

b24 b24
Answer

I used @Manquer guide and wrote this function:

public static function getRoleUsers($role_name)
    {
        $connection = \Yii::$app->db;
        $connection->open();

        $command = $connection->createCommand(
            "SELECT * FROM auth_assignment INNER JOIN user ON auth_assignment.user_id = user.id " .
            "WHERE auth_assignment.item_name = '" . $role_name . "';");

        $users = $command->queryAll();
        $connection->close();

        return $users;
    }

Maybe useful for someone.