From 8f3f7bc8661088a21b671bdf1cefe6c7948ce596 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Wed, 16 Nov 2022 16:14:36 +0100 Subject: [PATCH] chg: [internal] Speedup sighting rest search --- app/Model/Sighting.php | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/app/Model/Sighting.php b/app/Model/Sighting.php index f33e149c0..d513095ba 100644 --- a/app/Model/Sighting.php +++ b/app/Model/Sighting.php @@ -382,18 +382,33 @@ class Sighting extends AppModel return ['Sighting.attribute_id' => array_column(array_column($attributes, 'Attribute'), 'id')]; } - $hostOrgId = Configure::read('MISP.host_org_id'); + // Merge attributes by Event ID $userOrgId = $user['org_id']; - $conditions = []; + $attributesByEventId = []; foreach ($attributes as $attribute) { - $attributeConditions = ['Sighting.attribute_id' => $attribute['Attribute']['id']]; - $ownEvent = $attribute['Event']['org_id'] == $userOrgId; - if (!$ownEvent) { + $eventId = $attribute['Event']['id']; + if (isset($attributesByEventId[$eventId])) { + $attributesByEventId[$eventId]['ids'][] = $attribute['Attribute']['id']; + } else { + $ownEvent = $attribute['Event']['org_id'] == $userOrgId; + $attributesByEventId[$eventId] = [ + 'ids' => [$attribute['Attribute']['id']], + 'ownEvent' => $ownEvent, + ]; + } + } + + // Create conditions for merged attributes + $hostOrgId = Configure::read('MISP.host_org_id'); + $conditions = []; + foreach ($attributesByEventId as $eventId => $eventAttributes) { + $attributeConditions = ['Sighting.attribute_id' => $eventAttributes['ids']]; + if (!$eventAttributes['ownEvent']) { if ($sightingsPolicy === self::SIGHTING_POLICY_EVENT_OWNER) { $attributeConditions['Sighting.org_id'] = $userOrgId; } else if ($sightingsPolicy === self::SIGHTING_POLICY_SIGHTING_REPORTER) { - if (!$this->isReporter($attribute['Event']['id'], $userOrgId)) { - continue; // skip attribute + if (!$this->isReporter($eventId, $userOrgId)) { + continue; // skip event } } else if ($sightingsPolicy === self::SIGHTING_POLICY_HOST_ORG) { $attributeConditions['Sighting.org_id'] = [$userOrgId, $hostOrgId];