To Get Role Of A User - Yii2

5.4k views Asked by At

I was searching for user role for logged in user, I'm not getting how to print user role name. I Tried this:

$Role = Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());

When, I did print_r($Role); I got this :

Array ( [superadmin] => yii\rbac\Role Object ( [type] => 1 [name] => superadmin [description] => Super admin can do any operation in the application [ruleName] => [data] => [createdAt] => [updatedAt] => ) )

I was looking to get particular role name, but i was unable to access

Array ( [superadmin] => yii\rbac\Role Object ........)
           ^ unable to access this name.

When typing print_r($Role[0]->name); I'm getting error like

PHP Notice – yii\base\ErrorException

Undefined offset: 0

And, If I do like this way (means, manually passing $rolename to array index) It is working.

$roleName = 'superadmin';

print_r($Role[$roleName]->name);

Why this requirement came to me, because for logged in user It's Ok. But, if i want to know other user role name, then I need that index name to pass here $Role[$roleName]->name

Please help me to access this 'superadmin'. I'm not getting how to get index name of it.

Array ( [superadmin] => yii\rbac\Role Object
               ^ unable to access this name.

I also checked Get User Role & Specific Role Of User

5

There are 5 answers

3
Nuriddin Rashidov On BEST ANSWER

So you can use for this array_shift. It's return first element of array E.g:

if(is_array($Role))array_shift($Role)->name;
2
arogachev On

User can have multiple roles, and in authManager there is no method for getting just one role for user.

You can use this code in case of one role (I'd recommend to place it in a User model to keep code cleaner):

/**
 * Returns user role name according to RBAC
 * @return string
 */
public function getRoleName()
{
    $roles = Yii::$app->authManager->getRolesByUser($this->id);
    if (!$roles) {
        return null;
    }

    reset($roles);
    /* @var $role \yii\rbac\Role */
    $role = current($roles);

    return $role->name;
}

No need to check for array because getRolesByUser already returns an array.

Alternatively you can use array_shift as suggested here or return a key of array element because it's indexed by names (described here).

Because of such indexation, you can't get 0 element of an array (it simply don't exist). That's why you got Undefined offset: 0 exception.

Example of usage in view:

<?php if (!Yii::$app->user->isGuest) { ?>
    <div class="user-role"><?= Yii::$app->user->identity->getRoleName() ?></div>
<?php } ?>
4
Clyff On

Your array doesn't have the 0 index. As the documentation says the array is indexed by the role names.

So, if you have 100% all users will always have only one role. You could call it like this:

print_r(reset($Role));

But if the user can have multiple roles, you can use a loop for that:

foreach ($Role as $each) {
    print_r($each);
}
0
xPeng On
$getRolesByUser = Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());

$Role = array_keys($getRolesByUser)[0];
0
Francesco D'Agostino On

in my opinion the best way is:

if (array_key_exists('Admin', \Yii::$app->authManager->getRolesByUser(Yii::$app->user->id))) { ... }

PS the above for the Role "Admin".