/**
* Update role using string name.
*
* @param string $id
* @return mixed
* @throws NotFoundHttpException
*/
public function actionUpdate($id)
{
$this->layout = '@app/views/layouts/one-column';
$role = $this->findRole($id);
$model = RoleForm::createFromRole($role, $this->authManager->getChildren($role->name));
/* @var $systemAlert Alert */
$systemAlert = Yii::$app->systemAlert;
if (Yii::$app->request->isAjax && $model->load($_POST)) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load($_POST) && $model->validate()) {
$transaction = Yii::$app->db->beginTransaction();
try {
// update role description
$role->description = $model->description;
if (!$this->authManager->update($role->name, $role)) {
throw new Exception();
}
// update role permissions
$this->authManager->removeChildren($role);
foreach ($model->getPermissionModels() as $permission) {
$this->authManager->addChild($role, $permission);
}
$transaction->commit();
$systemAlert->setMessage(Alert::SUCCESS, Yii::t('user', 'Role updated successfully'));
return $this->redirect(['index']);
} catch (Exception $ex) {
$transaction->rollback();
$systemAlert->setMessage(Alert::DANGER, Yii::t('app', 'System error: {message}', ['message' => $ex->getMessage()]));
}
}
return $this->render('update', ['model' => $model]);
}