I’m trying to create code to add a Role to a user
$auth = Yii::$app->authManager;
$role = $auth->getRole('User');
$auth->assign($role, $this->id);
This works fine.
But I also need code to remove a Role or all Roles and can’t seem to get anything to actually work.
$auth = Yii::$app->authManager;
$role = $auth->getRole('User');
$auth->revoke($role, $this->id);
$auth = Yii::$app->authManager;
$auth->revokeAll($this->id);
Neither work, what am I doing wrong?
This is part of my user model beforeSave/afterSave.
public function beforeSave($insert)
{
$auth = Yii::$app->authManager;
if (!parent::beforeSave($insert)) {
return false;
}
// if (!$this->isNewRecord) {
// }
if (!$insert) {
if ($this->status == self::STATUS_DELETED) {
// Deactivating the user
$role = $auth->getRole('User');
$auth->revoke($role, $this->id);
}
}
return true;
}
I should also note that I’m using GitHub - mdmsoft/yii2-admin: Auth manager for Yii2 (RBAC Manager) if that changes anything.
What I don’t get is even if I remove the conditions, so it should always run, the role is never revoked?!