18

How to get user role in Yii2?

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

b24
  • 2,425
  • 6
  • 30
  • 51

8 Answers8

37

You can get Roles for an user by using getRolesByUser function

You can use it like this

\Yii::$app->authManager->getRolesByUser($user_id);
Manquer
  • 7,390
  • 8
  • 42
  • 69
  • 1
    Thanks man. It's work for me. Also I use **User::findByUsername($user_name)->getId()** to pass id to **getRolesByUser()** function – b24 Aug 22 '14 at 04:59
  • if you want to get role name as string not array, use this: ` $userRole = array_keys(Yii::$app->authManager->getRolesByUser($user_Id))‌​[0] ` – Shaig Khaligli Oct 12 '17 at 21:45
  • 1
    If you are using `defaultRoles` it will return those as well regardless of whether there are any rules that would effectively not allow them to take on those roles! – TheStoryCoder Jun 25 '19 at 10:14
21

You can use:

Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
d4v1d
  • 341
  • 2
  • 6
  • but this returns the array of all roles and not the user role. – Awais Mustafa Mar 12 '18 at 07:53
  • @AwaisMustafa I this you should check again, because getRolesByUser(Yii::$app->user->getId()) will return only the roles of that user. – d4v1d Aug 04 '18 at 14:59
  • @AwaisMustafa Is that because you are using `defaultRoles`? If so it seems like the rules are not being applied and hence you will see those default roles regardless.... – TheStoryCoder Jun 25 '19 at 10:16
10

I use :

if (\Yii::$app->authManager-> getAssignment($role,$rule_id))

for filtering user id and role in rbac, More details on Yii2 Documentation

elfarqy
  • 161
  • 1
  • 4
  • The parameter passed should not be $rule_id but $user_id. Minor small typo according to documentation. Thanks anyway. I have used it in my controller. I have voted you up. – Addi Apr 23 '20 at 11:38
1

One more example how to get user role:
Yii::$app->getUser()->identity->role;
It works if you have column with name "role" in your user table.

1

You can use :

 $user =[];
 $userAssigned = Yii::$app->authManager->getAssignments(user_id);
 foreach($userAssigned as $userAssign){
      $user[] = $userAssign->roleName;
 } 
Mohan Prasad
  • 682
  • 1
  • 9
  • 34
0

If you're using amnah/yii2-user module, you can use this:

Yii::$app->user->identity->role->name

It will give you the current user role name

Imtiaz
  • 2,484
  • 2
  • 26
  • 32
0

The good and more visual decision will be setting constants of all roles.

$userID = $user->getId();
array_keys(Yii::$app->authManager->getRolesByUser($userID))[0] == User::ROLE_NAME
Alliswell
  • 1,523
  • 20
  • 35
-2

You can get role of the user by using createcommand.

    $roleModel = Yii::$app->db
    ->createCommand("Select * from auth_assignment where user_id='889'")
    ->queryOne();

    echo $roleModel['item_name'];

This will you user's role in auth assignment. Now you can check that if user is admin or editor or member.

Awais Mustafa
  • 151
  • 2
  • 9
  • Its much easier to use the methods the framework has to verify a role (accepted answer), than making a query to the database and needing to verify manually – André Mar 12 '18 at 11:29
  • i faced this issue that within the code, i tried alot to get a the role name of the admi nuser but i was successfull with this query so thats why i have shared this so that it may helps any one else – Awais Mustafa Mar 17 '18 at 11:36
  • And what if the roles are stored not in db, but in redis (for example). This is not viable approach at all. – bksi Apr 18 '22 at 11:26