From 8fc85f95b99938ea0665cb123014338cfd2512b9 Mon Sep 17 00:00:00 2001 From: iglocska Date: Tue, 25 Feb 2014 12:27:20 +0100 Subject: [PATCH] Fixes with the synchronisation - background pulls fixed - now correctly logs changes - now correctly updates attributes --- app/Console/Command/ServerShell.php | 2 +- app/Model/Event.php | 32 +++++++++++++++++++---------- app/Model/Server.php | 16 +++++++++------ app/files/empty | 0 4 files changed, 32 insertions(+), 18 deletions(-) delete mode 100755 app/files/empty diff --git a/app/Console/Command/ServerShell.php b/app/Console/Command/ServerShell.php index 7db02baa8..146096138 100644 --- a/app/Console/Command/ServerShell.php +++ b/app/Console/Command/ServerShell.php @@ -16,7 +16,7 @@ class ServerShell extends AppShell $this->Job->read(null, $jobId); $this->Server->id = $serverId; $this->User->recursive = -1; - $user = $this->User->read(array('id', 'org', 'email'), $userId); + $user = $this->User->read(null, $userId); $server = $this->Server->read(null, $serverId); $result = $this->Server->pull($user['User'], null, $technique, $server, $jobId); $this->Job->id = $jobId; diff --git a/app/Model/Event.php b/app/Model/Event.php index 12d442f3d..e459606c5 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -1340,7 +1340,10 @@ class Event extends AppModel { * * @return bool true if success */ - public function _add(&$data, $fromXml, $user, $or='', $passAlong = null, $fromPull = false) { + public function _add(&$data, $fromXml, $user, $or='', $passAlong = null, $fromPull = false, $jobId = null) { + if ($jobId) { + App::import('Component','Auth'); + } $this->create(); // force check userid and orgname to be from yourself $data['Event']['user_id'] = $user['id']; @@ -1381,7 +1384,6 @@ class Event extends AppModel { 'Event' => array('org', 'orgc', 'date', 'threat_level_id', 'analysis', 'info', 'user_id', 'published', 'uuid', 'timestamp', 'distribution', 'locked'), 'Attribute' => array('event_id', 'category', 'type', 'value', 'value1', 'value2', 'to_ids', 'uuid', 'revision', 'timestamp', 'distribution') ); - $saveResult = $this->saveAssociated($data, array('validate' => true, 'fieldList' => $fieldList, 'atomic' => true)); // FIXME chri: check if output of $saveResult is what we expect when data not valid, see issue #104 @@ -1400,7 +1402,10 @@ class Event extends AppModel { } } - public function _edit(&$data, $id) { + public function _edit(&$data, $id, $jobId = null) { + if ($jobId) { + App::import('Component','Auth'); + } $localEvent = $this->find('first', array('conditions' => array('Event.id' => $id), 'recursive' => -1, 'contain' => array('Attribute', 'ThreatLevel', 'ShadowAttribute'))); if (!isset ($data['Event']['orgc'])) $data['Event']['orgc'] = $data['Event']['org']; if ($localEvent['Event']['timestamp'] < $data['Event']['timestamp']) { @@ -1413,7 +1418,7 @@ class Event extends AppModel { } $fieldList = array( 'Event' => array('date', 'threat_level_id', 'analysis', 'info', 'published', 'uuid', 'from', 'distribution', 'timestamp'), - 'Attribute' => array('event_id', 'category', 'type', 'value', 'value1', 'value2', 'to_ids', 'uuid', 'revision', 'distribution', 'timestamp') + 'Attribute' => array('event_id', 'category', 'type', 'value', 'value1', 'value2', 'to_ids', 'uuid', 'distribution', 'timestamp') ); $data['Event']['id'] = $localEvent['Event']['id']; if (isset($data['Event']['Attribute'])) { @@ -1425,18 +1430,24 @@ class Event extends AppModel { // If yes, it means that it's newer, so insert it. If no, it means that it's the same attribute or older - don't insert it, insert the old attribute. // Alternatively, we could unset this attribute from the request, but that could lead with issues if we decide that we want to start deleting attributes that don't exist in a pushed event. if ($data['Event']['Attribute'][$k]['timestamp'] > $existingAttribute['Attribute']['timestamp']) { - + $data['Event']['Attribute'][$k]['id'] = $existingAttribute['Attribute']['id']; + $data['Attribute'][] = $data['Event']['Attribute'][$k]; + unset($data['Event']['Attribute'][$k]); } else { unset($data['Event']['Attribute'][$k]); } - } else { + } else { unset($data['Event']['Attribute'][$k]['id']); - } - } - } + $data['Attribute'][] = $data['Event']['Attribute'][$k]; + unset($data['Event']['Attribute'][$k]); + } + } + } $this->cleanupEventArrayFromXML($data); $saveResult = $this->saveAssociated($data, array('validate' => true, 'fieldList' => $fieldList)); - if ($saveResult) return 'success'; + if ($saveResult) { + return 'success'; + } else return 'Saving the event has failed.'; } @@ -1744,5 +1755,4 @@ class Event extends AppModel { if (empty($localEvent) || $incomingEvent['timestamp'] > $localEvent['Event']['timestamp']) return true; return false; } - } diff --git a/app/Model/Server.php b/app/Model/Server.php index 376d5070d..11e065e14 100755 --- a/app/Model/Server.php +++ b/app/Model/Server.php @@ -109,11 +109,14 @@ class Server extends AppModel { } public function pull($user, $id = null, $technique=false, $server, $jobId = false, $percent = 100, $current = 0) { - $eventModel = ClassRegistry::init('Event'); if ($jobId) { $job = ClassRegistry::init('Job'); $job->read(null, $jobId); + App::import('Component','Auth'); + $this->Auth = new AuthComponent(new ComponentCollection()); + $this->Auth->login($user); } + $eventModel = ClassRegistry::init('Event'); App::uses('HttpSocket', 'Network/Http'); $eventIds = array(); if ("full" == $technique) { @@ -225,13 +228,13 @@ class Server extends AppModel { if (!$existingEvent) { // add data for newly imported events $passAlong = $server['Server']['url']; - $result = $eventModel->_add($event, $fromXml = true, $user, $server['Server']['organization'], $passAlong, true); + $result = $eventModel->_add($event, $fromXml = true, $user, $server['Server']['organization'], $passAlong, true, $jobId); if ($result) $successes[] = $eventId; else { $fails[$eventId] = 'Failed (partially?) because of validation errors: '. print_r($eventModel->validationErrors, true); } } else { - $result = $eventModel->_edit($event, $existingEvent['Event']['id']); + $result = $eventModel->_edit($event, $existingEvent['Event']['id'], $jobId); if ($result === 'success') $successes[] = $eventId; else $fails[$eventId] = $result; } @@ -239,6 +242,10 @@ class Server extends AppModel { // error $fails[$eventId] = 'failed downloading the event'; } + if ($jobId) { + $job->id = $jobId; + $job->saveField('progress', 100 * (($k + 1) / $eventCount)); + } } if (count($fails) > 0) { // there are fails, take the lowest fail @@ -284,9 +291,6 @@ class Server extends AppModel { } } } - if ($jobId && $k%10 == 0) { - $job->saveField('progress', $k / $eventCount); - } } } $this->Log = ClassRegistry::init('Log'); diff --git a/app/files/empty b/app/files/empty deleted file mode 100755 index e69de29bb..000000000