Answer the question
In order to leave comments, you need to log in
User role at registration (RBAC). How to do it right?
There is a Yii2 Advanced Template. For the convenience of working with RBAC, the yii2-admin module was taken . authManager is defined as:
'authManager' => [
'class' => 'yii\rbac\DbManager',
]
'defaultRole' => 'User'
. ...
if ($user->save()) {
$auth = Yii::$app->authManager;
$defaultRole = $auth->getRole(Yii::$app->params['defaultRole']);
$auth->assign($defaultRole, $user->getId());
return $user;
}
Answer the question
In order to leave comments, you need to log in
1) I won't say for that.
2) Yes, but for reinsurance, you can make it still a transaction, in case the user was added, but the rights were not added for some reason. after successfully adding a user, we add rights and, if an error occurs, then rollback the transaction.
3)
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
if($insert) {
$auth = Yii::$app->authManager;
$role = $auth->getRole(self::ROLE_USER);
$auth->assign($role, $this->id);
}
Yii::$app->authManager->invalidateCache();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question