/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = AuthItem::model()->with('parents.parentItem')->findByPk($id);
     $operationsList = $tasksList = $listModels = array();
     if ($model->type == AuthItem::TYPE_TASK) {
         $operations = AuthItem::model()->findAll('type = :type', array(':type' => AuthItem::TYPE_OPERATION));
         foreach ($operations as $op) {
             $listModels[$op->name] = $op->description . "({$op->name})";
         }
     } elseif ($model->type == AuthItem::TYPE_ROLE) {
         $tasks = AuthItem::model()->findAll('type = :type', array(':type' => AuthItem::TYPE_TASK));
         foreach ($tasks as $task) {
             $listModels[$task->name] = $task->description . "({$task->name})";
         }
     }
     foreach ($model->parents as $item) {
         if ($item->childItem->type == AuthItem::TYPE_OPERATION) {
             $operationsList[$item->childItem->name] = $item->childItem->description . " ({$item->childItem->name})";
         } elseif ($item->childItem->type == AuthItem::TYPE_TASK) {
             $tasksList[$item->childItem->name] = $item->childItem->description . " ({$item->childItem->name})";
         }
     }
     if (Yii::app()->request->isPostRequest && isset($_POST['AuthItem'])) {
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $model->attributes = $_POST['AuthItem'];
             if ($model->save()) {
                 $items = array();
                 if ($model->type == AuthItem::TYPE_TASK) {
                     $items = Yii::app()->request->getPost('operations', array());
                 } elseif ($model->type == AuthItem::TYPE_ROLE) {
                     $items = Yii::app()->request->getPost('tasks', array());
                 }
                 // удалим и создадим чайлдов заново
                 AuthItemChild::model()->deleteAll('parent = :parent', array(':parent' => $model->name));
                 if (count($items)) {
                     foreach ($items as $name) {
                         $child = new AuthItemChild();
                         $child->setAttributes(array('parent' => $model->name, 'child' => $name));
                         if (!$child->save()) {
                             throw new CException('Ошибка при сохранении связанных объектов!');
                         }
                     }
                 }
                 $transaction->commit();
                 Yii::app()->user->setFlash('success', 'Действие изменено!');
                 $this->redirect(array('update', 'id' => $model->name));
             }
         } catch (Exception $e) {
             Yii::app()->user->setFlash('error', $e->getMessage());
             $transaction->rollback();
         }
     }
     $this->render('update', array('model' => $model, 'operations' => $operationsList, 'tasks' => $tasksList, 'listModels' => $listModels));
 }
 public function actionCreate()
 {
     $parent = $this->getItem();
     $item = new AuthItem();
     $item_child = new AuthItemChild();
     if (isset($_POST['AuthItem'])) {
         $item->attributes = $_POST['AuthItem'];
         $item_child->attributes = $_POST['AuthItemChild'];
         $item_child->child = $item->name;
         if ($item->validate() && $item_child->validate()) {
             $item->save(false);
             $item_child->save(false);
             $this->redirect(array('view', 'name' => $item->name));
         }
     }
     $this->render('create', array('parent' => $parent, 'item' => $item, 'item_child' => $item_child));
 }
 public function actionManage()
 {
     if (isset($_POST['AuthItem'])) {
         $items_names = array();
         foreach ($_POST['AuthItem'] as $task_name => $data) {
             p($_POST['AuthItem']);
             die;
             if (!isset($data['description'])) {
                 continue;
             }
             $items_names[] = $task_name;
             if (!isset($data['operations'])) {
                 continue;
             }
             foreach ($data['operations'] as $name => $description) {
                 $items_names[] = $name;
             }
         }
         $items_names = array_map(function ($v) {
             return "'{$v}'";
         }, $items_names);
         $items_names = implode(',', $items_names);
         AuthItem::model()->delete("name NOT IN ({$items_names})");
         foreach ($_POST['AuthItem'] as $task_name => $data) {
             if (!isset($data['description'])) {
                 continue;
             }
             $task = AuthItem::model()->findByPk($task_name);
             if (!$task) {
                 $task = new AuthItem();
                 $task->type = CAuthItem::TYPE_TASK;
                 $task->name = $task_name;
                 $task->description = $data['description'];
             }
             if ($task->save() && isset($data['operations'])) {
                 foreach ($data['operations'] as $name => $description) {
                     $operation = AuthItem::model()->findByPk($name);
                     if (!$operation) {
                         $operation = new AuthItem();
                         $operation->type = CAuthItem::TYPE_OPERATION;
                         $operation->name = $name;
                         $operation->description = $description;
                     }
                     if ($operation->save()) {
                         $auth_item_child = AuthItemChild::model()->findByAttributes(array('parent' => $task->name, 'child' => $operation->name));
                         if (!$auth_item_child) {
                             $auth_item_child = new AuthItemChild();
                             $auth_item_child->parent = $task->name;
                             $auth_item_child->child = $operation->name;
                             $auth_item_child->save();
                         }
                     }
                 }
             }
         }
     }
     $auth_items = array();
     $tasks = $this->getModulesTasks();
     foreach ($tasks as $task) {
         $auth_items[] = array('id' => $task['name'], 'name' => $task['name'], 'exists' => $task['exists'], 'description' => $task['description']);
         if (isset($task['operations'])) {
             foreach ($task['operations'] as $operation) {
                 $operation['parent'] = $task['name'];
                 $operation['id'] = $operation['name'];
                 $auth_items[] = $operation;
             }
         }
     }
     $data_provider = new CArrayDataProvider($auth_items, array('pagination' => false));
     $this->render('manage', array('data_provider' => $data_provider, 'tasks' => $this->getModulesTasks()));
 }
Esempio n. 4
0
 public function actionImport()
 {
     $modulesList = [];
     $modules = [];
     foreach (Yii::app()->getModules() as $key => $value) {
         $key = strtolower($key);
         $module = Yii::app()->getModule($key);
         if ($module instanceof \yupe\components\WebModule) {
             $modulesList[$key] = $module->getName();
             $modules[$key] = $module;
         }
     }
     if (Yii::app()->getRequest()->isPostRequest) {
         $importModules = array_intersect(Yii::app()->getRequest()->getPost('modules', []), array_keys($modules));
         foreach ($importModules as $moduleName) {
             /* @var $module \yupe\components\WebModule */
             $module = $modules[$moduleName];
             $rules = $module->getAuthItems();
             // 1 - получить все элементы из дерева
             $items = $this->getRulesList($rules);
             $parentsChildren = $this->getRulesParentsAndChildren($items);
             // обновляем
             foreach ($items as $item) {
                 $model = AuthItem::model()->findByPk($item['name']);
                 if (!$model) {
                     $model = new AuthItem();
                 }
                 $model->attributes = $item;
                 $model->save();
             }
             // удаляем удаленные из модуля
             // оставшиеся
             $availableItems = array_map(function ($x) {
                 return $x['name'];
             }, $items);
             /* удаляем правила */
             $criteria = new CDbCriteria();
             $criteria->addCondition('name like :rule');
             $criteria->params = [':rule' => ucfirst($moduleName) . '.%'];
             $criteria->addNotInCondition('name', $availableItems);
             AuthItem::model()->deleteAll($criteria);
             /* создаем связи */
             foreach ($parentsChildren as $pair) {
                 $model = AuthItemChild::model()->findByPk(['parent' => $pair['parent'], 'child' => $pair['child']]);
                 if (!$model) {
                     $model = new AuthItemChild();
                     $model->attributes = $pair;
                     $model->save();
                 }
             }
         }
         Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('RbacModule.rbac', 'Items successfully imported!'));
         $this->redirect(['import']);
     }
     $this->render('import', ['modules' => $modulesList]);
 }