new: Added object relations

pull/2489/head
iglocska 2017-08-21 10:13:19 +02:00
parent 7b8e11379d
commit 859d6e1c46
10 changed files with 166 additions and 11 deletions

View File

@ -474,6 +474,22 @@ CREATE TABLE IF NOT EXISTS object_references (
-- --------------------------------------------------------
--
-- Table structure for table `object_relationships`
--
CREATE TABLE IF NOT EXISTS object_relationships (
`id` int(11) NOT NULL AUTO_INCREMENT,
`version` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`description` text COLLATE utf8_bin NOT NULL,
`format` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (id),
INDEX `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `object_templates`
--

View File

@ -13,7 +13,7 @@ class ObjectReferencesController extends AppController {
),
);
public function add($objectId, $targetId = false, $targetType = false) {
public function add($objectId) {
if (Validation::uuid($objectId)) {
$temp = $this->ObjectReference->MispObject->find('first', array(
'recursive' => -1,
@ -72,17 +72,20 @@ class ObjectReferencesController extends AppController {
$referenced_id = $target_attribute['Attribute']['id'];
$referenced_type = 0;
}
$relationship_type = empty($this->request->data['ObjectReference']['relationship_type']) ? '' : $this->request->data['ObjectReference']['relationship_type'];
if (!empty($this->request->data['ObjectReference']['relationship_type_select']) && $this->request->data['ObjectReference']['relationship_type_select'] !== 'custom') {
$relationship_type = $this->request->data['ObjectReference']['relationship_type_select'];
}
$data = array(
'referenced_type' => $referenced_type,
'referenced_id' => $referenced_id,
'uuid' => $this->request->data['ObjectReference']['uuid'],
'relationship_type' => !empty($this->request->data['ObjectReference']['relationship_type']) ? $this->request->data['ObjectReference']['relationship_type'] : '',
'relationship_type' => $relationship_type,
'comment' => !empty($this->request->data['ObjectReference']['comment']) ? $this->request->data['ObjectReference']['comment'] : '',
'event_id' => $object['Event']['id'],
'object_id' => $objectId
);
$data['referenced_type'] = $referenced_type;
$data['relationship_type'] = $this->request->data['ObjectReference']['relationship_type'];
$data['uuid'] = $this->request->data['ObjectReference']['uuid'];
$this->ObjectReference->create();
$result = $this->ObjectReference->save(array('ObjectReference' => $data));
@ -137,6 +140,18 @@ class ObjectReferencesController extends AppController {
$event[$d] = $temp;
}
}
$this->loadModel('ObjectRelationship');
$relationshipsTemp = $this->ObjectRelationship->find('all', array(
'recursive' => -1
));
$relationships = array();
$relationshipMetadata = array();
foreach ($relationshipsTemp as $k => $v) {
$relationshipMetadata[$v['ObjectRelationship']['name']] = $v;
$relationships[$v['ObjectRelationship']['name']] = $v['ObjectRelationship']['name'];
}
$relationships['custom'] = 'custom';
$this->set('relationships', $relationships);
$this->set('event', $event);
$this->set('objectId', $objectId);
$this->layout = 'ajax';

View File

@ -98,6 +98,8 @@ class ObjectTemplatesController extends AppController {
public function update() {
$result = $this->ObjectTemplate->update($this->Auth->user());
$this->loadModel('ObjectRelationship');
$result2 = $this->ObjectRelationship->update();
$this->Log = ClassRegistry::init('Log');
$fails = 0;
$successes = 0;
@ -157,6 +159,5 @@ class ObjectTemplatesController extends AppController {
$this->Session->setFlash($message);
}
$this->redirect(array('controller' => 'ObjectTemplates', 'action' => 'index'));
}
}

View File

@ -72,7 +72,7 @@ class ObjectsController extends AppController {
if (empty($error)) {
$error = $this->MispObject->ObjectTemplate->checkTemplateConformity($template, $object);
if ($error === true) {
$result = $this->MispObject->saveObject($object, $eventId, $template, $this->Auth->user(), $errorBehaviour = 'halt');
$result = $this->MispObject->saveObject($object, $eventId, $template, $this->Auth->user(), $errorBehaviour = 'halt');
} else {
$result = false;
}

View File

@ -747,6 +747,17 @@ class AppModel extends Model {
INDEX `relationship_type` (`relationship_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$sqlArray[] = "CREATE TABLE IF NOT EXISTS object_relationships (
`id` int(11) NOT NULL AUTO_INCREMENT,
`version` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`description` text COLLATE utf8_bin NOT NULL,
`format` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (id),
INDEX `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$sqlArray[] = "CREATE TABLE IF NOT EXISTS object_templates (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,

View File

@ -3675,4 +3675,35 @@ class Event extends AppModel {
if (!empty($subcondition)) array_push ($conditions['AND'], $subcondition);
return $conditions;
}
public function prepareEventForView() {
// workaround to get the event dates in to the attribute relations
$relatedDates = array();
if (!empty($event['RelatedEvent'])) {
foreach ($event['RelatedEvent'] as $relation) {
$relatedDates[$relation['Event']['id']] = $relation['Event']['date'];
}
if (!empty($event['RelatedAttribute'])) {
foreach ($event['RelatedAttribute'] as $key => $relatedAttribute) {
foreach ($relatedAttribute as $key2 => $relation) {
$event['RelatedAttribute'][$key][$key2]['date'] = $relatedDates[$relation['id']];
}
}
}
}
$dataForView = array(
'Attribute' => array('attrDescriptions', 'typeDefinitions', 'categoryDefinitions', 'distributionDescriptions', 'distributionLevels', 'shortDist'),
'Event' => array('fieldDescriptions')
);
foreach ($dataForView as $m => $variables) {
if ($m === 'Event') {
$currentModel = $this->Event;
} else if ($m === 'Attribute') {
$currentModel = $this->Event->Attribute;
}
foreach ($variables as $alias => $variable) {
$this->set($alias, $currentModel->{$variable});
}
}
}
}

View File

@ -0,0 +1,57 @@
<?php
App::uses('AppModel', 'Model');
class ObjectRelationship extends AppModel {
public $actsAs = array(
'Containable',
'SysLogLogable.SysLogLogable' => array( // TODO Audit, logable
'userModel' => 'User',
'userKey' => 'user_id',
'change' => 'full'),
);
public $validate = array(
'name' => array(
'unique' => array(
'rule' => 'isUnique',
'message' => 'A relationship with this name already exists.'
),
'valueNotEmpty' => array(
'rule' => array('valueNotEmpty'),
),
),
);
public function beforeValidate($options = array()) {
parent::beforeValidate();
return true;
}
public function afterFind($results, $primary = false) {
foreach ($results as $k => $result) {
if (!empty($results[$k]['ObjectRelationship']['format'])) {
$results[$k]['ObjectRelationship']['format'] = json_decode($results[$k]['ObjectRelationship']['format'], true);
}
}
return $results;
}
public function update() {
$relationsFile = APP . 'files/misp-objects/relationships/definition.json';
if (file_exists($relationsFile)) {
$file = new File($relationsFile);
$relations = json_decode($file->read(), true);
if (!isset($relations['version'])) $relations['version'] = 1;
$this->deleteAll(array('version <' => $relations['version']));
foreach ($relations['values'] as $k => $relation) {
$relation['format'] = json_encode($relation['format'], true);
$relation['version'] = $relations['version'];
$this->create();
$this->save($relation);
}
}
return true;
}
}

View File

@ -29,7 +29,7 @@ class ObjectTemplate extends AppModel {
'ObjectTemplateElement' => array(
'className' => 'ObjectTemplateElement',
'dependent' => true,
),
)
);
public $validate = array(
);
@ -135,11 +135,10 @@ class ObjectTemplate extends AppModel {
}
if ($update_required) {
$attribute = $existingTemplateElements[$k];
unset($existingTemplateElements);
$attribute['object_template_id'] = $id;
$this->ObjectTemplateElement->save(array('ObjectTemplateElement' => $attribute));
}
unset($existingTemplateElements[$k]);
if (isset($existingTemplateElements[$k])) unset($existingTemplateElements[$k]);
} else {
$this->ObjectTemplateElement->create();
$attribute['object_template_id'] = $id;

View File

@ -6,11 +6,23 @@
<div class="row-fluid">
<div class="span6">
<?php
echo $this->Form->input('relationship_type', array(
echo $this->Form->input('relationship_type_select', array(
'label' => 'Relationship type',
'style' => 'width:320px;'
'options' => $relationships,
'style' => 'width:334px;',
'div' => false
));
?>
?>
<div id="" class="hidden">
<label for="ObjectReferenceRelationshipTypeSelect">Relationship type</label>
<?php
echo $this->Form->input('relationship_type', array(
'label' => false,
'style' => 'width:320px;',
'div' => false
));
?>
</div>
</div>
<div class="span6">
<?php
@ -88,6 +100,9 @@
$(".selectOption").click(function() {
changeObjectReferenceSelectOption();
});
$("#ObjectReferenceRelationshipTypeSelect").change(function() {
objectReferenceCheckForCustomRelationship();
});
});
</script>
<?php echo $this->Js->writeBuffer(); // Write cached scripts

View File

@ -3142,6 +3142,16 @@ function objectReferenceInput() {
}
}
function objectReferenceCheckForCustomRelationship() {
var relationship_type_field = $('#ObjectReferenceRelationshipTypeSelect option:selected');
var relationship_type = $(relationship_type_field).val();
if (relationship_type == 'custom') {
$('#ObjectReferenceRelationshipType').parent().removeClass('hidden');
} else {
$('#ObjectReferenceRelationshipType').parent().addClass('hidden');
}
}
function changeObjectReferenceSelectOption() {
var object = $('#targetSelect option:selected');
var uuid = $(object).val();