From e75bc1b0b000c48af62141fea89ccc64db4b38f3 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Mon, 30 May 2022 15:09:25 +0200 Subject: [PATCH] chg: [event:publish] Publishing execute `publish` trigger --- app/Model/Event.php | 54 +++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/app/Model/Event.php b/app/Model/Event.php index 1973a9d29..8fc81dfaa 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -4486,24 +4486,46 @@ class Event extends AppModel if (empty($event)) { return false; } - $hostOrg = $this->Org->getHostOrg(); - $user = [ + $hostOrg = $this->Org->find('first', [ + 'recursive' => -1, + 'conditions' => [ + 'id' => Configure::read('MISP.host_org_id') + ], + ]); + if (empty($hostOrg)) { + $hostOrg = $this->Org->find('first', [ + 'recursive' => -1, + 'order' => ['id ASC'] + ]); + } + $userForPubSub = [ 'id' => 0, 'org_id' => $hostOrg['Org']['id'], - 'Role' => array('perm_sync' => 0, 'perm_audit' => 0, 'perm_site_admin' => 0), + 'Role' => ['perm_sync' => 0, 'perm_audit' => 0, 'perm_site_admin' => 0], 'Organisation' => $hostOrg['Org'] ]; - $fullEvent = null; - if (Configure::read('Plugin.Action_services_enable')) { - $fullEvent = $this->fetchEvent($user, [ - 'eventid' => $id, - 'includeAttachments' => 1 - ]); - $this->Module = ClassRegistry::init('Module'); - $error = null; - if (!$this->Module->executeActions('publish', $user, $fullEvent, ['model' => 'Event', 'id' => $id], $error)) { - return $error; - } + $currentUserId = Configure::read('CurrentUserId'); + $tmpUser = $this->User->find('first', [ + 'recursive' => -1, + 'conditions' => [ + 'User.id' => $currentUserId, + ], + 'contain' => ['Organisation', 'Role'], + ]); + $userForWorkflow = array_merge($tmpUser['User'], [ + 'Role' => $tmpUser['Role'], + 'Organisation' => $tmpUser['Organisation'], + ]); + $fullEvent = $this->fetchEvent($userForWorkflow, [ + 'eventid' => $id, + 'includeAttachments' => 1 + ]); + $errors = []; + $success = $this->executeTrigger('publish', $fullEvent, $errors); + if (empty($success)) { + $errorMessage = implode(', ', $errors); + $this->loadLog()->createLogEntry('SYSTEM', 'warning', 'Event', $id, __('Publishing stopped by a blocking workflow.'), __('Returned message: %s', $errorMessage)); + return $errorMessage; } if ($jobId) { $this->Behaviors->unload('SysLogLogable.SysLogLogable'); @@ -4519,14 +4541,14 @@ class Event extends AppModel } $kafkaTopic = Configure::read('Plugin.Kafka_event_publish_notifications_topic'); if (Configure::read('Plugin.ZeroMQ_enable')) { - $this->publishEventToZmq($id, $user, $fullEvent); + $this->publishEventToZmq($id, $userForPubSub, $fullEvent); } if ( Configure::read('Plugin.Kafka_enable') && Configure::read('Plugin.Kafka_event_publish_notifications_enable') && !empty($kafkaTopic) ) { - $this->publishEventToKafka($id, $user, $fullEvent, $kafkaTopic); + $this->publishEventToKafka($id, $userForPubSub, $fullEvent, $kafkaTopic); } return $this->uploadEventToServersRouter($id, $passAlong); }