removeChildren() public method

public removeChildren ( $parent )
コード例 #1
0
 /**
  * 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]);
 }
コード例 #2
0
ファイル: AuthItem.php プロジェクト: wayhood/yii2-rbac
 /**
  * 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;
 }
コード例 #3
0
ファイル: AssignModel.php プロジェクト: shuangjie/galaxy
 /**
  * assign permissions to roles
  */
 public function save()
 {
     $permissions = $this->permissions;
     $auth = new DbManager();
     $auth->init();
     $role = $auth->getRole($this->role_name);
     $auth->removeChildren($role);
     foreach ($this->_permissions as $key => $value) {
         if (isset($permissions[$key]) && is_array($permissions[$key])) {
             foreach ($permissions[$key] as $v) {
                 if ($key == $value[$v]) {
                     $auth->addChild($role, $auth->getPermission($key));
                 } else {
                     $auth->addChild($role, $auth->getPermission($key . '_' . $value[$v]));
                 }
             }
         }
     }
 }
コード例 #4
0
 /**
  * @inheritdoc
  */
 public function removeChildren($parent)
 {
     $result = parent::removeChildren($parent);
     if ($this->_children !== null) {
         unset($this->_children[$parent->name]);
     }
     $this->invalidate(self::PART_CHILDREN);
     return $result;
 }
コード例 #5
0
ファイル: Rbac.php プロジェクト: lenarx/yii2-cms-rbac
 public function afterDelete()
 {
     $rbac = new DbManager();
     $rbac->init();
     $role = $rbac->createRole($this->name);
     $role->description = $this->title;
     $rbac->remove($role);
     $rbac->removeChildren($role);
     return parent::afterDelete();
 }