new: [CLI] Added cleanup tool to purge all events related to a feed

- Simply run /var/www/MISP/app/Console/cake Admin purgeFeedEvents [user_id] [feed_id]
- works for CSV/Freetext feeds
pull/5158/head
iglocska 2019-09-13 10:48:43 +02:00
parent fdf1379f3f
commit 3240a6cac4
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
3 changed files with 69 additions and 1 deletions

View File

@ -2,7 +2,7 @@
App::uses('AppShell', 'Console/Command');
class AdminShell extends AppShell
{
public $uses = array('Event', 'Post', 'Attribute', 'Job', 'User', 'Task', 'Whitelist', 'Server', 'Organisation', 'AdminSetting', 'Galaxy', 'Taxonomy', 'Warninglist', 'Noticelist', 'ObjectTemplate', 'Bruteforce', 'Role');
public $uses = array('Event', 'Post', 'Attribute', 'Job', 'User', 'Task', 'Whitelist', 'Server', 'Organisation', 'AdminSetting', 'Galaxy', 'Taxonomy', 'Warninglist', 'Noticelist', 'ObjectTemplate', 'Bruteforce', 'Role', 'Feed');
public function jobGenerateCorrelation() {
$jobId = $this->args[0];
@ -532,4 +532,23 @@ class AdminShell extends AppShell
$this->User->resetAllSyncAuthKeys($user, $jobId);
}
}
public function purgeFeedEvents()
{
if (
(empty($this->args[0]) || !is_numeric($this->args[0])) ||
(empty($this->args[1]) || !is_numeric($this->args[1]))
) {
echo 'Usage: ' . APP . '/cake ' . 'Admin purgeFeedEvents [user_id] [feed_id]' . PHP_EOL;
} else {
$user_id = $this->args[0];
$feed_id = $this->args[1];
$result = $this->Feed->cleanupFeedEvents($user_id, $feed_id);
if (is_string($result)) {
echo __("\nError: %s\n", $result);
} else {
echo __("%s events purged.\n", $result);
}
}
}
}

View File

@ -1642,4 +1642,52 @@ class Feed extends AppModel
$message .= "\nStack Trace:\n" . $exception->getTraceAsString();
return $message;
}
/**
* remove all events tied to a feed. Returns int on success, error message
* as string on failure
*/
public function cleanupFeedEvents($user_id, $id)
{
$feed = $this->find('first', array(
'conditions' => array('Feed.id' => $id),
'recursive' => -1
));
if (empty($feed)) {
return __('Invalid feed id.');
}
if (!in_array($feed['Feed']['source_format'], array('csv', 'freetext'))) {
return __('Feed has to be either a CSV or a freetext feed for the purging to work.');
}
$this->User = ClassRegistry::init('User');
$user = $this->User->getAuthUser($user_id);
if (empty($user)) {
return __('Invalid user id.');
}
$conditions = array('Event.info' => $feed['Feed']['name'] . ' feed');
$this->Event = ClassRegistry::init('Event');
$events = $this->Event->find('list', array(
'conditions' => $conditions,
'fields' => array('Event.id', 'Event.id')
));
$count = count($events);
foreach ($events as $event_id) {
$this->Event->delete($event_id);
}
$this->Log = ClassRegistry::init('Log');
$this->Log->create();
$this->Log->save(array(
'org' => 'SYSTEM',
'model' => 'Feed',
'model_id' => $id,
'email' => $user['email'],
'action' => 'purge_events',
'title' => __('Events related to feed %s purged.', $id),
'change' => null,
));
$feed['Feed']['fixed_event'] = 1;
$feed['Feed']['event_id'] = 0;
$this->save($feed);
return $count;
}
}

View File

@ -43,6 +43,7 @@ class Log extends AppModel
'publish',
'publish alert',
'pull',
'purge_events',
'push',
'remove_dead_workers',
'request',