diff --git a/app/Model/Event.php b/app/Model/Event.php index fd01406ad..f575ba131 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -3641,6 +3641,7 @@ class Event extends AppModel $this->OrgBlocklist = ClassRegistry::init('OrgBlocklist'); } if ($this->OrgBlocklist->isBlocked($orgc)) { + $this->OrgBlocklist->saveEventBlocked($orgc); return 'blocked'; } } diff --git a/app/Model/OrgBlocklist.php b/app/Model/OrgBlocklist.php index 531917390..ba69e1940 100644 --- a/app/Model/OrgBlocklist.php +++ b/app/Model/OrgBlocklist.php @@ -43,6 +43,24 @@ class OrgBlocklist extends AppModel return true; } + public function afterDelete() + { + parent::afterDelete(); + if (!empty($this->data['OrgBlocklist']['org_uuid'])) { + $this->cleanupBlockedCount($this->data['OrgBlocklist']['org_uuid']); + } + } + + public function afterFind($results, $primary = false) + { + foreach ($results as $k => $result) { + if (isset($result['OrgBlocklist']['org_uuid'])) { + $results[$k]['OrgBlocklist']['blocked_data'] = $this->getBlockedData($result['OrgBlocklist']['org_uuid']); + } + } + return $results; + } + /** * @param array $eventArray */ @@ -74,16 +92,7 @@ class OrgBlocklist extends AppModel } if (is_numeric($orgIdOrUuid)) { - $this->Organisation = ClassRegistry::init('Organisation'); - $orgUuid = $this->Organisation->find('first', [ - 'conditions' => ['Organisation.id' => $orgIdOrUuid], - 'fields' => ['Organisation.uuid'], - 'recursive' => -1, - ]); - if (empty($orgUuid)) { - return false; // org not found by ID, so it is not blocked - } - $orgUuid = $orgUuid['Organisation']['uuid']; + $orgUuid = $this->getUUIDFromID($orgIdOrUuid); } else { $orgUuid = $orgIdOrUuid; } @@ -92,4 +101,67 @@ class OrgBlocklist extends AppModel $this->blockedCache[$orgIdOrUuid] = $isBlocked; return $isBlocked; } + + private function getUUIDFromID($orgID) + { + $this->Organisation = ClassRegistry::init('Organisation'); + $orgUuid = $this->Organisation->find('first', [ + 'conditions' => ['Organisation.id' => $orgID], + 'fields' => ['Organisation.uuid'], + 'recursive' => -1, + ]); + if (empty($orgUuid)) { + return false; // org not found by ID, so it is not blocked + } + $orgUuid = $orgUuid['Organisation']['uuid']; + return $orgUuid; + } + + public function saveEventBlocked($orgIdOrUUID) + { + if (is_numeric($orgIdOrUUID)) { + $orgcUUID = $this->getUUIDFromID($orgIdOrUUID); + } else { + $orgcUUID = $orgIdOrUUID; + } + $lastBlockTime = time(); + $redisKeyBlockAmount = "misp:blocklist_blocked_amount:{$orgcUUID}"; + $redisKeyBlockLastTime = "misp:blocklist_blocked_last_time:{$orgcUUID}"; + $redis = RedisTool::init(); + if ($redis !== false) { + $pipe = $redis->multi(Redis::PIPELINE) + ->incr($redisKeyBlockAmount) + ->set($redisKeyBlockLastTime, $lastBlockTime); + $pipe->exec(); + } + } + + private function cleanupBlockedCount($orgcUUID) + { + $redisKeyBlockAmount = "misp:blocklist_blocked_amount:{$orgcUUID}"; + $redisKeyBlockLastTime = "misp:blocklist_blocked_last_time:{$orgcUUID}"; + $redis = RedisTool::init(); + if ($redis !== false) { + $pipe = $redis->multi(Redis::PIPELINE) + ->del($redisKeyBlockAmount) + ->del($redisKeyBlockLastTime); + $pipe->exec(); + } + } + + public function getBlockedData($orgcUUID) + { + $redisKeyBlockAmount = "misp:blocklist_blocked_amount:{$orgcUUID}"; + $redisKeyBlockLastTime = "misp:blocklist_blocked_last_time:{$orgcUUID}"; + $blockData = [ + 'blocked_amount' => false, + 'blocked_last_time' => false, + ]; + $redis = RedisTool::init(); + if ($redis !== false) { + $blockData['blocked_amount'] = $redis->get($redisKeyBlockAmount); + $blockData['blocked_last_time'] = $redis->get($redisKeyBlockLastTime); + } + return $blockData; + } } diff --git a/app/View/OrgBlocklists/index.ctp b/app/View/OrgBlocklists/index.ctp index b00767d59..62d2a9dbb 100644 --- a/app/View/OrgBlocklists/index.ctp +++ b/app/View/OrgBlocklists/index.ctp @@ -32,6 +32,17 @@ echo $this->element('genericElements/IndexTable/scaffold', [ 'data_path' => 'OrgBlocklist.comment', 'class' => 'bitwider' ], + [ + 'name' => 'Blocked amount', + 'sort' => 'OrgBlocklist.blocked_data.blocked_amount', + 'data_path' => 'OrgBlocklist.blocked_data.blocked_amount', + ], + [ + 'name' => 'Blocked last time ', + 'sort' => 'OrgBlocklist.blocked_data.blocked_last_time', + 'data_path' => 'OrgBlocklist.blocked_data.blocked_last_time', + 'element' => 'datetime' + ], ], 'title' => empty($ajax) ? __('Organisation Blocklists') : false,