How to get user role in Yii2?

2020-02-25 08:12发布

How to get user role in Yii2?

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

标签: php yii yii2 rbac
8条回答
Emotional °昔
2楼-- · 2020-02-25 08:36

I use :

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

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

查看更多
仙女界的扛把子
3楼-- · 2020-02-25 08:36

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.

查看更多
登录 后发表回答