公告
财富商城
积分规则
提问
发文
2020-02-25 08:12发布
倾城 Initia
How to get user role in Yii2?
I searched and read Yii2 guide but I didn't find any solution.
You can get Roles for an user by using getRolesByUser function
You can use it like this
\Yii::$app->authManager->getRolesByUser($user_id);
You can use :
$user =[]; $userAssigned = Yii::$app->authManager->getAssignments(user_id); foreach($userAssigned as $userAssign){ $user[] = $userAssign->roleName; }
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
You can use:
Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());
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.
Yii::$app->getUser()->identity->role;
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
最多设置5个标签!
You can get Roles for an user by using getRolesByUser function
You can use it like this
You can use :
The good and more visual decision will be setting constants of all roles.
You can use:
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.
If you're using amnah/yii2-user module, you can use this:
It will give you the current user role name