How to check if user has role with RBAC


I use mdmsoft/yii2-admin. I can check the current logged in user role like

How to check other user has admin role? I tried
Yii::$app->user->can('admin', ['user_id' => 2]); but not works.

Hi @istvan0304, welcome to the forum.

Please check the API Reference of yii\rbac\ManagerInterface ( where you’ll find some methods that you might be interrested.

  1. checkAccess() … Checks if the user has the specified permission.
  2. getRolesByUser() … Returns the roles that are assigned to the user via assign()
  3. getuserIdsByRole() … Returns all user IDs assigned to the role specified.

These methods can be accessed via authManager application component. For example:

// user ID
$user_id = 1;
// The roles assigned to the user
$user_roles = Yii::$app->authManager->getRolesByUser($user_id);
1 Like