chg: [internal] Use find('column') on more places

pull/6797/head
Jakub Onderka 2020-12-23 12:50:19 +01:00
parent a6b76e70ae
commit 9bc52c18fe
4 changed files with 56 additions and 86 deletions

View File

@ -136,12 +136,12 @@ class EventsController extends AppController
$includeConditions['OR'][] = array('lower(Attribute.value2) LIKE' => $i);
}
$includeIDs = array_values($this->Event->Attribute->fetchAttributes($this->Auth->user(), array(
$includeIDs = $this->Event->Attribute->fetchAttributes($this->Auth->user(), array(
'conditions' => $includeConditions,
'flatten' => true,
'event_ids' => true,
'list' => true,
)));
));
}
if (!empty($exclude)) {
@ -151,12 +151,12 @@ class EventsController extends AppController
$excludeConditions['OR'][] = array('lower(Attribute.value2) LIKE' => $e);
}
$excludeIDs = array_values($this->Event->Attribute->fetchAttributes($this->Auth->user(), array(
$excludeIDs = $this->Event->Attribute->fetchAttributes($this->Auth->user(), array(
'conditions' => $excludeConditions,
'flatten' => true,
'event_ids' => true,
'list' => true,
)));
));
}
}
// return -1 as the only value in includedIDs if both arrays are empty. This will mean that no events will be shown if there was no hit
@ -191,15 +191,13 @@ class EventsController extends AppController
$conditions = array(
'OR' => $subconditions,
);
$attributeHits = $this->Event->Attribute->fetchAttributes($this->Auth->user(), array(
$result = $this->Event->Attribute->fetchAttributes($this->Auth->user(), array(
'conditions' => $conditions,
'flatten' => 1,
'event_ids' => true,
'list' => true,
));
$result = array_values($attributeHits);
// we now have a list of event IDs that match on an attribute level, and the user can see it. Let's also find all of the events that match on other criteria!
// What is interesting here is that we no longer have to worry about the event's releasability. With attributes this was a different case,
// because we might run into a situation where a user can see an event but not a specific attribute
@ -234,10 +232,9 @@ class EventsController extends AppController
foreach ($values as $v) {
$subconditions[] = array('lower(name) LIKE' => $v);
}
$orgs = $this->Event->Org->find('list', array(
$orgs = $this->Event->Org->find('column', array(
'conditions' => $subconditions,
'recursive' => -1,
'fields' => array('id')
'fields' => array('Org.id')
));
$conditions = empty($result) ? [] : ['NOT' => ['id' => $result]]; // Do not include events that we already found
@ -246,11 +243,10 @@ class EventsController extends AppController
$conditions['OR'][] = array('lower(uuid) LIKE' => $v);
}
if (!empty($orgs)) {
$conditions['OR']['orgc_id'] = array_values($orgs);
$conditions['OR']['orgc_id'] = $orgs;
}
$otherEvents = $this->Event->find('list', array(
'recursive' => -1,
'fields' => array('id'),
$otherEvents = $this->Event->find('column', array(
'fields' => array('Event.id'),
'conditions' => $conditions,
));
foreach ($otherEvents as $eventId) {
@ -454,9 +450,9 @@ class EventsController extends AppController
$test = array();
foreach ($pieces as $piece) {
if ($piece[0] == '!') {
$this->paginate['conditions']['AND'][] = array('lower(Event.info)' . ' NOT LIKE' => '%' . strtolower(substr($piece, 1)) . '%');
$this->paginate['conditions']['AND'][] = array('lower(Event.info) NOT LIKE' => '%' . strtolower(substr($piece, 1)) . '%');
} else {
$test['OR'][] = array('lower(Event.info)' . ' LIKE' => '%' . strtolower($piece) . '%');
$test['OR'][] = array('lower(Event.info) LIKE' => '%' . strtolower($piece) . '%');
}
}
$this->paginate['conditions']['AND'][] = $test;
@ -495,17 +491,13 @@ class EventsController extends AppController
$filterString .= '!' . $piece;
continue;
}
$block = $this->Event->EventTag->find('all', array(
'conditions' => array('EventTag.tag_id' => $tagName['Tag']['id']),
'fields' => 'event_id',
'recursive' => -1,
$block = $this->Event->EventTag->find('column', array(
'conditions' => array('EventTag.tag_id' => $tagName['Tag']['id']),
'fields' => ['EventTag.event_id'],
));
if (!empty($block)) {
$sqlSubQuery = 'Event.id NOT IN (';
foreach ($block as $b) {
$sqlSubQuery .= $b['EventTag']['event_id'] . ',';
}
$tagRules['AND'][] = substr($sqlSubQuery, 0, -1) . ')';
$sqlSubQuery = 'Event.id NOT IN (' . implode(",", $block) . ')';
$tagRules['AND'][] = $sqlSubQuery;
}
if ($filterString != "") {
$filterString .= "|";
@ -532,18 +524,14 @@ class EventsController extends AppController
continue;
}
$allow = $this->Event->EventTag->find('all', array(
'conditions' => array('EventTag.tag_id' => $tagName['Tag']['id']),
'fields' => 'event_id',
'recursive' => -1,
$allow = $this->Event->EventTag->find('column', array(
'conditions' => array('EventTag.tag_id' => $tagName['Tag']['id']),
'fields' => ['EventTag.event_id'],
));
if (!empty($allow)) {
$sqlSubQuery = 'Event.id IN (';
foreach ($allow as $a) {
$setOR = true;
$sqlSubQuery .= $a['EventTag']['event_id'] . ',';
}
$tagRules['OR'][] = substr($sqlSubQuery, 0, -1) . ')';
$setOR = true;
$sqlSubQuery = 'Event.id IN ('. implode(",", $allow) . ')';
$tagRules['OR'][] = $sqlSubQuery;
}
if ($filterString != "") {
$filterString .= "|";

View File

@ -998,6 +998,7 @@ class Attribute extends AppModel
'recursive' => -1,
'fields' => array('id'),
'conditions' => $conditions,
'order' => false,
);
if (!empty($this->find('first', $params))) {
// value isn't unique
@ -2851,8 +2852,7 @@ class Attribute extends AppModel
// get all attributes..
if (!$eventId) {
$eventIds = $this->Event->find('list', [
'recursive' => -1,
$eventIds = $this->Event->find('column', [
'fields' => ['Event.id'],
'conditions' => ['Event.disable_correlation' => 0],
]);
@ -2868,7 +2868,7 @@ class Attribute extends AppModel
} else {
$jobId = false;
}
foreach (array_values($eventIds) as $j => $id) {
foreach ($eventIds as $j => $id) {
if ($jobId) {
if ($attributeId) {
$message = 'Correlating Attribute ' . $attributeId;
@ -2878,10 +2878,10 @@ class Attribute extends AppModel
$this->Job->saveProgress($jobId, $message, $startPercentage + ($j / $eventCount * (100 - $startPercentage)));
}
$event = $this->Event->find('first', array(
'recursive' => -1,
'fields' => array('Event.distribution', 'Event.id', 'Event.info', 'Event.org_id', 'Event.date', 'Event.sharing_group_id', 'Event.disable_correlation'),
'conditions' => array('id' => $id),
'order' => array()
'recursive' => -1,
'fields' => array('Event.distribution', 'Event.id', 'Event.info', 'Event.org_id', 'Event.date', 'Event.sharing_group_id', 'Event.disable_correlation'),
'conditions' => array('id' => $id),
'order' => false,
));
$attributeConditions = array(
'Attribute.event_id' => $id,
@ -3207,19 +3207,6 @@ class Attribute extends AppModel
return $conditions;
}
public function listVisibleAttributes($user, $options = array())
{
$params = array(
'conditions' => $this->buildConditions($user),
'recursive' => -1,
'fields' => array('Attribute.id', 'Attribute.id'),
);
if (isset($options['conditions'])) {
$params['conditions']['AND'][] = $options['conditions'];
}
return $this->find('list', $params);
}
/*
* Unlike the other fetchers, this one foregoes any ACL checks.
* the objective is simple: Fetch the given attribute with all related objects needed for the ZMQ output,
@ -3454,21 +3441,22 @@ class Attribute extends AppModel
}
if (!empty($options['list'])) {
if (!empty($options['event_ids'])) {
$fields = array('Attribute.event_id', 'Attribute.event_id');
$group = array('Attribute.event_id');
return $this->find('column', [
'conditions' => $params['conditions'],
'contain' => array('Event', 'Object'),
'fields' => ['Attribute.event_id'],
'unique' => true,
'sort' => false,
]);
} else {
$fields = array('Attribute.event_id');
$group = false;
return $this->find('list', array(
'conditions' => $params['conditions'],
'recursive' => -1,
'contain' => array('Event', 'Object'),
'fields' => array('Attribute.event_id'),
'sort' => false
));
}
$results = $this->find('list', array(
'conditions' => $params['conditions'],
'recursive' => -1,
'contain' => array('Event', 'Object'),
'fields' => $fields,
'group' => $group,
'sort' => false
));
return $results;
}
if (($options['enforceWarninglist'] || $options['includeWarninglistHits']) && !isset($this->Warninglist)) {

View File

@ -865,12 +865,11 @@ class Sighting extends AppModel
}
// fetch sightings matching the query
$sightingIds = $this->find('list', array(
'recursive' => -1,
$sightingIds = $this->find('column', [
'conditions' => $conditions,
'fields' => array('id'),
'fields' => ['Sighting.id'],
'contain' => $contain,
));
]);
$includeAttribute = isset($filters['includeAttribute']) && $filters['includeAttribute'];
$includeEvent = isset($filters['includeEvent']) && $filters['includeEvent'];
@ -928,11 +927,9 @@ class Sighting extends AppModel
// Since sightings are immutable (it is not possible to change it from web interface), we can check
// if sighting with given uuid already exists and skip them
$existingSighting = $this->find('list', [
'fields' => ['uuid'],
'recursive' => -1,
$existingSighting = $this->find('column', [
'fields' => ['Sighting.uuid'],
'conditions' => ['uuid' => array_column($sightings, 'uuid')],
'callbacks' => false,
]);
// Move UUID to array key
$existingSighting = array_flip($existingSighting);

View File

@ -304,8 +304,7 @@ class Warninglist extends AppModel
if ($id && $warninglist['Warninglist']['id'] != $id) {
continue;
}
$entries = $this->WarninglistEntry->find('list', array(
'recursive' => -1,
$entries = $this->WarninglistEntry->find('column', array(
'conditions' => array('warninglist_id' => $warninglist['Warninglist']['id']),
'fields' => array('value')
));
@ -390,11 +389,10 @@ class Warninglist extends AppModel
if ($redis !== false && $redis->exists('misp:warninglist_entries_cache:' . $id)) {
return $redis->sMembers('misp:warninglist_entries_cache:' . $id);
} else {
$entries = array_values($this->WarninglistEntry->find('list', array(
'recursive' => -1,
$entries = $this->WarninglistEntry->find('column', array(
'conditions' => array('warninglist_id' => $id),
'fields' => array('value')
)));
'fields' => array('WarninglistEntry.value')
));
$this->cacheWarninglistEntries($entries, $id);
return $entries;
}
@ -677,14 +675,13 @@ class Warninglist extends AppModel
*/
public function fetchTLDLists()
{
$tldLists = $this->find('list', array(
$tldLists = $this->find('column', array(
'conditions' => array('Warninglist.name' => $this->__tlds),
'recursive' => -1,
'fields' => array('Warninglist.id', 'Warninglist.id')
'fields' => array('Warninglist.id')
));
$tlds = array();
if (!empty($tldLists)) {
$tlds = $this->WarninglistEntry->find('list', array(
$tlds = $this->WarninglistEntry->find('column', array(
'conditions' => array('WarninglistEntry.warninglist_id' => $tldLists),
'fields' => array('WarninglistEntry.value')
));