How to change/update user role? Yii2

7.2k views Asked by At

I'm using yii2 role based access control (RBAC) system. I defined roles and permissions, defined rules, assigned roles to users. Everything works fine.

but i want to update assigned roles to users. For example:

Yii::$app->authManager->assign('admin', '11');

Here i did user id 11 admin. Now i want to update and change. I want to make this user manager.

When i try:

Yii::$app->authManager->assign('manager', '11');

This user have admin + manager. I just want to update this user role admin to manager.

How can i do this?

1

There are 1 answers

0
jithin On

first you revoke() or revokeAll() user role:

Remove current role 'admin':

$manager = Yii::$app->authManager;
$item = $manager->getRole('admin');
$item = $item ? : $manager->getPermission('admin');
$manager->revoke($item,'11');

and again update new role to user:

$authorRole = $manager->getRole('manager');
$manager->assign($authorRole, 11);