Answer the question
In order to leave comments, you need to log in
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
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.
RBAC returns an array of the user, check if the required role is in the array.
You can get the role like this
User Model:
public function getRole()
{
return array_values(Yii::$app->authManager->getRolesByUser($this->id))[0];
}
echo Yii::$app->user->identity->role->name
/*echo Yii::$app->user->identity->role->description*/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question