Merge pull request #8653 from JakubOnderka/workflow-fixes

fix: [workflow] Basic cleanup
pull/8682/head
Jakub Onderka 2022-10-18 21:20:40 +02:00 committed by GitHub
commit aae65c42c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 39 deletions

View File

@ -53,6 +53,7 @@ class AuditLogsController extends AppController
'News',
'Warninglist',
'Workflow',
'WorkflowBlueprint',
];
public $paginate = [

View File

@ -1,6 +1,9 @@
<?php
App::uses('AppController', 'Controller');
/**
* @property WorkflowBlueprint $WorkflowBlueprint
*/
class WorkflowBlueprintsController extends AppController
{
public $components = array(
@ -14,10 +17,9 @@ class WorkflowBlueprintsController extends AppController
$message = __('Default workflow blueprints updated');
if ($this->_isRest()) {
return $this->RestResponse->saveSuccessResponse('WorkflowBlueprint', 'update', false, $this->response->type(), $message);
} else {
$this->Flash->success($message);
$this->redirect(array('controller' => 'workflowBlueprints', 'action' => 'index'));
}
$this->Flash->success($message);
$this->redirect(array('controller' => 'workflowBlueprints', 'action' => 'index'));
}
public function index()
@ -69,9 +71,7 @@ class WorkflowBlueprintsController extends AppController
public function delete($id)
{
$params = [
];
$this->CRUD->delete($id, $params);
$this->CRUD->delete($id);
if ($this->IndexFilter->isRest()) {
return $this->restResponsePayload;
}
@ -82,16 +82,15 @@ class WorkflowBlueprintsController extends AppController
{
$filters = $this->IndexFilter->harvestParameters(['format']);
if (!empty($filters['format'])) {
if ($filters['format'] == 'dot') {
if ($filters['format'] === 'dot') {
$dot = $this->WorkflowBlueprint->getDotNotation($id);
return $this->RestResponse->viewData($dot, $this->response->type());
} else if ($filters['format'] == 'mermaid') {
} else if ($filters['format'] === 'mermaid') {
$mermaid = $this->WorkflowBlueprint->getMermaid($id);
return $this->RestResponse->viewData($mermaid, $this->response->type());
}
}
$this->CRUD->view($id, [
]);
$this->CRUD->view($id);
if ($this->IndexFilter->isRest()) {
return $this->restResponsePayload;
}
@ -102,11 +101,8 @@ class WorkflowBlueprintsController extends AppController
public function import()
{
if ($this->request->is('post') || $this->request->is('put')) {
$workflowBlueprintData = JsonTool::decode($this->request->data['WorkflowBlueprint']['data']);
if ($workflowBlueprintData === null) {
throw new MethodNotAllowedException(__('Error while decoding JSON'));
}
$this->request->data['WorkflowBlueprint']['data'] = JsonTool::encode($workflowBlueprintData);
$workflowBlueprintData = $this->_jsonDecode($this->request->data['WorkflowBlueprint']['json']);
$this->request->data = $workflowBlueprintData;
$this->add();
}
}
@ -118,7 +114,11 @@ class WorkflowBlueprintsController extends AppController
'id' => $id,
]
]);
$content = JsonTool::encode($workflowBlueprint, JSON_PRETTY_PRINT);
if (empty($workflowBlueprint)) {
throw new NotFoundException(__('Invalid workflow blueprint'));
}
$content = JsonTool::encode($workflowBlueprint, true);
$this->response->body($content);
$this->response->type('json');
$this->response->download(sprintf('blueprint_%s_%s.json', str_replace(' ', '-', strtolower($workflowBlueprint['WorkflowBlueprint']['name'])), time()));

View File

@ -3735,7 +3735,7 @@ class AppModel extends Model
* @param array $logging If the execution failure should be logged
* @return boolean If the execution for the blocking path was a success
*/
public function executeTrigger($trigger_id, array $data=[], array &$blockingErrors=[], array $logging=[]): bool
protected function executeTrigger($trigger_id, array $data=[], array &$blockingErrors=[], array $logging=[]): bool
{
if ($this->isTriggerCallable($trigger_id)) {
$success = $this->Workflow->executeWorkflowForTriggerRouter($trigger_id, $data, $blockingErrors, $logging);
@ -3749,8 +3749,17 @@ class AppModel extends Model
return true;
}
public function isTriggerCallable($trigger_id): bool
protected function isTriggerCallable($trigger_id): bool
{
static $workflowEnabled;
if ($workflowEnabled === null) {
$workflowEnabled = (bool)Configure::read('Plugin.Workflow_enable');
}
if (!$workflowEnabled) {
return false;
}
if ($this->Workflow === null) {
$this->Workflow = ClassRegistry::init('Workflow');
}

View File

@ -515,14 +515,16 @@ class Attribute extends AppModel
$kafkaPubTool = $this->getKafkaPubTool();
$kafkaPubTool->publishJson($kafkaTopic, $attributeForPublish, $action);
}
$workflowErrors = [];
$logging = [
if ($isTriggerCallable) {
$workflowErrors = [];
$logging = [
'model' => 'Attribute',
'action' => $action,
'id' => $attributeForPublish['Attribute']['id'],
];
$triggerData = $attributeForPublish;
$this->executeTrigger('attribute-after-save', $triggerData, $workflowErrors, $logging);
$triggerData = $attributeForPublish;
$this->executeTrigger('attribute-after-save', $triggerData, $workflowErrors, $logging);
}
}
}
if ($created && isset($attribute['event_id']) && empty($attribute['skip_auto_increment'])) {

View File

@ -1,7 +1,6 @@
<?php
App::uses('AppModel', 'Model');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
class WorkflowBlueprint extends AppModel
{
@ -18,10 +17,9 @@ class WorkflowBlueprint extends AppModel
],
];
public $belongsTo = [
];
public $validate = [
'name' => 'stringNotEmpty',
'value' => [
'stringNotEmpty' => [
'rule' => ['stringNotEmpty']
@ -92,11 +90,11 @@ class WorkflowBlueprint extends AppModel
return $blueprint;
}
/**
* __readBlueprintsFromRepo Reads blueprints from the misp-workflow-blueprints repository
*
* @return array
* @throws Exception
*/
private function __readBlueprintsFromRepo(): array
{
@ -114,6 +112,7 @@ class WorkflowBlueprint extends AppModel
*
* @param boolean $force
* @return void
* @throws Exception
*/
public function update($force=false)
{
@ -133,7 +132,6 @@ class WorkflowBlueprint extends AppModel
if ($force || $blueprint_from_repo['timestamp'] > $existing_blueprint['timestamp']) {
$blueprint_from_repo['id'] = $existing_blueprint['id'];
$this->save($blueprint_from_repo);
continue;
}
} else {
$this->create();

View File

@ -1664,13 +1664,13 @@ $divider = $this->element('/genericElements/SideMenu/side_menu_divider');
if ($isSiteAdmin) {
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'element_id' => 'edit',
'url' => '/workflows/edit/' . h($id),
'url' => '/workflowBlueprints/edit/' . h($id),
'text' => __('Edit Workflow Blueprint')
));
}
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'url' => '/admin/audit_logs/index/model:WorkflowBlueprints/model_id:' . h($id),
'text' => __('View worflow blueprint history'),
'url' => '/admin/audit_logs/index/model:WorkflowBlueprint/model_id:' . h($id),
'text' => __('View workflow blueprint history'),
'requirement' => Configure::read('MISP.log_new_audit') && $canAccess('auditLogs', 'admin_index'),
));
}

View File

@ -5,7 +5,7 @@ echo $this->element('genericElements/Form/genericForm', [
'enctype' => 'multipart/form-data',
],
'data' => [
'model' => 'Workflow',
'model' => 'WorkflowBlueprint',
'title' => __('Import Workflow Blueprint'),
'description' => __('Paste a JSON of a Workflow blueprint to import it or provide a JSON file below.'),
'fields' => [

View File

@ -32,10 +32,10 @@
'name' => __('Timestamp'),
'sort' => 'WorkflowBlueprint.timestamp',
'data_path' => 'WorkflowBlueprint.timestamp',
'element' => 'datetime',
],
];
echo $this->element('genericElements/IndexTable/scaffold', [
'scaffold_data' => [
'data' => [
@ -73,11 +73,13 @@
'url_params_data_paths' => ['WorkflowBlueprint.id'],
'icon' => 'eye',
'dbclickAction' => true,
'title' => __('View'),
],
[
'url' => $baseurl . '/workflowBlueprints/edit',
'url_params_data_paths' => ['WorkflowBlueprint.id'],
'icon' => 'edit',
'title' => __('Edit'),
],
[
'url' => $baseurl . '/workflowBlueprints/export',
@ -86,13 +88,11 @@
'icon' => 'download',
],
[
'onclick' => sprintf(
'openGenericModal(\'%s/workflowBlueprints/delete/[onclick_params_data_path]\');',
$baseurl
),
'onclick_params_data_path' => 'WorkflowBlueprint.id',
'icon' => 'trash'
'class' => 'modal-open',
'url' => $baseurl . '/workflowBlueprints/delete/',
'url_params_data_paths' => 'WorkflowBlueprint.id',
'icon' => 'trash',
'title' => __('Delete'),
]
]
]