mirror of https://github.com/MISP/MISP
chg: [Tag] Helper function to attach/detach tags and bump timestamps
parent
668566ee6e
commit
f478841401
|
@ -2422,6 +2422,60 @@ class Attribute extends AppModel
|
|||
return $attribute;
|
||||
}
|
||||
|
||||
public function touch($attribute_id)
|
||||
{
|
||||
$attribute = $this->find('first', [
|
||||
'conditions' => ['Attribute.id' => $attribute_id],
|
||||
'recursive' => -1,
|
||||
]);
|
||||
$event = $this->Event->find('first', [
|
||||
'conditions' => ['Event.id' => $attribute['Attribute']['event_id']],
|
||||
'recursive' => -1,
|
||||
]);
|
||||
$timestamp = (new DateTime())->getTimestamp();
|
||||
$event['Event']['published'] = 0;
|
||||
$event['Event']['timestamp'] = $timestamp;
|
||||
$attribute['Attribute']['timestamp'] = $timestamp;
|
||||
$saveSucces = true;
|
||||
if ($attribute['Attribute']['object_id'] != 0) {
|
||||
$saveSucces = $this->Attribute->Object->updateTimestamp($attribute['Attribute']['object_id'], $timestamp);
|
||||
}
|
||||
$saveSucces = $saveSucces && $this->save($attribute['Attribute'], true, ['timestamp']);
|
||||
return $saveSucces && $this->Event->save($event, true, ['timestamp', 'published']);
|
||||
}
|
||||
|
||||
public function attachTagsFromAttributeAndTouch($attribute_id, $event_id, $tags)
|
||||
{
|
||||
$touchAttribute = false;
|
||||
$success = false;
|
||||
foreach ($tags as $tag_id) {
|
||||
$nothingToChange = false;
|
||||
$saveSuccess = $this->AttributeTag->attachTagToAttribute($attribute_id, $event_id, $tag_id, false, $nothingToChange);
|
||||
$success = $success || !empty($saveSuccess);
|
||||
$touchAttribute = $touchAttribute || !$nothingToChange;
|
||||
}
|
||||
if ($touchAttribute) {
|
||||
return $this->touch($attribute_id);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
public function detachTagsFromAttributeAndTouch($attribute_id, $event_id, $tags)
|
||||
{
|
||||
$touchAttribute = false;
|
||||
$success = false;
|
||||
foreach ($tags as $tag_id) {
|
||||
$nothingToChange = false;
|
||||
$saveSuccess = $this->AttributeTag->detachTagFromAttribute($attribute_id, $event_id, $tag_id, $nothingToChange);
|
||||
$success = $success || !empty($saveSuccess);
|
||||
$touchAttribute = $touchAttribute || !$nothingToChange;
|
||||
}
|
||||
if ($touchAttribute) {
|
||||
return $this->touch($attribute_id);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
private function __blockAttributeViaProposal($attribute)
|
||||
{
|
||||
if (!empty($attribute['ShadowAttribute'])) {
|
||||
|
|
|
@ -135,7 +135,7 @@ class AttributeTag extends AppModel
|
|||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function attachTagToAttribute($attribute_id, $event_id, $tag_id, $local = false)
|
||||
public function attachTagToAttribute($attribute_id, $event_id, $tag_id, $local = false, &$nothingToChange = false)
|
||||
{
|
||||
$existingAssociation = $this->hasAny([
|
||||
'tag_id' => $tag_id,
|
||||
|
@ -152,11 +152,13 @@ class AttributeTag extends AppModel
|
|||
if (!$this->save($data)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$nothingToChange = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function detachTagFromAttribute($attribute_id, $event_id, $tag_id)
|
||||
public function detachTagFromAttribute($attribute_id, $event_id, $tag_id, &$nothingToChange = false)
|
||||
{
|
||||
$existingAssociation = $this->find('first', array(
|
||||
'recursive' => -1,
|
||||
|
@ -173,6 +175,8 @@ class AttributeTag extends AppModel
|
|||
if ($result) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
$nothingToChange = true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -489,6 +489,47 @@ class Event extends AppModel
|
|||
return $events;
|
||||
}
|
||||
|
||||
public function touch($event_id)
|
||||
{
|
||||
$event = $this->find('first', [
|
||||
'conditions' => ['Event.id' => $event_id],
|
||||
'recursive' => -1,
|
||||
]);
|
||||
$event['Event']['published'] = 0;
|
||||
$event['Event']['timestamp'] = (new DateTime())->getTimestamp();
|
||||
return $this->save($event, true, ['timestamp', 'published']);
|
||||
}
|
||||
|
||||
public function attachTagsToEventAndTouch($event_id, $tags)
|
||||
{
|
||||
$touchEvent = false;
|
||||
$success = false;
|
||||
foreach ($tags as $tagId) {
|
||||
$nothingToChange = false;
|
||||
$success = $success || $this->EventTag->attachTagToEvent($event_id, ['id' => $tagId], $nothingToChange);
|
||||
$touchEvent = $touchEvent || !$nothingToChange;
|
||||
}
|
||||
if ($touchEvent) {
|
||||
return $this->touch($event_id);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
public function detachTagsFromEventAndTouch($event_id, $tags)
|
||||
{
|
||||
$touchEvent = false;
|
||||
$success = false;
|
||||
foreach ($tags as $tagId) {
|
||||
$nothingToChange = false;
|
||||
$success = $success || $this->EventTag->detachTagFromEvent($event_id, $tagId, $nothingToChange);
|
||||
$touchEvent = $touchEvent || !$nothingToChange;
|
||||
}
|
||||
if ($touchEvent) {
|
||||
return $this->touch($event_id);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the logged in user + an array of events, attaches the correlation count to each
|
||||
* @param array $user
|
||||
|
|
Loading…
Reference in New Issue