J
J
Javid Askerov2015-09-18 20:49:35
Yii
Javid Askerov, 2015-09-18 20:49:35

Yii2 RBAC - get user role?

I use the following construction to get the user's role:
\Yii::$app->authManager->getRolesByUser($user->id)
I get an empty array. Maybe I didn’t set up RBAC in the right way, of course, but everything seems to be the same, AccessControl according to the roles shows only the necessary data to the right users. But I wanted to see the roles of users in the admin panel and I can’t display it.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
J
Javid Askerov, 2016-07-17
@HalfBloodPrince

sawa4 , I don’t have much to do with ui now. But then, personally, my problem was that I did not assign roles to users during registration. After I fixed it, it worked.

S
sawa4, 2016-07-16
@sawa4

Has anyone figured out how to get a role in yii2?

N
Nico_Bellic, 2017-03-24
@Nico_Bellic

RBAC returns an array of the user, check if the required role is in the array.

A
artursharipov, 2020-02-04
@artursharipov

You can get the role like this
User Model:

public function getRole()
{
     return array_values(Yii::$app->authManager->getRolesByUser($this->id))[0];
}

In view:
echo Yii::$app->user->identity->role->name
   /*echo Yii::$app->user->identity->role->description*/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question