mirror of https://github.com/MISP/MISP
chg: [workflow] added `addWorkflow` function in model
parent
5829f18db8
commit
ab26b27268
|
@ -57,13 +57,13 @@ class WorkflowsController extends AppController
|
|||
$newWorkflow = $this->request->data;
|
||||
$newWorkflow['Workflow']['data'] = JsonTool::decode($newWorkflow['Workflow']['data']);
|
||||
$newWorkflow = $this->__applyDataFromSavedWorkflow($newWorkflow, $savedWorkflow);
|
||||
$errors = $this->Workflow->editWorkflow($newWorkflow);
|
||||
$result = $this->Workflow->editWorkflow($newWorkflow);
|
||||
$redirectTarget = ['action' => 'view', $id];
|
||||
if (!empty($errors)) {
|
||||
return $this->__getFailResponseBasedOnContext($errors, null, 'edit', $this->Workflow->id, $redirectTarget);
|
||||
if (!empty($result['errors'])) {
|
||||
return $this->__getFailResponseBasedOnContext($result['errors'], null, 'edit', $this->Workflow->id, $redirectTarget);
|
||||
} else {
|
||||
$successMessage = __('Workflow saved.');
|
||||
$savedWorkflow =$this->Workflow->fetchWorkflow($id);
|
||||
$savedWorkflow = $result['saved'];
|
||||
return $this->__getSuccessResponseBasedOnContext($successMessage, $savedWorkflow, 'edit', false, $redirectTarget);
|
||||
}
|
||||
} else {
|
||||
|
@ -121,21 +121,21 @@ class WorkflowsController extends AppController
|
|||
}
|
||||
$workflow = $this->Workflow->fetchWorkflowByTrigger($trigger_id, false);
|
||||
if (empty($workflow)) { // Workflow do not exists yet. Create it.
|
||||
$this->Workflow->create();
|
||||
$savedWorkflow = $this->Workflow->save([
|
||||
$result = $this->Workflow->addWorkflow([
|
||||
'name' => sprintf('Workflow for trigger %s', $trigger_id),
|
||||
'data' => $this->Workflow->genGraphDataForTrigger($trigger_id),
|
||||
'trigger_id' => $trigger_id,
|
||||
]);
|
||||
if (empty($savedWorkflow)) {
|
||||
if (!empty($result['errors'])) {
|
||||
return $this->__getFailResponseBasedOnContext(
|
||||
[__('Could not create workflow for trigger %s', $trigger_id), $this->Workflow->validationErrors],
|
||||
[__('Could not create workflow for trigger %s', $trigger_id), $result['errors']],
|
||||
null,
|
||||
'add',
|
||||
$trigger_id,
|
||||
['controller' => 'workflows', 'action' => 'editor']
|
||||
);
|
||||
}
|
||||
$workflow = $savedWorkflow;
|
||||
$workflow = $result['saved'];
|
||||
}
|
||||
$modules = $this->Workflow->attachNotificationToModules($modules, $workflow);
|
||||
$this->loadModel('WorkflowBlueprint');
|
||||
|
@ -314,7 +314,7 @@ class WorkflowsController extends AppController
|
|||
$newReport = ['Workflow' => $newWorkflow];
|
||||
}
|
||||
$ignoreFieldList = ['id', 'uuid'];
|
||||
foreach (Workflow::CAPTURE_FIELDS as $field) {
|
||||
foreach (Workflow::CAPTURE_FIELDS_EDIT as $field) {
|
||||
if (!in_array($field, $ignoreFieldList) && isset($newWorkflow['Workflow'][$field])) {
|
||||
$savedWorkflow['Workflow'][$field] = $newWorkflow['Workflow'][$field];
|
||||
}
|
||||
|
|
|
@ -65,7 +65,8 @@ class Workflow extends AppModel
|
|||
|
||||
private $module_initialized = false;
|
||||
|
||||
const CAPTURE_FIELDS = ['name', 'description', 'timestamp', 'data'];
|
||||
const CAPTURE_FIELDS_EDIT = ['name', 'description', 'timestamp', 'data'];
|
||||
const CAPTURE_FIELDS_ADD = ['uuid', 'name', 'description', 'timestamp', 'data', 'trigger_id'];
|
||||
|
||||
const MODULE_ROOT_PATH = APP . 'Model/WorkflowModules/';
|
||||
const CUSTOM_MODULE_ROOT_PATH = APP . 'Lib/WorkflowModules/';
|
||||
|
@ -1055,15 +1056,32 @@ class Workflow extends AppModel
|
|||
return $workflow[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* addWorkflow Add a worflow
|
||||
*
|
||||
* @param array $trigger
|
||||
* @return array Any errors preventing the edition
|
||||
*/
|
||||
public function addWorkflow(array $workflow): array
|
||||
{
|
||||
$errors = [];
|
||||
$this->create();
|
||||
$saved = $this->__saveAndReturnErrors($workflow, ['fieldList' => self::CAPTURE_FIELDS_ADD], $errors);
|
||||
return [
|
||||
'saved' => $saved,
|
||||
'errors' => $errors,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* editWorkflow Edit a worflow
|
||||
*
|
||||
* @param array $workflow
|
||||
* @return array Any errors preventing the edition
|
||||
*/
|
||||
public function editWorkflow(array $workflow)
|
||||
public function editWorkflow(array $workflow): array
|
||||
{
|
||||
$errors = array();
|
||||
$errors = [];
|
||||
if (!isset($workflow['Workflow']['uuid'])) {
|
||||
$errors[] = __('Workflow doesn\'t have an UUID');
|
||||
return $errors;
|
||||
|
@ -1071,8 +1089,38 @@ class Workflow extends AppModel
|
|||
$existingWorkflow = $this->fetchWorkflow($workflow['Workflow']['id']);
|
||||
$workflow['Workflow']['id'] = $existingWorkflow['Workflow']['id'];
|
||||
unset($workflow['Workflow']['timestamp']);
|
||||
$errors = $this->__saveAndReturnErrors($workflow, ['fieldList' => self::CAPTURE_FIELDS], $errors);
|
||||
return $errors;
|
||||
$saved = $this->__saveAndReturnErrors($workflow, ['fieldList' => self::CAPTURE_FIELDS_EDIT], $errors);
|
||||
return [
|
||||
'saved' => $saved,
|
||||
'errors' => $errors,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* genGraphDataForTrigger Generate fake graph data under the drawflow format
|
||||
*
|
||||
* @param string $trigger_id
|
||||
* @return array
|
||||
*/
|
||||
public function genGraphDataForTrigger($trigger_id): array
|
||||
{
|
||||
if (empty($this->loaded_modules['trigger'][$trigger_id])) {
|
||||
throw new TriggerNotFoundException(__('Unknown trigger `%s`', $trigger_id));
|
||||
}
|
||||
$module_config = $this->loaded_modules['trigger'][$trigger_id];
|
||||
$data = [
|
||||
1 => [
|
||||
'class' => 'block-type-trigger',
|
||||
'data' => $module_config,
|
||||
'id' => 1,
|
||||
'inputs' => [],
|
||||
'outputs' => [],
|
||||
'pos_x' => 0,
|
||||
'pos_y' => 0,
|
||||
'typenode' => false,
|
||||
]
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1109,15 +1157,19 @@ class Workflow extends AppModel
|
|||
return !empty($edges);
|
||||
}
|
||||
|
||||
private function __saveAndReturnErrors($data, $saveOptions = [], $errors = [])
|
||||
private function __saveAndReturnErrors($data, $saveOptions = [], &$errors = [])
|
||||
{
|
||||
$saveSuccess = $this->save($data, $saveOptions);
|
||||
if (!$saveSuccess) {
|
||||
foreach ($this->validationErrors as $validationError) {
|
||||
$errors[] = $validationError[0];
|
||||
}
|
||||
} else {
|
||||
if (!empty($saveSuccess['Workflow']['data'])) {
|
||||
$saveSuccess['Workflow']['data'] = JsonTool::decode($saveSuccess['Workflow']['data']);
|
||||
}
|
||||
}
|
||||
return $errors;
|
||||
return $saveSuccess;
|
||||
}
|
||||
|
||||
public function getDotNotation($id)
|
||||
|
|
Loading…
Reference in New Issue