/**
  * 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]);
 }
Example #2
0
 /**
  * Saves item.
  *
  * @return bool
  */
 public function save()
 {
     if ($this->validate() == false) {
         return false;
     }
     if ($isNewItem = $this->item === null) {
         $this->item = $this->createItem($this->name);
     } else {
         $oldName = $this->item->name;
     }
     $this->item->name = $this->name;
     $this->item->description = $this->description;
     if (!empty($this->rule)) {
         $rule = \Yii::createObject($this->rule);
         if (null === $this->manager->getRule($rule->name)) {
             $this->manager->add($rule);
         }
         $this->item->ruleName = $rule->name;
     } else {
         $this->item->ruleName = null;
     }
     $createdFlashMessage = '';
     $updatedFlashMessage = '';
     if ($this->item->type = Item::TYPE_PERMISSION) {
         $createdFlashMessage = Yii::t('rbac', 'Permission has been created');
         $updatedFlashMessage = Yii::t('rbac', 'Permission has been updated');
     } else {
         if ($this->item->type = Item::TYPE_ROLE) {
             $createdFlashMessage = Yii::t('rbac', 'Role has been updated');
             $updatedFlashMessage = Yii::t('rbac', 'Role has been updated');
         }
     }
     if ($isNewItem) {
         \Yii::$app->session->setFlash('success', $createdFlashMessage);
         $this->manager->add($this->item);
     } else {
         \Yii::$app->session->setFlash('success', $updatedFlashMessage);
         $this->manager->update($oldName, $this->item);
     }
     $this->manager->removeChildren($this->item);
     if (is_array($this->children)) {
         foreach ($this->children as $name) {
             if ($this->item->type = Item::TYPE_PERMISSION) {
                 $child = $this->manager->getPermission($name);
             } else {
                 if ($this->item->type = Item::TYPE_ROLE) {
                     $child = $this->manager->getRole($name);
                 }
             }
             if ($this->manager->hasChild($this->item, $child) == false) {
                 $this->manager->addChild($this->item, $child);
             }
         }
     }
     return true;
 }
Example #3
0
 public function afterSave($insert, $changedAttributes)
 {
     $rbac = new DbManager();
     $rbac->init();
     $role = $rbac->createRole($this->name);
     $role->description = $this->title;
     if ($insert) {
         if ($rbac->add($role)) {
             if ($this->rights) {
                 $this->rights = unserialize($this->rights);
                 if (is_array($this->rights) && count($this->rights)) {
                     foreach ($this->rights as $key => $child) {
                         if (intval($child)) {
                             $child = $rbac->createPermission($key);
                             if (!$rbac->getPermission($key)) {
                                 $rbac->add($child);
                             }
                             $rbac->addChild($role, $child);
                         }
                     }
                 }
             }
         }
     } else {
         if ($rbac->update($this->name, $role)) {
             $rbac->removeChildren($role);
             if ($this->rights) {
                 $this->rights = unserialize($this->rights);
                 if (is_array($this->rights) && count($this->rights)) {
                     foreach ($this->rights as $key => $child) {
                         if (intval($child)) {
                             $child = $rbac->createPermission($key);
                             if (!$rbac->getPermission($key)) {
                                 $rbac->add($child);
                             }
                             $rbac->addChild($role, $child);
                         }
                     }
                 }
             }
         }
     }
     return parent::afterSave($insert, $changedAttributes);
 }