/** * Akce vracející pravidla pro vykreslení v easymineru * @param int $id * @param int $miner * @param string $task * @param string $rule * @throws BadRequestException * @throws ForbiddenRequestException */ public function renderRuleDetails($id = null, $miner, $rule) { $rule = $this->rulesFacade->findRule($rule); //kontrola přístupů $task = $rule->task; $minerId = $miner; $miner = $task->miner; if ($miner->minerId != $minerId || $task->taskId != $id) { throw new ForbiddenRequestException($this->translator->translate('You are not authorized to access selected data!')); } $this->checkMinerAccess($miner); $this->template->rule = $rule; }
/** * Akce pro odebrání celého obsahu rule clipboard z konkrétního rulesetu * @param int $id * @param int $miner * @param int $ruleset * @param string $returnRules ='' - IDčka oddělená čárkami, případně jedno ID * @throws ForbiddenRequestException * @throws BadRequestException */ public function actionRemoveRulesFromRuleSet($id = null, $miner, $ruleset, $returnRules = '') { //načtení dané úlohy a zkontrolování přístupu k mineru $task = $this->tasksFacade->findTask($id); $this->checkMinerAccess($task->miner); $ruleIdsArr = explode(',', str_replace(';', ',', $returnRules)); //najití RuleSetu a kontroly $ruleSet = $this->ruleSetsFacade->findRuleSet($ruleset); $this->ruleSetsFacade->checkRuleSetAccess($ruleSet, $this->user->id); //přidání pravidel $this->ruleSetsFacade->removeAllRuleClipboardRulesFromRuleSet($task, $ruleSet); $result = array(); if (count($ruleIdsArr) > 0) { foreach ($ruleIdsArr as $ruleId) { try { $rule = $this->rulesFacade->findRule($ruleId); //TODO optimalizovat kontroly... $ruleTask = $rule->task; if ($ruleTask->taskId != $task->taskId) { throw new InvalidArgumentException(); } if ($ruleTask->miner->minerId != $miner) { throw new InvalidArgumentException(); } $result[$rule->ruleId] = $rule->getBasicDataArr(); } catch (\Exception $e) { continue; } } } $this->sendJsonResponse(array('rules' => $result)); }
/** * Funkce připravující pole s informacemi o vybraných pravidlech pro vrácení v rámci JSON odpovědi * @param int[] $ruleIdsArr * @param RuleSet|int $ruleSet * @return array */ private function prepareRulesResult($ruleIdsArr, $ruleSet = null) { $result = []; if (!empty($ruleIdsArr)) { foreach ($ruleIdsArr as $ruleId) { if (!$ruleId) { continue; } try { $rule = $this->rulesFacade->findRule($ruleId); $result[$rule->ruleId] = $rule->getBasicDataArr(); if (!empty($ruleSet)) { $relation = $rule->getRuleSetRelation($ruleSet); if (!empty($relation)) { $result[$rule->ruleId]['ruleSetRelation'] = $relation->relation; } else { $result[$rule->ruleId]['ruleSetRelation'] = ''; } } } catch (\Exception $e) { continue; } } } return $result; }
/** * Funkce pro nalezení pravidla dle zadaného ID a kontrolu oprávnění aktuálního uživatele pracovat s daným pravidlem * @param int $ruleId * @return RuleSet * @throws \Nette\Application\BadRequestException */ private function findRuleWithCheckAccess($ruleId) { try { /** @var Rule $rule */ $rule = $this->rulesFacade->findRule($ruleId); } catch (EntityNotFoundException $e) { $this->error('Requested rule was not found.'); return null; } //TODO kontrola oprávnění return $rule; }
/** * Funkce pro přidání/změnu relace Rule k RuleSet * @param Rule|int $rule * @param RuleSet|int $ruleSet * @param string $relation * @param bool $updateRulesCount = true * @return bool * @throws \Exception */ public function addRuleToRuleSet($rule, $ruleSet, $relation, $updateRulesCount = true) { if (!$rule instanceof Rule) { $rule = $this->rulesFacade->findRule($rule); } if (!$ruleSet instanceof RuleSet) { $ruleSet = $this->ruleSetsRepository->find($ruleSet); } try { $ruleSetRuleRelation = $this->ruleSetRuleRelationsRepository->findBy(['rule_id' => $rule->ruleId, 'rule_set_id' => $ruleSet->ruleSetId]); } catch (\Exception $e) { $ruleSetRuleRelation = new RuleSetRuleRelation(); $ruleSetRuleRelation->rule = $rule; $ruleSetRuleRelation->ruleSet = $ruleSet; } $ruleSetRuleRelation->relation = $relation; $result = $this->ruleSetRuleRelationsRepository->persist($ruleSetRuleRelation); if ($updateRulesCount) { $this->updateRuleSetRulesCount($ruleSet); } return $result; }
/** * Akce pro odebrání pravidel z rulesetu * @param int $id * @param int|string $rules * @throws \Nette\Application\BadRequestException * @SWG\Delete( * tags={"RuleSets"}, * path="/rule-sets/{id}/rules", * summary="Remove rules from the selected rule set", * security={{"apiKey":{}},{"apiKeyHeader":{}}}, * produces={"application/json","application/xml"}, * @SWG\Parameter( * name="id", * description="RuleSet ID", * required=true, * type="integer", * in="path" * ), * @SWG\Parameter( * name="rules", * description="IDs of rules (optinally multiple - separated with , or ;)", * required=true, * type="array", * @SWG\items( * type="integer" * ), * collectionFormat="csv", * in="query" * ), * @SWG\Response( * response=200, * description="Rules have been removed from the rule set.", * @SWG\Schema(ref="#/definitions/StatusResponse"),examples={"code":200,"status":"OK"} * ), * @SWG\Response(response=404, description="Requested rule set was not found.") * ) */ public function actionDeleteRules($id, $rules) { /** @var RuleSet $ruleSet */ $ruleSet = $this->findRuleSetWithCheckAccess($id); /** @var int[] $ruleIdsArr */ $ruleIdsArr = explode(',', str_replace(';', ',', $rules)); if (!empty($ruleIdsArr)) { foreach ($ruleIdsArr as $ruleId) { if (!$ruleId) { continue; } try { $rule = $this->rulesFacade->findRule($ruleId); $this->ruleSetsFacade->removeRuleFromRuleSet($rule, $ruleSet); } catch (\Exception $e) { continue; } } } $this->ruleSetsFacade->updateRuleSetRulesCount($ruleSet); $this->resource = ['code' => 200, 'status' => 'ok']; $this->sendResource(); }