Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change/update user role? Yii2

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?

like image 953
devugur Avatar asked Oct 19 '22 08:10

devugur


1 Answers

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);
like image 179
jithin Avatar answered Oct 21 '22 22:10

jithin