mirror of https://github.com/MISP/MISP
Merge pull request #6980 from JakubOnderka/tag-view-refactor
chg: [internal] Refactor TagsController::viewpull/6982/head
commit
acc459a517
|
@ -343,79 +343,33 @@ class TagsController extends AppController
|
|||
|
||||
public function view($id)
|
||||
{
|
||||
if ($this->_isRest()) {
|
||||
$contain = array('EventTag' => array('fields' => 'event_id'));
|
||||
$contain['AttributeTag'] = array('fields' => 'attribute_id');
|
||||
$tag = $this->Tag->find('first', array(
|
||||
'conditions' => array('id' => $id),
|
||||
'recursive' => -1,
|
||||
'contain' => $contain
|
||||
));
|
||||
if (empty($tag)) {
|
||||
throw new MethodNotAllowedException('Invalid Tag');
|
||||
}
|
||||
if (empty($tag['EventTag'])) {
|
||||
$tag['Tag']['count'] = 0;
|
||||
} else {
|
||||
$eventIDs = array();
|
||||
foreach ($tag['EventTag'] as $eventTag) {
|
||||
$eventIDs[] = $eventTag['event_id'];
|
||||
}
|
||||
$conditions = array('Event.id' => $eventIDs);
|
||||
if (!$this->_isSiteAdmin()) {
|
||||
$conditions = array_merge(
|
||||
$conditions,
|
||||
array('OR' => array(
|
||||
array('AND' => array(
|
||||
array('Event.distribution >' => 0),
|
||||
array('Event.published =' => 1)
|
||||
)),
|
||||
array('Event.orgc_id' => $this->Auth->user('org_id'))
|
||||
))
|
||||
);
|
||||
}
|
||||
$events = $this->Tag->EventTag->Event->find('all', array(
|
||||
'fields' => array('Event.id', 'Event.distribution', 'Event.orgc_id'),
|
||||
'conditions' => $conditions
|
||||
));
|
||||
$tag['Tag']['count'] = count($events);
|
||||
}
|
||||
unset($tag['EventTag']);
|
||||
if (empty($tag['AttributeTag'])) {
|
||||
$tag['Tag']['attribute_count'] = 0;
|
||||
} else {
|
||||
$attributeIDs = array();
|
||||
foreach ($tag['AttributeTag'] as $attributeTag) {
|
||||
$attributeIDs[] = $attributeTag['attribute_id'];
|
||||
}
|
||||
$conditions = array('Attribute.id' => $attributeIDs);
|
||||
if (!$this->_isSiteAdmin()) {
|
||||
$conditions = array_merge(
|
||||
$conditions,
|
||||
array('OR' => array(
|
||||
array('AND' => array(
|
||||
array('Attribute.deleted =' => 0),
|
||||
array('Attribute.distribution >' => 0),
|
||||
array('Event.distribution >' => 0),
|
||||
array('Event.published =' => 1)
|
||||
)),
|
||||
array('Event.orgc_id' => $this->Auth->user('org_id'))
|
||||
))
|
||||
);
|
||||
}
|
||||
$attributes = $this->Tag->AttributeTag->Attribute->find('all', array(
|
||||
'fields' => array('Attribute.id', 'Attribute.deleted', 'Attribute.distribution', 'Event.id', 'Event.distribution', 'Event.orgc_id'),
|
||||
'contain' => array('Event' => array('fields' => array('id', 'distribution', 'orgc_id'))),
|
||||
'conditions' => $conditions
|
||||
));
|
||||
$tag['Tag']['attribute_count'] = count($attributes);
|
||||
}
|
||||
unset($tag['AttributeTag']);
|
||||
$this->set('Tag', $tag['Tag']);
|
||||
$this->set('_serialize', 'Tag');
|
||||
} else {
|
||||
if (!$this->_isRest()) {
|
||||
throw new MethodNotAllowedException('This action is only for REST users.');
|
||||
}
|
||||
|
||||
$tag = $this->Tag->find('first', array(
|
||||
'conditions' => array('id' => $id),
|
||||
'recursive' => -1,
|
||||
'contain' => ['AttributeTag' => ['fields' => 'attribute_id']],
|
||||
));
|
||||
if (empty($tag)) {
|
||||
throw new MethodNotAllowedException('Invalid Tag');
|
||||
}
|
||||
|
||||
$tag['Tag']['count'] = $this->Tag->EventTag->countForTag($tag['Tag']['id'], $this->Auth->user());
|
||||
|
||||
if (empty($tag['AttributeTag'])) {
|
||||
$tag['Tag']['attribute_count'] = 0;
|
||||
} else {
|
||||
$attributeIDs = array_column($tag['AttributeTag'], 'attribute_id');
|
||||
$tag['Tag']['attribute_count'] = count($this->Tag->AttributeTag->Attribute->fetchAttributes($this->Auth->user(), [
|
||||
'conditions' => ['Attribute.id' => $attributeIDs],
|
||||
'list' => true,
|
||||
]));
|
||||
}
|
||||
unset($tag['AttributeTag']);
|
||||
|
||||
return $this->RestResponse->viewData($tag['Tag'], $this->response->type());
|
||||
}
|
||||
|
||||
public function showEventTag($id)
|
||||
|
|
|
@ -3,6 +3,7 @@ App::uses('AppModel', 'Model');
|
|||
|
||||
/**
|
||||
* @property Tag $Tag
|
||||
* @property Attribute $Attribute
|
||||
*/
|
||||
class AttributeTag extends AppModel
|
||||
{
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
<?php
|
||||
App::uses('AppModel', 'Model');
|
||||
|
||||
/**
|
||||
* @property EventTag $EventTag
|
||||
* @property AttributeTag $AttributeTag
|
||||
*/
|
||||
class Tag extends AppModel
|
||||
{
|
||||
public $useTable = 'tags';
|
||||
|
|
Loading…
Reference in New Issue