在我的yii2 basic应用程序中,我使用RBAC根据用户的角色为他们分配模块。
我将角色id和user_id存储在auth_assignment表中。
现在,如果我在更新期间更改用户的角色。我还必须在auth_assignment表中更改它。现在,我希望从auth赋值中删除该用户的所有条目,并在表中添加新条目。
问题是我找不到任何RBAC函数来更新auth_assignment表数据或删除auth赋值表数据。
Yii2文档removeAllAssignments()
中有一个函数,但它截断了整个表,我只想删除perticular用户的条目。
有什么可用的功能吗?
发布于 2016-09-28 02:04:51
发布于 2021-09-28 03:37:27
假设角色名称是从表单发送的,并且将值存储在“角色”字段中,然后将其添加到模型中。
这将删除现有的辅助,并分配新的辅助。
public function afterSave($isInsert, $changedOldAttributes)
{
// Update the user role in the RBAC layer
// Purge the user tokens when the password is changed
if (array_key_exists('role', $changedOldAttributes)) {
$auth = Yii::$app->authManager;
$auth->revokeAll($this->id );
$role = $auth->getRole($this->role);
$auth->assign($role, $this->id);
}
return parent::afterSave($isInsert, $changedOldAttributes);
}
https://stackoverflow.com/questions/39744128
复制