Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user role in Yii2?

Tags:

php

yii

yii2

rbac

How to get user role in Yii2?

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

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

b24


3 Answers

You can get Roles for an user by using getRolesByUser function

You can use it like this

\Yii::$app->authManager->getRolesByUser($user_id);
like image 64
Manquer Avatar answered Sep 23 '22 06:09

Manquer


You can use:

Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());
like image 25
d4v1d Avatar answered Sep 21 '22 06:09

d4v1d


I use :

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

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

like image 45
elfarqy Avatar answered Sep 23 '22 06:09

elfarqy