mirror of https://github.com/MISP/MISP
Merge branch 'feature/tags-deletion' into fix-update-tags-on-attribute-edit
commit
5b4cef3e6c
|
@ -881,10 +881,16 @@ class AttributesController extends AppController
|
||||||
throw new NotFoundException(__('Invalid Event.'));
|
throw new NotFoundException(__('Invalid Event.'));
|
||||||
}
|
}
|
||||||
if ($existingAttribute['Attribute']['object_id']) {
|
if ($existingAttribute['Attribute']['object_id']) {
|
||||||
$result = $this->Attribute->save($this->request->data, array('fieldList' => $this->Attribute->editableFieds));
|
$result = $this->Attribute->save($this->request->data, array('Attribute.category', 'Attribute.value', 'Attribute.to_ids', 'Attribute.comment', 'Attribute.distribution', 'Attribute.sharing_group_id'));
|
||||||
|
if ($result) {
|
||||||
|
$this->Attribute->AttributeTag->handleAttributeTags($this->request->data['Attribute'], $event['Event']['id']);
|
||||||
|
}
|
||||||
$this->Attribute->Object->updateTimestamp($existingAttribute['Attribute']['object_id']);
|
$this->Attribute->Object->updateTimestamp($existingAttribute['Attribute']['object_id']);
|
||||||
} else {
|
} else {
|
||||||
$result = $this->Attribute->save($this->request->data, array('fieldList' => $this->Attribute->editableFieds));
|
$result = $this->Attribute->save($this->request->data);
|
||||||
|
if ($result) {
|
||||||
|
$this->Attribute->AttributeTag->handleAttributeTags($this->request->data['Attribute'], $event['Event']['id']);
|
||||||
|
}
|
||||||
if ($this->request->is('ajax')) {
|
if ($this->request->is('ajax')) {
|
||||||
$this->autoRender = false;
|
$this->autoRender = false;
|
||||||
if ($result) {
|
if ($result) {
|
||||||
|
@ -908,20 +914,22 @@ class AttributesController extends AppController
|
||||||
$this->Attribute->Object->save($object);
|
$this->Attribute->Object->save($object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($this->_isRest() || $this->response->type() === 'application/json') {
|
if ($this->_isRest()) {
|
||||||
$saved_attribute = $this->Attribute->find('first', array(
|
$saved_attribute = $this->Attribute->find('first', array(
|
||||||
'conditions' => array('id' => $this->Attribute->id),
|
'conditions' => array('id' => $this->Attribute->id),
|
||||||
'recursive' => -1,
|
'recursive' => -1,
|
||||||
'fields' => $this->Attribute->defaultFields
|
'contain' => array('AttributeTag' => array('Tag'))
|
||||||
));
|
));
|
||||||
$response = array('response' => array('Attribute' => $saved_attribute['Attribute']));
|
if ($this->response->type() === 'application/json') {
|
||||||
$this->set('response', $response);
|
$type = 'json';
|
||||||
if ($this->response->type() === 'application/json') {
|
} else {
|
||||||
$this->render('/Attributes/json/view');
|
$type = 'xml';
|
||||||
} else {
|
}
|
||||||
$this->render('view');
|
App::uses(strtoupper($type) . 'ConverterTool', 'Tools');
|
||||||
}
|
$tool = strtoupper($type) . 'ConverterTool';
|
||||||
return;
|
$converter = new $tool();
|
||||||
|
$saved_attribute = $converter->convertAttribute($saved_attribute, true);
|
||||||
|
return $this->RestResponse->viewData($saved_attribute, $type);
|
||||||
} else {
|
} else {
|
||||||
$this->redirect(array('controller' => 'events', 'action' => 'view', $eventId));
|
$this->redirect(array('controller' => 'events', 'action' => 'view', $eventId));
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,24 @@ class JSONConverterTool
|
||||||
return ']}' . PHP_EOL;
|
return ']}' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function convertAttribute($attribute, $raw = false)
|
||||||
|
{
|
||||||
|
$toRearrange = array('AttributeTag');
|
||||||
|
foreach ($toRearrange as $object) {
|
||||||
|
if (isset($attribute[$object])) {
|
||||||
|
$attribute['Attribute'][$object] = $attribute[$object];
|
||||||
|
unset($attribute[$object]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Submit as list to the attribute cleaner but obtain the only attribute
|
||||||
|
$attribute['Attribute'] = $this->__cleanAttributes(array($attribute['Attribute']))[0];
|
||||||
|
if ($raw) {
|
||||||
|
return $attribute;
|
||||||
|
}
|
||||||
|
return json_encode($attribute, JSON_PRETTY_PRINT);
|
||||||
|
}
|
||||||
|
|
||||||
public function convertObject($object, $isSiteAdmin = false, $raw = false)
|
public function convertObject($object, $isSiteAdmin = false, $raw = false)
|
||||||
{
|
{
|
||||||
$toRearrange = array('SharingGroup', 'Attribute', 'ShadowAttribute', 'Event');
|
$toRearrange = array('SharingGroup', 'Attribute', 'ShadowAttribute', 'Event');
|
||||||
|
|
|
@ -4303,8 +4303,9 @@ class Attribute extends AppModel
|
||||||
foreach ($attribute['Tag'] as $tag) {
|
foreach ($attribute['Tag'] as $tag) {
|
||||||
$tag_id = $this->AttributeTag->Tag->captureTag($tag, $user);
|
$tag_id = $this->AttributeTag->Tag->captureTag($tag, $user);
|
||||||
if ($tag_id) {
|
if ($tag_id) {
|
||||||
|
$tag['id'] = $tag_id;
|
||||||
// fix the IDs here
|
// fix the IDs here
|
||||||
$this->AttributeTag->attachTagToAttribute($this->id, $attribute['event_id'], $tag_id);
|
$this->AttributeTag->handleAttributeTag($this->id, $attribute['event_id'], $tag);
|
||||||
} else {
|
} else {
|
||||||
// If we couldn't attach the tag it is most likely because we couldn't create it - which could have many reasons
|
// If we couldn't attach the tag it is most likely because we couldn't create it - which could have many reasons
|
||||||
// However, if a tag couldn't be added, it could also be that the user is a tagger but not a tag editor
|
// However, if a tag couldn't be added, it could also be that the user is a tagger but not a tag editor
|
||||||
|
|
|
@ -85,6 +85,30 @@ class AttributeTag extends AppModel
|
||||||
$this->delete($id);
|
$this->delete($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function handleAttributeTags($attribute, $event_id)
|
||||||
|
{
|
||||||
|
if (isset($attribute['Tag'])) {
|
||||||
|
foreach ($attribute['Tag'] as $tag) {
|
||||||
|
if (!isset($tag['id'])) {
|
||||||
|
$tag_id = $this->Tag->lookupTagIdFromName($tag['name']);
|
||||||
|
$tag['id'] = $tag_id;
|
||||||
|
}
|
||||||
|
if ($tag['id'] > 0) {
|
||||||
|
$this->handleAttributeTag($attribute['id'], $event_id, $tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleAttributeTag($attribute_id, $event_id, $tag)
|
||||||
|
{
|
||||||
|
if (empty($tag['deleted'])) {
|
||||||
|
$this->attachTagToAttribute($attribute_id, $event_id, $tag['id']);
|
||||||
|
} else {
|
||||||
|
$this->detachTagFromAttribute($attribute_id, $event_id, $tag['id']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function attachTagToAttribute($attribute_id, $event_id, $tag_id)
|
public function attachTagToAttribute($attribute_id, $event_id, $tag_id)
|
||||||
{
|
{
|
||||||
$existingAssociation = $this->find('first', array(
|
$existingAssociation = $this->find('first', array(
|
||||||
|
@ -103,6 +127,26 @@ class AttributeTag extends AppModel
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function detachTagFromAttribute($attribute_id, $event_id, $tag_id)
|
||||||
|
{
|
||||||
|
$existingAssociation = $this->find('first', array(
|
||||||
|
'recursive' => -1,
|
||||||
|
'conditions' => array(
|
||||||
|
'tag_id' => $tag_id,
|
||||||
|
'event_id' => $event_id,
|
||||||
|
'attribute_id' => $attribute_id
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!empty($existingAssociation)) {
|
||||||
|
$result = $this->delete($existingAssociation['AttributeTag']['id']);
|
||||||
|
if ($result) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// This function help mirroring the tags at attribute level. It will delete tags that are not present on the remote attribute
|
// This function help mirroring the tags at attribute level. It will delete tags that are not present on the remote attribute
|
||||||
public function pruneOutdatedAttributeTagsFromSync($newerTags, $originalAttributeTags)
|
public function pruneOutdatedAttributeTagsFromSync($newerTags, $originalAttributeTags)
|
||||||
{
|
{
|
||||||
|
|
|
@ -3942,7 +3942,8 @@ class Event extends AppModel
|
||||||
$tag_id = $this->EventTag->Tag->captureTag($tag, $user);
|
$tag_id = $this->EventTag->Tag->captureTag($tag, $user);
|
||||||
if ($tag_id) {
|
if ($tag_id) {
|
||||||
$nothingToChange = false;
|
$nothingToChange = false;
|
||||||
$result = $this->EventTag->attachTagToEvent($this->id, $tag_id, $nothingToChange);
|
$tag['id'] = $tag_id;
|
||||||
|
$result = $this->EventTag->handleEventTag($this->id, $tag, $nothingToChange);
|
||||||
if ($result && !$nothingToChange) {
|
if ($result && !$nothingToChange) {
|
||||||
$changed = true;
|
$changed = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,6 +110,16 @@ class EventTag extends AppModel
|
||||||
return $eventIDs;
|
return $eventIDs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function handleEventTag($event_id, $tag, &$nothingToChange = false)
|
||||||
|
{
|
||||||
|
if (empty($tag['deleted'])) {
|
||||||
|
$result = $this->attachTagToEvent($event_id, $tag['id'], $nothingToChange);
|
||||||
|
} else {
|
||||||
|
$result = $this->detachTagFromEvent($event_id, $tag['id'], $nothingToChange);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
public function attachTagToEvent($event_id, $tag_id, &$nothingToChange = false)
|
public function attachTagToEvent($event_id, $tag_id, &$nothingToChange = false)
|
||||||
{
|
{
|
||||||
$existingAssociation = $this->find('first', array(
|
$existingAssociation = $this->find('first', array(
|
||||||
|
@ -130,6 +140,27 @@ class EventTag extends AppModel
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function detachTagFromEvent($event_id, $tag_id, &$nothingToChange = false)
|
||||||
|
{
|
||||||
|
$existingAssociation = $this->find('first', array(
|
||||||
|
'recursive' => -1,
|
||||||
|
'conditions' => array(
|
||||||
|
'tag_id' => $tag_id,
|
||||||
|
'event_id' => $event_id
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!empty($existingAssociation)) {
|
||||||
|
$result = $this->delete($existingAssociation['EventTag']['id']);
|
||||||
|
if ($result) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$nothingToChange = true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function getSortedTagList($context = false)
|
public function getSortedTagList($context = false)
|
||||||
{
|
{
|
||||||
$conditions = array();
|
$conditions = array();
|
||||||
|
|
Loading…
Reference in New Issue