2022-05-03 22:59:42 +02:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
class WorkflowsController extends AppController
{
public $components = array (
2022-05-04 10:44:18 +02:00
'RequestHandler'
2022-05-03 22:59:42 +02:00
);
2022-07-26 11:07:52 +02:00
private $toggleableFields = [ 'enabled' ];
2022-05-20 17:34:41 +02:00
public function beforeFilter ()
{
parent :: beforeFilter ();
2022-07-13 15:38:51 +02:00
$this -> Security -> unlockedActions [] = 'checkGraph' ;
2022-09-05 10:40:44 +02:00
$this -> Security -> unlockedActions [] = 'moduleStatelessExecution' ;
2022-06-13 10:48:59 +02:00
$requirementErrors = [];
if ( empty ( Configure :: read ( 'MISP.background_jobs' ))) {
$requirementErrors [] = __ ( 'Background workers must be enabled to use workflows' );
$this -> render ( 'error' );
}
2022-06-24 13:44:51 +02:00
if ( empty ( Configure :: read ( 'Plugin.Workflow_enable' ))) {
$requirementErrors [] = __ ( 'The workflow plugin must be enabled to use workflows. Go to `/servers/serverSettings/Plugin` the enable the `Plugin.Workflow` setting' );
$this -> render ( 'error' );
}
2022-05-24 14:11:49 +02:00
try {
2022-05-24 15:24:13 +02:00
$this -> Workflow -> setupRedisWithException ();
2022-05-24 14:11:49 +02:00
} catch ( Exception $e ) {
2022-06-13 10:48:59 +02:00
$requirementErrors [] = $e -> getMessage ();
}
if ( ! empty ( $requirementErrors )) {
$this -> set ( 'requirementErrors' , $requirementErrors );
2022-05-24 14:11:49 +02:00
$this -> render ( 'error' );
}
2022-05-20 17:34:41 +02:00
}
2022-05-03 22:59:42 +02:00
public function index ()
{
2022-05-04 00:01:02 +02:00
$params = [
'filters' => [ 'name' , 'uuid' ],
2022-05-04 10:44:18 +02:00
'quickFilters' => [ 'name' , 'uuid' ],
2022-05-04 00:01:02 +02:00
];
$this -> CRUD -> index ( $params );
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
}
$this -> set ( 'menuData' , array ( 'menuList' => 'workflows' , 'menuItem' => 'index' ));
2022-05-03 22:59:42 +02:00
}
2022-05-30 10:53:13 +02:00
public function rebuildRedis ()
{
2022-06-07 13:46:26 +02:00
$this -> Workflow -> rebuildRedis ();
2022-05-26 11:20:59 +02:00
}
2022-05-04 00:01:02 +02:00
public function edit ( $id )
2022-05-03 22:59:42 +02:00
{
2022-05-04 00:01:02 +02:00
$this -> set ( 'id' , $id );
2022-06-07 13:46:26 +02:00
$savedWorkflow = $this -> Workflow -> fetchWorkflow ( $id );
2022-05-04 13:54:55 +02:00
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
$newWorkflow = $this -> request -> data ;
2022-05-04 15:30:02 +02:00
$newWorkflow [ 'Workflow' ][ 'data' ] = JsonTool :: decode ( $newWorkflow [ 'Workflow' ][ 'data' ]);
2022-05-04 13:54:55 +02:00
$newWorkflow = $this -> __applyDataFromSavedWorkflow ( $newWorkflow , $savedWorkflow );
2022-07-25 13:24:15 +02:00
$result = $this -> Workflow -> editWorkflow ( $newWorkflow );
2022-05-04 13:54:55 +02:00
$redirectTarget = [ 'action' => 'view' , $id ];
2022-07-25 13:24:15 +02:00
if ( ! empty ( $result [ 'errors' ])) {
return $this -> __getFailResponseBasedOnContext ( $result [ 'errors' ], null , 'edit' , $this -> Workflow -> id , $redirectTarget );
2022-05-04 13:54:55 +02:00
} else {
$successMessage = __ ( 'Workflow saved.' );
2022-07-25 13:24:15 +02:00
$savedWorkflow = $result [ 'saved' ];
2023-05-03 14:56:38 +02:00
$savedWorkflow = $this -> Workflow -> attachLabelToConnections ( $savedWorkflow );
2022-05-04 13:54:55 +02:00
return $this -> __getSuccessResponseBasedOnContext ( $successMessage , $savedWorkflow , 'edit' , false , $redirectTarget );
}
} else {
2022-05-16 14:43:54 +02:00
$savedWorkflow [ 'Workflow' ][ 'data' ] = JsonTool :: encode ( $savedWorkflow [ 'Workflow' ][ 'data' ]);
2022-05-04 13:54:55 +02:00
$this -> request -> data = $savedWorkflow ;
2022-05-04 00:01:02 +02:00
}
2022-05-03 22:59:42 +02:00
2022-05-04 00:01:02 +02:00
$this -> set ( 'menuData' , array ( 'menuList' => 'workflows' , 'menuItem' => 'edit' ));
$this -> render ( 'add' );
2022-05-03 22:59:42 +02:00
}
2022-05-04 00:01:02 +02:00
public function delete ( $id )
2022-05-03 22:59:42 +02:00
{
2022-05-04 00:01:02 +02:00
$params = [
];
$this -> CRUD -> delete ( $id , $params );
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
}
}
2022-05-03 22:59:42 +02:00
2022-05-04 00:01:02 +02:00
public function view ( $id )
{
2022-07-13 10:42:43 +02:00
$filters = $this -> IndexFilter -> harvestParameters ([ 'format' ]);
if ( ! empty ( $filters [ 'format' ])) {
if ( $filters [ 'format' ] == 'dot' ) {
$dot = $this -> Workflow -> getDotNotation ( $id );
return $this -> RestResponse -> viewData ( $dot , $this -> response -> type ());
} else if ( $filters [ 'format' ] == 'mermaid' ) {
$mermaid = $this -> Workflow -> getMermaid ( $id );
return $this -> RestResponse -> viewData ( $mermaid , $this -> response -> type ());
}
}
2022-05-04 00:01:02 +02:00
$this -> CRUD -> view ( $id , [
2022-11-11 09:45:38 +01:00
'afterFind' => function ( $workflow ) {
return $this -> Workflow -> attachLabelToConnections ( $workflow );
}
2022-05-04 00:01:02 +02:00
]);
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
}
$this -> set ( 'id' , $id );
$this -> set ( 'menuData' , array ( 'menuList' => 'workflows' , 'menuItem' => 'view' ));
2022-05-03 22:59:42 +02:00
}
2022-08-03 16:05:29 +02:00
public function editor ( $id )
2022-05-03 22:59:42 +02:00
{
2022-08-03 16:05:29 +02:00
$trigger_id = false ;
$workflow = false ;
if ( is_numeric ( $id )) {
$workflow_id = $id ;
} else {
$trigger_id = $id ;
2022-06-07 13:46:26 +02:00
}
2022-08-03 16:05:29 +02:00
$modules = $this -> Workflow -> getModulesByType ();
if ( ! empty ( $trigger_id )) {
$trigger_ids = Hash :: extract ( $modules [ 'modules_trigger' ], '{n}.id' );
if ( ! in_array ( $trigger_id , $trigger_ids )) {
2022-06-07 13:46:26 +02:00
return $this -> __getFailResponseBasedOnContext (
2022-08-03 16:05:29 +02:00
[ __ ( 'Unkown trigger %s' , $trigger_id )],
2022-06-07 13:46:26 +02:00
null ,
'add' ,
$trigger_id ,
2022-08-03 16:05:29 +02:00
[ 'controller' => 'workflows' , 'action' => 'triggers' ]
2022-06-07 13:46:26 +02:00
);
}
2022-07-25 13:25:49 +02:00
$workflow = $this -> Workflow -> fetchWorkflowByTrigger ( $trigger_id , false );
2022-08-03 16:05:29 +02:00
if ( empty ( $workflow )) { // Workflow do not exists yet. Create it.
$result = $this -> Workflow -> addWorkflow ([
'name' => sprintf ( 'Workflow for trigger %s' , $trigger_id ),
'data' => $this -> Workflow -> genGraphDataForTrigger ( $trigger_id ),
'trigger_id' => $trigger_id ,
]);
if ( ! empty ( $result [ 'errors' ])) {
return $this -> __getFailResponseBasedOnContext (
[ __ ( 'Could not create workflow for trigger %s' , $trigger_id ), $result [ 'errors' ]],
null ,
'add' ,
$trigger_id ,
[ 'controller' => 'workflows' , 'action' => 'editor' ]
);
}
$workflow = $this -> Workflow -> fetchWorkflowByTrigger ( $trigger_id , false );
}
} else {
$workflow = $this -> Workflow -> fetchWorkflow ( $workflow_id );
2022-06-07 13:46:26 +02:00
}
2022-11-11 09:45:38 +01:00
$workflow = $this -> Workflow -> attachLabelToConnections ( $workflow , $trigger_id );
2022-06-07 13:46:26 +02:00
$modules = $this -> Workflow -> attachNotificationToModules ( $modules , $workflow );
2022-06-24 17:09:12 +02:00
$this -> loadModel ( 'WorkflowBlueprint' );
$workflowBlueprints = $this -> WorkflowBlueprint -> find ( 'all' );
2022-08-24 09:14:17 +02:00
$workflowBlueprints = array_map ( function ( $blueprint ) {
return $this -> WorkflowBlueprint -> attachModuleDataToBlueprint ( $blueprint );
}, $workflowBlueprints );
2022-05-05 18:02:04 +02:00
$this -> set ( 'selectedWorkflow' , $workflow );
2022-07-22 15:04:48 +02:00
$this -> set ( 'workflowTriggerId' , $trigger_id );
2022-05-04 00:01:02 +02:00
$this -> set ( 'modules' , $modules );
2022-06-24 17:09:12 +02:00
$this -> set ( 'workflowBlueprints' , $workflowBlueprints );
2022-05-04 00:01:02 +02:00
}
2022-08-03 16:05:29 +02:00
public function executeWorkflow ( $workflow_id )
{
2022-08-04 10:01:32 +02:00
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
$blockingErrors = [];
2023-10-25 16:20:39 +02:00
if ( ! JsonTool :: isValid ( $this -> request -> data [ 'Workflow' ][ 'data' ])) {
return $this -> RestResponse -> viewData ([
'success' => false ,
'outcome' => __ ( 'Invalid JSON' ),
], $this -> response -> type ());
}
2022-08-04 10:01:32 +02:00
$data = JsonTool :: decode ( $this -> request -> data [ 'Workflow' ][ 'data' ]);
$result = $this -> Workflow -> executeWorkflow ( $workflow_id , $data , $blockingErrors );
if ( ! empty ( $logging ) && empty ( $result [ 'success' ])) {
$logging [ 'message' ] = ! empty ( $logging [ 'message' ]) ? $logging [ 'message' ] : __ ( 'Error while executing workflow.' );
$errorMessage = implode ( ', ' , $blockingErrors );
$this -> Workflow -> loadLog () -> createLogEntry ( 'SYSTEM' , $logging [ 'action' ], $logging [ 'model' ], $logging [ 'id' ], $logging [ 'message' ], __ ( 'Returned message: %s' , $errorMessage ));
}
return $this -> RestResponse -> viewData ([
'success' => $result [ 'success' ],
'outcome' => $result [ 'outcomeText' ],
], $this -> response -> type ());
2022-08-03 16:05:29 +02:00
}
2022-08-04 10:01:32 +02:00
$this -> render ( 'ajax/executeWorkflow' );
2022-08-03 16:05:29 +02:00
}
2022-06-07 13:46:26 +02:00
public function triggers ()
{
$triggers = $this -> Workflow -> getModulesByType ( 'trigger' );
$triggers = $this -> Workflow -> attachWorkflowToTriggers ( $triggers );
2022-10-17 11:29:50 +02:00
$scopes = array_unique ( Hash :: extract ( $triggers , '{n}.scope' ));
sort ( $scopes );
$filters = $this -> IndexFilter -> harvestParameters ([ 'scope' , 'enabled' , 'blocking' ]);
if ( ! empty ( $filters [ 'scope' ])) {
$triggers = array_filter ( $triggers , function ( $trigger ) use ( $filters ) {
return $trigger [ 'scope' ] === $filters [ 'scope' ];
});
}
if ( isset ( $filters [ 'enabled' ])) {
$triggers = array_filter ( $triggers , function ( $trigger ) use ( $filters ) {
return $trigger [ 'disabled' ] != $filters [ 'enabled' ];
});
}
if ( isset ( $filters [ 'blocking' ])) {
$triggers = array_filter ( $triggers , function ( $trigger ) use ( $filters ) {
return $trigger [ 'blocking' ] == $filters [ 'blocking' ];
});
}
2022-07-26 14:44:24 +02:00
App :: uses ( 'CustomPaginationTool' , 'Tools' );
$customPagination = new CustomPaginationTool ();
2022-10-17 11:29:50 +02:00
$customPagination -> truncateAndPaginate ( $triggers , $this -> params , 'Workflow' , true );
2022-06-07 13:46:26 +02:00
if ( $this -> _isRest ()) {
2022-10-17 11:29:50 +02:00
return $this -> RestResponse -> viewData ( $triggers , $this -> response -> type ());
2022-06-07 13:46:26 +02:00
}
2022-07-25 11:37:31 +02:00
2022-10-17 11:29:50 +02:00
$this -> set ( 'data' , $triggers );
$this -> set ( 'scopes' , $scopes );
$this -> set ( 'filters' , $filters );
2022-06-07 13:46:26 +02:00
$this -> set ( 'menuData' , [ 'menuList' => 'workflows' , 'menuItem' => 'index_trigger' ]);
}
2022-06-01 08:22:26 +02:00
public function moduleIndex ()
2022-05-16 14:44:54 +02:00
{
2022-05-21 10:16:58 +02:00
$modules = $this -> Workflow -> getModulesByType ();
2022-07-19 09:01:32 +02:00
$errorWhileLoading = $this -> Workflow -> getModuleLoadingError ();
2022-06-01 11:49:51 +02:00
$this -> Module = ClassRegistry :: init ( 'Module' );
2022-06-07 13:46:26 +02:00
$mispModules = $this -> Module -> getModules ( 'Action' );
2022-06-01 11:49:51 +02:00
$this -> set ( 'module_service_error' , ! is_array ( $mispModules ));
2022-06-22 09:45:03 +02:00
$filters = $this -> IndexFilter -> harvestParameters ([ 'type' , 'actiontype' , 'enabled' ]);
2022-06-07 13:46:26 +02:00
$moduleType = $filters [ 'type' ] ? ? 'action' ;
2022-06-22 09:45:03 +02:00
$actionType = $filters [ 'actiontype' ] ? ? 'all' ;
$enabledState = $filters [ 'enabled' ] ? ? false ;
2022-07-07 09:04:09 +02:00
if ( $moduleType == 'all' || $moduleType == 'custom' ) {
2022-06-01 08:22:26 +02:00
$data = array_merge (
2022-07-26 11:32:15 +02:00
$modules [ " modules_action " ],
$modules [ " modules_logic " ]
2022-06-01 08:22:26 +02:00
);
} else {
2022-07-26 11:32:15 +02:00
$data = $modules [ " modules_ { $moduleType } " ];
2022-06-01 08:22:26 +02:00
}
2022-06-22 09:45:03 +02:00
if ( $actionType == 'mispmodule' ) {
$data = array_filter ( $data , function ( $module ) {
return ! empty ( $module [ 'is_misp_module' ]);
});
} else if ( $actionType == 'blocking' ) {
$data = array_filter ( $data , function ( $module ) {
2022-07-19 09:52:38 +02:00
return ! empty ( $module [ 'blocking' ]);
2022-06-22 09:45:03 +02:00
});
2022-07-07 09:04:09 +02:00
} else if ( $moduleType == 'custom' ) {
$data = array_filter ( $data , function ( $module ) {
return ! empty ( $module [ 'is_custom' ]);
});
2022-06-22 09:45:03 +02:00
}
if ( $enabledState !== false ) {
$moduleType = ! empty ( $enabledState ) ? 'enabled' : 'disabled' ;
$data = array_filter ( $data , function ( $module ) use ( $enabledState ) {
return ! empty ( $enabledState ) ? empty ( $module [ 'disabled' ]) : ! empty ( $module [ 'disabled' ]);
});
}
2022-06-01 09:24:20 +02:00
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $data , $this -> response -> type ());
}
2022-07-26 14:44:24 +02:00
App :: uses ( 'CustomPaginationTool' , 'Tools' );
$customPagination = new CustomPaginationTool ();
$params = $customPagination -> createPaginationRules ( $data , $this -> passedArgs , 'Workflow' );
$params = $customPagination -> applyRulesOnArray ( $data , $params , 'Workflow' );
$params [ 'options' ] = array_merge ( $params [ 'options' ], $filters );
$this -> params [ 'paging' ] = [ $this -> modelClass => $params ];
2022-06-01 08:22:26 +02:00
$this -> set ( 'data' , $data );
$this -> set ( 'indexType' , $moduleType );
2022-06-22 09:45:03 +02:00
$this -> set ( 'actionType' , $actionType );
2022-07-19 09:01:32 +02:00
$this -> set ( 'errorWhileLoading' , $errorWhileLoading );
2022-06-01 08:22:26 +02:00
$this -> set ( 'menuData' , [ 'menuList' => 'workflows' , 'menuItem' => 'index_module' ]);
2022-05-16 14:44:54 +02:00
}
2022-06-01 08:22:26 +02:00
public function moduleView ( $module_id )
2022-05-17 08:37:37 +02:00
{
2022-06-01 08:22:26 +02:00
$module = $this -> Workflow -> getModuleByID ( $module_id );
if ( empty ( $module )) {
2022-05-17 08:37:37 +02:00
throw new NotFoundException ( __ ( 'Invalid trigger ID' ));
}
2022-06-07 13:46:26 +02:00
$is_trigger = $module [ 'module_type' ] == 'trigger' ;
if ( $is_trigger ) {
$module = $this -> Workflow -> attachWorkflowToTriggers ([ $module ])[ 0 ];
2022-07-07 09:28:28 +02:00
$module [ 'listening_workflows' ] = $this -> Workflow -> getListeningWorkflowForTrigger ( $module );
2022-06-01 08:22:26 +02:00
}
2022-06-01 09:24:20 +02:00
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $module , $this -> response -> type ());
}
2023-03-31 04:48:02 +02:00
if ( ! isset ( $module [ 'Workflow' ]))
$module [ 'Workflow' ] = [ 'counter' => false , 'id' => false ];
2022-06-01 08:22:26 +02:00
$this -> set ( 'data' , $module );
$this -> set ( 'menuData' , [ 'menuList' => 'workflows' , 'menuItem' => 'view_module' ]);
2022-05-17 08:37:37 +02:00
}
2022-06-17 10:06:11 +02:00
public function toggleModule ( $module_id , $enabled , $is_trigger = false )
2022-05-30 10:10:46 +02:00
{
2022-06-17 09:20:27 +02:00
$this -> request -> allowMethod ([ 'post' , 'put' ]);
2022-06-17 10:06:11 +02:00
$saved = $this -> Workflow -> toggleModule ( $module_id , $enabled , $is_trigger );
2022-06-17 09:20:27 +02:00
if ( $saved ) {
return $this -> __getSuccessResponseBasedOnContext (
__ ( '%s module %s' , ( $enabled ? 'Enabled' : 'Disabled' ), $module_id ),
null ,
'toggle_module' ,
$module_id ,
2022-06-17 10:06:11 +02:00
[ 'action' => ( ! empty ( $is_trigger ) ? 'triggers' : 'moduleIndex' )]
2022-06-17 09:20:27 +02:00
);
} else {
return $this -> __getFailResponseBasedOnContext (
__ ( 'Could not %s module %s' , ( $enabled ? 'Enabled' : 'Disabled' ), $module_id ),
null ,
'toggle_module' ,
$module_id ,
2022-06-17 10:06:11 +02:00
[ 'action' => ( ! empty ( $is_trigger ) ? 'triggers' : 'moduleIndex' )]
2022-06-17 09:20:27 +02:00
);
2022-05-30 10:10:46 +02:00
}
}
2022-08-03 11:45:17 +02:00
public function debugToggleField ( $workflow_id , $enabled )
{
if ( ! $this -> request -> is ( 'ajax' )) {
throw new MethodNotAllowedException ( __ ( 'This action is available via AJAX only.' ));
}
$this -> layout = false ;
$this -> render ( 'ajax/getDebugToggleField' );
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
$success = $this -> Workflow -> toggleDebug ( $workflow_id , $enabled );
if ( ! empty ( $success )) {
return $this -> __getSuccessResponseBasedOnContext (
__ ( '%s debug mode' , ( $enabled ? __ ( 'Enabled' ) : __ ( 'Disabled' ))),
null ,
'toggle_debug' ,
$workflow_id ,
[ 'action' => 'triggers' ]
);
} else {
return $this -> __getFailResponseBasedOnContext (
__ ( 'Could not %s debug mode' , ( $enabled ? __ ( 'enable' ) : __ ( 'disable' ))),
null ,
'toggle_debug' ,
$workflow_id ,
[ 'action' => 'triggers' ]
);
}
}
}
2022-07-26 11:07:52 +02:00
public function massToggleField ( $fieldName , $enabled , $is_trigger = false )
{
if ( ! in_array ( $fieldName , $this -> toggleableFields )) {
throw new MethodNotAllowedException ( __ ( 'The field `%s` cannot be toggled' , $fieldName ));
}
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
$module_ids = JsonTool :: decode ( $this -> request -> data [ 'Workflow' ][ 'module_ids' ]);
$enabled_count = $this -> Workflow -> toggleModules ( $module_ids , $enabled , $is_trigger );
if ( ! empty ( $enabled_count )) {
return $this -> __getSuccessResponseBasedOnContext (
__ ( '%s %s modules' , ( $enabled ? 'Enabled' : 'Disabled' ), $enabled_count ),
null ,
'toggle_module' ,
$module_ids ,
[ 'action' => ( ! empty ( $is_trigger ) ? 'triggers' : 'moduleIndex' )]
);
} else {
return $this -> __getFailResponseBasedOnContext (
__ ( 'Could not %s modules' , ( $enabled ? 'enable' : 'disable' )),
null ,
'toggle_module' ,
$module_ids ,
[ 'action' => ( ! empty ( $is_trigger ) ? 'triggers' : 'moduleIndex' )]
);
}
}
}
2022-05-04 00:01:02 +02:00
private function __getSuccessResponseBasedOnContext ( $message , $data = null , $action = '' , $id = false , $redirect = array ())
{
if ( $this -> _isRest ()) {
if ( ! is_null ( $data )) {
return $this -> RestResponse -> viewData ( $data , $this -> response -> type ());
} else {
2022-05-04 13:54:55 +02:00
return $this -> RestResponse -> saveSuccessResponse ( 'Workflow' , $action , $id , false , $message );
2022-05-04 00:01:02 +02:00
}
} elseif ( $this -> request -> is ( 'ajax' )) {
2022-05-04 13:54:55 +02:00
return $this -> RestResponse -> saveSuccessResponse ( 'Workflow' , $action , $id , false , $message , $data );
2022-05-04 00:01:02 +02:00
} else {
$this -> Flash -> success ( $message );
$this -> redirect ( $redirect );
}
return ;
}
2022-05-03 22:59:42 +02:00
2022-05-04 00:01:02 +02:00
private function __getFailResponseBasedOnContext ( $message , $data = null , $action = '' , $id = false , $redirect = array ())
{
if ( is_array ( $message )) {
$message = implode ( ', ' , $message );
}
if ( $this -> _isRest ()) {
if ( $data !== null ) {
return $this -> RestResponse -> viewData ( $data , $this -> response -> type ());
} else {
2022-05-04 13:54:55 +02:00
return $this -> RestResponse -> saveFailResponse ( 'Workflow' , $action , $id , $message );
2022-05-04 00:01:02 +02:00
}
} elseif ( $this -> request -> is ( 'ajax' )) {
2022-05-04 13:54:55 +02:00
return $this -> RestResponse -> saveFailResponse ( 'Workflow' , $action , $id , $message , false , $data );
2022-05-04 00:01:02 +02:00
} else {
$this -> Flash -> error ( $message );
2022-06-07 13:46:26 +02:00
$this -> redirect ( $redirect );
2022-05-04 00:01:02 +02:00
}
2022-05-03 22:59:42 +02:00
}
2022-05-04 13:54:55 +02:00
private function __applyDataFromSavedWorkflow ( $newWorkflow , $savedWorkflow )
{
2022-07-25 14:09:29 +02:00
if ( ! isset ( $newWorkflow [ 'Workflow' ])) {
$newWorkflow = [ 'Workflow' => $newWorkflow ];
2022-05-04 13:54:55 +02:00
}
2022-06-07 13:46:26 +02:00
$ignoreFieldList = [ 'id' , 'uuid' ];
2022-07-25 13:24:15 +02:00
foreach ( Workflow :: CAPTURE_FIELDS_EDIT as $field ) {
2022-05-04 13:54:55 +02:00
if ( ! in_array ( $field , $ignoreFieldList ) && isset ( $newWorkflow [ 'Workflow' ][ $field ])) {
$savedWorkflow [ 'Workflow' ][ $field ] = $newWorkflow [ 'Workflow' ][ $field ];
}
}
return $savedWorkflow ;
}
2022-05-20 17:34:41 +02:00
2022-07-13 15:38:51 +02:00
public function checkGraph ()
2022-05-20 17:34:41 +02:00
{
$this -> request -> allowMethod ([ 'post' ]);
2022-06-21 10:01:28 +02:00
$graphData = JsonTool :: decode ( $this -> request -> data [ 'graph' ]);
2022-05-20 17:34:41 +02:00
$cycles = [];
$isAcyclic = $this -> Workflow -> workflowGraphTool -> isAcyclic ( $graphData , $cycles );
2022-07-22 13:28:56 +02:00
$edgesMultipleOutput = [];
$hasMultipleOutputConnection = $this -> Workflow -> workflowGraphTool -> hasMultipleOutputConnection ( $graphData , $edgesMultipleOutput );
$edgesWarnings = [];
$hasPathWarnings = $this -> Workflow -> hasPathWarnings ( $graphData , $edgesWarnings );
2022-05-20 17:34:41 +02:00
$data = [
2022-07-13 15:38:51 +02:00
'is_acyclic' => [
'is_acyclic' => $isAcyclic ,
'cycles' => $cycles ,
],
'multiple_output_connection' => [
'has_multiple_output_connection' => $hasMultipleOutputConnection ,
2022-07-22 13:28:56 +02:00
'edges' => $edgesMultipleOutput ,
],
'path_warnings' => [
'has_path_warnings' => $hasPathWarnings ,
'edges' => $edgesWarnings ,
],
2022-05-20 17:34:41 +02:00
];
return $this -> RestResponse -> viewData ( $data , 'json' );
}
2022-09-05 10:40:44 +02:00
public function moduleStatelessExecution ( $module_id )
{
$this -> request -> allowMethod ([ 'post' ]);
$input_data = JsonTool :: decode ( $this -> request -> data [ 'input_data' ]);
$param_data = $this -> request -> data [ 'module_indexed_param' ];
2022-10-17 14:35:39 +02:00
$convert_data = $this -> request -> data [ 'convert_data' ];
$result = $this -> Workflow -> moduleStatelessExecution ( $module_id , $input_data , $param_data , $convert_data );
2022-09-05 10:40:44 +02:00
return $this -> RestResponse -> viewData ( $result , 'json' );
}
2022-05-03 22:59:42 +02:00
}