From 4ae71fc963e5c9b6347f75b3369781bced73cf54 Mon Sep 17 00:00:00 2001 From: noud Date: Tue, 28 Aug 2012 15:36:14 +0200 Subject: [PATCH] Sync. Sync worked, but we did not know what to do with user_id and org. Now, on sync, anonymize the user_id, get the Server.organization and put that into Event.org. And, display owning flag if Event.user_id or get the Server.logo belonging to Event.org (=Server.organization) when Event.user_id is empty (=0). To this there is organization name and logo in bootstrap and other organizations names and logos in Servers. --- app/Config/bootstrap.php | 2 ++ app/Controller/EventsController.php | 17 ++++++++---- app/Controller/ServersController.php | 4 +-- app/Model/Event.php | 39 +++++++++++++++------------- app/Model/Server.php | 11 ++++++++ app/View/Events/index.ctp | 5 ++++ app/View/Servers/add.ctp | 8 ++++++ app/View/Servers/edit.ctp | 8 ++++++ app/View/Servers/index.ctp | 4 +++ 9 files changed, 73 insertions(+), 25 deletions(-) mode change 100644 => 100755 app/View/Servers/add.ctp mode change 100644 => 100755 app/View/Servers/edit.ctp diff --git a/app/Config/bootstrap.php b/app/Config/bootstrap.php index bf7686d23..dea4093be 100644 --- a/app/Config/bootstrap.php +++ b/app/Config/bootstrap.php @@ -91,6 +91,8 @@ Configure::write('CyDefSIG.name', 'CyDefSIG'); Configure::write('CyDefSIG.header', 'CyDefSIG: Cyber Defence Signature Sharing Platform'); Configure::write('CyDefSIG.footer', 'Powered by CyDefSIG © Belgian Defense CERT & NCIRC'); //Configure::write('CyDefSIG.logo', '/img/logo_big.gif'); +Configure::write('CyDefSIG.org', 'BE MOD'); // if sync this will be Event.org content on the peer side +Configure::write('CyDefSIG.logo', 'logo_defense.jpg'); // used in Events::index for owned events Configure::write('CyDefSIG.showorg', 'false'); // show the name of the organisation that uploaded the data diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 2092d3af7..5252cfd1d 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -72,6 +72,10 @@ class EventsController extends AppController { $this->Session->setFlash('No GPG key set in your profile. To receive emails, submit your public key in your profile.'); } $this->set('event_descriptions', $this->Event->field_descriptions); + + $this->set('logo', Configure::read('CyDefSIG.logo')); + $this->set('logo_alt', Configure::read('CyDefSIG.org')); + $this->set('logos', ClassRegistry::init('Server')->getLogos()); } /** @@ -204,7 +208,7 @@ class EventsController extends AppController { */ public function add() { if ($this->request->is('post')) { - if ($this->_add($this->request->data, $this->Auth, $this->_isRest())) { + if ($this->_add($this->request->data, $this->Auth, $this->_isRest(),'')) { if ($this->_isRest()) { // REST users want to see the newly created event $this->view($this->Event->getId()); @@ -232,10 +236,12 @@ class EventsController extends AppController { * * @return bool true if success */ - public function _add(&$data, &$auth, $fromXml) { + public function _add(&$data, &$auth, $fromXml, $or='') { // force check userid and orgname to be from yourself if (!$fromXml) $data['Event']['user_id'] = $auth->user('id'); - $data['Event']['org'] = $auth->user('org'); + else $data['Event']['user_id'] = '0'; + if (!$fromXml) $data['Event']['org'] = $auth->user('org'); + if (strlen($or)) $data['Event']['org'] = $or; unset ($data['Event']['id']); $this->Event->create(); @@ -253,12 +259,13 @@ class EventsController extends AppController { $data['Event']['private'] = true; } - if (isset($data['Event']['uuid'])) { + if (isset($data['Event']['uuid'])) { // TODO here we should start RESTful dialog // check if the uuid already exists $existingEventCount = $this->Event->find('count', array('conditions' => array('Event.uuid'=>$data['Event']['uuid']))); if ($existingEventCount > 0) { $existingEvent = $this->Event->find('first', array('conditions' => array('Event.uuid'=>$data['Event']['uuid']))); $data['Event']['id'] = $existingEvent['Event']['id']; + $data['Event']['org'] = $existingEvent['Event']['org']; // attributes.. $c = 0; if (isset($data['Attribute'])) { @@ -406,7 +413,7 @@ class EventsController extends AppController { if ($this->Event->delete()) { // delete the event from remote servers - if ('true' == Configure::read('CyDefSIG.sync')) { + if ('true' == Configure::read('CyDefSIG.sync')) { // TODO test..(!$this->_isRest()) && $this->_deleteEventFromServers($uuid); } diff --git a/app/Controller/ServersController.php b/app/Controller/ServersController.php index c2a3d5069..4afaa8c19 100755 --- a/app/Controller/ServersController.php +++ b/app/Controller/ServersController.php @@ -98,7 +98,7 @@ class ServersController extends AppController { if ($this->request->is('post') || $this->request->is('put')) { // say what fields are to be updated - $fieldList=array('url', 'push', 'pull'); + $fieldList=array('url', 'push', 'pull', 'organization','logo'); if ("" != $this->request->data['Server']['authkey']) $fieldList[] = 'authkey'; // Save the data @@ -177,7 +177,7 @@ class ServersController extends AppController { $event['Event']['info'] .= "\n Imported from ".$this->Server->data['Server']['url']; $eventsController = new EventsController(); try { - $result = $eventsController->_add($event, $this->Auth, $fromXml=true); + $result = $eventsController->_add($event, $this->Auth, $fromXml=true, $this->Server->data['Server']['organization']); } catch (MethodNotAllowedException $e) { if ($e->getMessage() == 'Event already exists') { //$successes[] = $event_id; // commented given it's in a catch.. diff --git a/app/Model/Event.php b/app/Model/Event.php index 0d2d79f21..a5bf2cb42 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -265,7 +265,7 @@ class Event extends AppModel { unset($event['Attribute']); // cleanup the array from things we do not want to expose - unset($event['Event']['org']); + //unset($event['Event']['org']); // remove value1 and value2 from the output foreach($event['Event']['Attribute'] as $key => &$attribute) { // do not keep attributes that are private @@ -286,7 +286,8 @@ class Event extends AppModel { // add the 'Imported from' conform ServersController.php:177 // no need to remove lateron cause on pushing server Event is already saved. $event['Event']['info'] .= "\n Published from ".Configure::read('CyDefSIG.baseurl'); - + if ($event['Event']['user_id'] != '0') $event['Event']['org'] = Configure::read('CyDefSIG.org'); // TODO + // display the XML to the user $xmlArray['Event'][] = $event['Event']; $xmlObject = Xml::fromArray($xmlArray, array('format' => 'tags')); @@ -299,21 +300,23 @@ class Event extends AppModel { // TODO NETWORK for now do not know how to catch the following.. // TODO NETWORK No route to host $response = $HttpSocket->post($uri, $data, $request); - if ($response->isOk()) { - return true; - } - else { - try { - // parse the XML response and keep the reason why it failed - $xml_array = Xml::toArray(Xml::build($response->body)); - } catch (XmlException $e) { - return true; - } - if (strpos($xml_array['response']['name'],"Event already exists")) { // strpos, so i can piggyback some value if needed. - return true; - } else { - return $xml_array['response']['name']; - } + if ($response->code == '200') { // 200 (OK) + entity-action-result + if ($response->isOk()) { + return true; + } + else { + try { + // parse the XML response and keep the reason why it failed + $xml_array = Xml::toArray(Xml::build($response->body)); + } catch (XmlException $e) { + return true; + } + if (strpos($xml_array['response']['name'],"Event already exists")) { // strpos, so i can piggyback some value if needed. + return true; + } else { + return $xml_array['response']['name']; + } + } } } } @@ -349,7 +352,7 @@ class Event extends AppModel { // TODO NETWORK for now do not know how to catch the following.. // TODO NETWORK No route to host $response = $HttpSocket->delete($uri, array(), $request); - // TODO REST, DELETE, no responce needed + // TODO REST, DELETE, some responce needed } } diff --git a/app/Model/Server.php b/app/Model/Server.php index 994808f66..328d6d83a 100644 --- a/app/Model/Server.php +++ b/app/Model/Server.php @@ -94,5 +94,16 @@ class Server extends AppModel { return $this->field('id', array('id' => $serverid, 'org' => $org)) === $serverid; } + public function getLogos() { + $logos = array(); + $results = $this->find('all', + array('recursive' => 0, + 'fields' => 'Server.organization,Server.logo' + )); + foreach ($results as $result ) { + $logos[$result['Server']['organization']] = $result['Server']['logo']; + } + return $logos; + } } diff --git a/app/View/Events/index.ctp b/app/View/Events/index.ctp index f08224863..0aa9da60f 100755 --- a/app/View/Events/index.ctp +++ b/app/View/Events/index.ctp @@ -2,6 +2,7 @@

Events

+ @@ -19,6 +20,10 @@ foreach ($events as $event): ?> + diff --git a/app/View/Servers/add.ctp b/app/View/Servers/add.ctp old mode 100644 new mode 100755 index 198b5a6c1..9145353c6 --- a/app/View/Servers/add.ctp +++ b/app/View/Servers/add.ctp @@ -7,6 +7,14 @@ 'label' => 'Base URL', 'before' => $this->Html->div('forminfo', 'The base-url to the external server you want to sync with.
Example: https://foo.sig.mil.be'), )); + echo $this->Form->input('organization', array( + 'label' => 'Organization', + 'before' => $this->Html->div('forminfo', 'The organization having the external server you want to sync with.
Example: BE'), + )); + echo $this->Form->input('logo', array( + 'label' => 'Logo', + 'before' => $this->Html->div('forminfo', 'The logo of the organization having the external server you want to sync with.
Example: jaune.jpg'), + )); echo $this->Form->input('authkey', array( 'before' => $this->Html->div('forminfo', 'You can find the authentication key on your profile on the external server.'), )); diff --git a/app/View/Servers/edit.ctp b/app/View/Servers/edit.ctp old mode 100644 new mode 100755 index 37a96c006..34539e610 --- a/app/View/Servers/edit.ctp +++ b/app/View/Servers/edit.ctp @@ -8,6 +8,14 @@ 'label' => 'Base URL', 'before' => $this->Html->div('forminfo', 'The base-url to the external server you want to sync with.
Example: https://foo.sig.mil.be'), )); + echo $this->Form->input('organization', array( + 'label' => 'Organization', + 'before' => $this->Html->div('forminfo', 'The organization having the external server you want to sync with.
Example: https://foo.sig.mil.be'), + )); + echo $this->Form->input('logo', array( + 'label' => 'Logo', + 'before' => $this->Html->div('forminfo', 'The logo of the organization having the external server you want to sync with.
Example: jaune.jpg'), + )); echo $this->Form->input('authkey', array( 'before' => $this->Html->div('forminfo', 'You can find the authentication key on your profile on the external server.
Leave empty if you don\'t want to change it.'), )); diff --git a/app/View/Servers/index.ctp b/app/View/Servers/index.ctp index ec1047e7f..0d5ba93dd 100644 --- a/app/View/Servers/index.ctp +++ b/app/View/Servers/index.ctp @@ -5,6 +5,8 @@ + + @@ -18,6 +20,8 @@ + +
Paginator->sort('id');?> Paginator->sort('org');?>
Html->image($logo, array('alt' => $logo_alt,'width'=>'50','hight'=>'38')); + else echo $this->Html->image($logos[$event['Event']['org']], array('alt' => $event['Event']['org'],'width'=>'50','hight'=>'38')); + ?> Html->link($event['Event']['id'], array('controller' => 'events', 'action' => 'view', $event['Event']['id'])); ?>  Paginator->sort('push');?> Paginator->sort('pull');?> Paginator->sort('url');?>FromLogo Paginator->sort('org');?>