diff --git a/src/Controller/Admin/AccessLogsController.php b/src/Controller/Admin/AccessLogsController.php index 8e3c8dcd8..aea37842d 100644 --- a/src/Controller/Admin/AccessLogsController.php +++ b/src/Controller/Admin/AccessLogsController.php @@ -3,19 +3,19 @@ namespace App\Controller\Admin; use App\Controller\AppController; +use App\Model\Entity\AccessLog; use Cake\Core\Configure; use Cake\Http\Exception\NotFoundException; class AccessLogsController extends AppController { + protected $fields = ['id', 'created', 'user_id', 'org_id', 'authkey_id', 'ip', 'request_method', 'user_agent', 'request_id', 'controller', 'action', 'url', 'response_code', 'memory_usage', 'duration', 'query_count', 'request']; + protected $contain = [ + 'Users' => ['fields' => ['id', 'email', 'org_id']], + 'Organisations' => ['fields' => ['id', 'name', 'uuid']], + ]; public $paginate = [ - 'recursive' => -1, 'limit' => 60, - 'fields' => ['id', 'created', 'user_id', 'org_id', 'authkey_id', 'ip', 'request_method', 'user_agent', 'request_id', 'controller', 'action', 'url', 'response_code', 'memory_usage', 'duration', 'query_count', 'request'], - 'contain' => [ - 'Users' => ['fields' => ['id', 'email', 'org_id']], - 'Organisations' => ['fields' => ['id', 'name', 'uuid']], - ], 'order' => [ 'AccessLogs.id' => 'DESC' ], @@ -77,7 +77,7 @@ class AccessLogsController extends AppController ] ); - // $conditions = $this->__searchConditions($params); + $conditions = $this->__searchConditions($params); $afterFindHandler = function ($entry) { if (!empty($entry['request'])) { @@ -91,6 +91,9 @@ class AccessLogsController extends AppController 'filters' => $this->filterFields, 'quickFilters' => $this->quickFilterFields, 'afterFind' => $afterFindHandler, + 'conditions' => $conditions, + 'contain' => $this->contain, + 'fields' => $this->fields, ] ); @@ -108,8 +111,8 @@ class AccessLogsController extends AppController $request = $this->AccessLogs->find( 'all', [ - 'conditions' => ['AccessLogs.id' => $id], - 'fields' => ['AccessLogs.request'], + 'conditions' => ['id' => $id], + 'fields' => ['request'], ] )->first(); if (empty($request)) { @@ -164,4 +167,111 @@ class AccessLogsController extends AppController { $this->CRUD->filtering(); } + + /** + * @param array $params + * @return array + */ + private function __searchConditions(array $params) + { + $qbRules = []; + foreach ($params as $key => $value) { + if ($key === 'created') { + $qbRules[] = [ + 'id' => $key, + 'operator' => is_array($value) ? 'between' : 'greater_or_equal', + 'value' => $value, + ]; + } else { + if (is_array($value)) { + $value = implode('||', $value); + } + $qbRules[] = [ + 'id' => $key, + 'value' => $value, + ]; + } + } + $this->set('qbRules', $qbRules); + + $conditions = []; + if (isset($params['user'])) { + if (is_numeric($params['user'])) { + $conditions['user_id'] = $params['user']; + } else { + $user = $this->User->find( + 'first', + [ + 'conditions' => ['User.email' => $params['user']], + 'fields' => ['id'], + ] + ); + if (!empty($user)) { + $conditions['user_id'] = $user['User']['id']; + } else { + $conditions['user_id'] = -1; + } + } + } + if (isset($params['ip'])) { + $conditions['ip'] = inet_pton($params['ip']); + } + foreach (['authkey_id', 'request_id', 'controller', 'action'] as $field) { + if (isset($params[$field])) { + $conditions['' . $field] = $params[$field]; + } + } + if (isset($params['url'])) { + $conditions['url LIKE'] = "%{$params['url']}%"; + } + if (isset($params['user_agent'])) { + $conditions['user_agent LIKE'] = "%{$params['user_agent']}%"; + } + if (isset($params['memory_usage'])) { + $conditions['memory_usage >='] = ($params['memory_usage'] * 1024); + } + if (isset($params['memory_usage'])) { + $conditions['memory_usage >='] = ($params['memory_usage'] * 1024); + } + if (isset($params['duration'])) { + $conditions['duration >='] = $params['duration']; + } + if (isset($params['query_count'])) { + $conditions['query_count >='] = $params['query_count']; + } + if (isset($params['request_method'])) { + $methodId = array_flip(AccessLog::REQUEST_TYPES)[$params['request_method']] ?? -1; + $conditions['request_method'] = $methodId; + } + if (isset($params['org'])) { + if (is_numeric($params['org'])) { + $conditions['org_id'] = $params['org']; + } else { + $org = $this->AccessLog->Organisation->fetchOrg($params['org']); + if ($org) { + $conditions['org_id'] = $org['id']; + } else { + $conditions['org_id'] = -1; + } + } + } + if (isset($params['created'])) { + $tempData = is_array($params['created']) ? $params['created'] : [$params['created']]; + foreach ($tempData as $k => $v) { + $tempData[$k] = $this->AccessLog->resolveTimeDelta($v); + } + if (count($tempData) === 1) { + $conditions['created >='] = date("Y-m-d H:i:s", $tempData[0]); + } else { + if ($tempData[0] < $tempData[1]) { + $temp = $tempData[1]; + $tempData[1] = $tempData[0]; + $tempData[0] = $temp; + } + $conditions['AND'][] = ['created <=' => date("Y-m-d H:i:s", $tempData[0])]; + $conditions['AND'][] = ['created >=' => date("Y-m-d H:i:s", $tempData[1])]; + } + } + return $conditions; + } } diff --git a/src/Controller/Admin/AuditLogsController.php b/src/Controller/Admin/AuditLogsController.php index c061fa08f..fac12db99 100644 --- a/src/Controller/Admin/AuditLogsController.php +++ b/src/Controller/Admin/AuditLogsController.php @@ -59,13 +59,15 @@ class AuditLogsController extends AppController 'WorkflowBlueprint', ]; + // Pagination + protected $fields = ['id', 'created', 'user_id', 'org_id', 'request_action', 'model', 'model_id', 'model_title', 'event_id', 'changed']; + protected $contain = [ + 'Users' => ['fields' => ['id', 'email', 'org_id']], + 'Organisations' => ['fields' => ['id', 'name', 'uuid']], + ]; + protected $conditions = []; public $paginate = [ 'limit' => 60, - 'fields' => ['id', 'created', 'user_id', 'org_id', 'request_action', 'model', 'model_id', 'model_title', 'event_id', 'changed'], - 'contain' => [ - 'Users' => ['fields' => ['id', 'email', 'org_id']], - 'Organisations' => ['fields' => ['id', 'name', 'uuid']], - ], 'order' => [ 'id' => 'DESC' ], @@ -110,12 +112,14 @@ class AuditLogsController extends AppController public function index() { - $this->paginate['fields'][] = 'ip'; - $this->paginate['fields'][] = 'request_type'; - $this->paginate['fields'][] = 'authkey_id'; + + + $this->fields[] = 'ip'; + $this->fields[] = 'request_type'; + $this->fields[] = 'authkey_id'; if ($this->ParamHandler->isRest()) { - $this->paginate['fields'][] = 'request_id'; + $this->fields[] = 'request_id'; } if (!Configure::read('MISP.log_new_audit')) { $this->Flash->warning(__("Audit log is not enabled. See 'MISP.log_new_audit' in the Server Settings. (Administration -> Server Settings -> MISP tab)")); @@ -137,12 +141,22 @@ class AuditLogsController extends AppController ] ); - $this->paginate['conditions'] = $this->__searchConditions($params); + $this->conditions = $this->__searchConditions($params); $acl = $this->__applyAuditACL($this->ACL->getUser()->toArray()); if ($acl) { - $this->paginate['conditions']['AND'][] = $acl; + $this->conditions['AND'][] = $acl; } - $list = $this->paginate()->toArray(); + + $query = $this->AuditLogs->find( + 'all', + [ + 'conditions' => $this->conditions, + 'fields' => $this->fields, + 'contain' => $this->contain, + ] + ); + + $list = $this->paginate($query)->toArray(); if ($this->ParamHandler->isRest()) { return $this->RestResponse->viewData($list, 'json'); diff --git a/src/Controller/GalaxyClustersController.php b/src/Controller/GalaxyClustersController.php index 6bea4fea8..dd038a57b 100644 --- a/src/Controller/GalaxyClustersController.php +++ b/src/Controller/GalaxyClustersController.php @@ -18,30 +18,30 @@ class GalaxyClustersController extends AppController { use LocatorAwareTrait; + protected $conditions = []; + protected $contain = [ + 'Tag' => [ + 'fields' => ['Tag.id'], + /* + 'EventTag' => array( + 'fields' => array('EventTag.event_id') + ), + 'AttributeTag' => array( + 'fields' => array('AttributeTag.event_id', 'AttributeTag.attribute_id') + ) + */ + ], + 'GalaxyElement' => [ + 'conditions' => ['GalaxyElement.key' => 'synonyms'], + 'fields' => ['value'] + ], + ]; public $paginate = [ 'limit' => 60, - 'recursive' => -1, 'order' => [ 'GalaxyClusters.version' => 'DESC', 'GalaxyClusters.value' => 'ASC' ], - 'contain' => [ - 'Tag' => [ - 'fields' => ['Tag.id'], - /* - 'EventTag' => array( - 'fields' => array('EventTag.event_id') - ), - 'AttributeTag' => array( - 'fields' => array('AttributeTag.event_id', 'AttributeTag.attribute_id') - ) - */ - ], - 'GalaxyElement' => [ - 'conditions' => ['GalaxyElement.key' => 'synonyms'], - 'fields' => ['value'] - ], - ] ]; public function initialize(): void @@ -124,11 +124,19 @@ class GalaxyClustersController extends AppController return $this->RestResponse->viewData($clusters, $this->response->getType()); } - $this->paginate['conditions']['AND'][] = $contextConditions; - $this->paginate['conditions']['AND'][] = $searchConditions; - $this->paginate['conditions']['AND'][] = $aclConditions; - $this->paginate['contain'] = array_merge($this->paginate['contain'], ['Org', 'Orgc', 'SharingGroup', 'GalaxyClusterRelation', 'TargetingClusterRelation']); - $clusters = $this->paginate(); + $this->conditions['AND'][] = $contextConditions; + $this->conditions['AND'][] = $searchConditions; + $this->conditions['AND'][] = $aclConditions; + $this->contain = array_merge($this->contain, ['Org', 'Orgc', 'SharingGroup', 'GalaxyClusterRelation', 'TargetingClusterRelation']); + + $query = $this->GalaxyClusters->find( + 'all', + [ + 'conditions' => $this->conditions, + 'contain' => $this->contain + ] + ); + $clusters = $this->paginate($query); $this->GalaxyClusters->attachExtendByInfo($this->ACL->getUser()->toArray(), $clusters); diff --git a/src/Controller/GalaxyElementsController.php b/src/Controller/GalaxyElementsController.php index 7a8ab91bb..04cfc5860 100644 --- a/src/Controller/GalaxyElementsController.php +++ b/src/Controller/GalaxyElementsController.php @@ -10,7 +10,6 @@ class GalaxyElementsController extends AppController { public $paginate = [ 'limit' => 20, - 'recursive' => -1, 'order' => [ 'GalaxyElement.key' => 'ASC' ] diff --git a/src/Controller/JobsController.php b/src/Controller/JobsController.php index c982e6908..5d68ef7bd 100644 --- a/src/Controller/JobsController.php +++ b/src/Controller/JobsController.php @@ -15,17 +15,17 @@ class JobsController extends AppController { use LocatorAwareTrait; + protected $conditions = []; + protected $contain = [ + 'Organisations' => [ + 'fields' => ['id', 'name', 'uuid'], + ], + ]; public $paginate = [ 'limit' => 20, - 'recursive' => 0, 'order' => [ 'Job.id' => 'DESC' ], - 'contain' => [ - 'Organisations' => [ - 'fields' => ['id', 'name', 'uuid'], - ], - ] ]; public function beforeFilter(EventInterface $event) @@ -46,9 +46,16 @@ class JobsController extends AppController $workers = $ServerTable->workerDiagnostics($issueCount); $queues = ['email', 'default', 'cache', 'prio', 'update']; if ($queue && in_array($queue, $queues, true)) { - $this->paginate['conditions'] = ['Job.worker' => $queue]; + $this->conditions = ['Job.worker' => $queue]; } - $jobs = $this->paginate()->toArray(); + $query = $this->Jobs->find( + 'all', + [ + 'conditions' => $this->conditions, + 'contain' => $this->contain, + ] + ); + $jobs = $this->paginate($query)->toArray(); foreach ($jobs as &$job) { if (!empty($job['process_id'])) { $job['job_status'] = $this->getJobStatus($job['process_id']); diff --git a/src/Controller/ObjectTemplateElementsController.php b/src/Controller/ObjectTemplateElementsController.php index e71dfd9c7..855a6367b 100644 --- a/src/Controller/ObjectTemplateElementsController.php +++ b/src/Controller/ObjectTemplateElementsController.php @@ -6,17 +6,16 @@ use App\Controller\AppController; class ObjectTemplateElementsController extends AppController { - public $paginate = array( + public $paginate = [ 'limit' => 60, - 'order' => array( + 'order' => [ 'ObjectTemplateElement.id' => 'desc' - ), - 'recursive' => -1 - ); + ], + ]; public function viewElements($id, $context = 'all') { - $this->paginate['conditions'] = array('ObjectTemplateElements.object_template_id' => $id); + $this->paginate['conditions'] = ['ObjectTemplateElements.object_template_id' => $id]; $elements = $this->paginate(); $this->set('list', $elements); $this->layout = false; diff --git a/src/Controller/ObjectTemplatesController.php b/src/Controller/ObjectTemplatesController.php index 440b705a6..4bedf24c6 100644 --- a/src/Controller/ObjectTemplatesController.php +++ b/src/Controller/ObjectTemplatesController.php @@ -22,7 +22,6 @@ class ObjectTemplatesController extends AppController 'order' => [ 'Object.id' => 'desc' ], - 'recursive' => -1 ]; public function beforeFilter(EventInterface $event) @@ -41,17 +40,20 @@ class ObjectTemplatesController extends AppController $metas = $this->ObjectTemplate->find( 'column', [ - 'conditions' => ['ObjectTemplate.active' => 1], - 'fields' => ['ObjectTemplate.meta_category'], - 'order' => ['ObjectTemplate.meta_category asc'], - 'unique' => true, + 'conditions' => ['ObjectTemplate.active' => 1], + 'fields' => ['ObjectTemplate.meta_category'], + 'order' => ['ObjectTemplate.meta_category asc'], + 'unique' => true, ] ); - $items = [[ - 'name' => __('All Objects'), - 'value' => $this->baseurl . "/ObjectTemplates/objectChoice/$eventId/0" - ]]; + $items = [ + [ + 'name' => __('All Objects'), + 'value' => $this->baseurl . "/ObjectTemplates/objectChoice/$eventId/0" + ] + + ]; foreach ($metas as $meta) { $items[] = [ 'name' => $meta, @@ -63,7 +65,7 @@ class ObjectTemplatesController extends AppController $this->set( 'options', [ - 'multiple' => 0, + 'multiple' => 0, ] ); $this->render('/Elements/generic_picker'); @@ -80,10 +82,10 @@ class ObjectTemplatesController extends AppController $templates_raw = $this->ObjectTemplate->find( 'all', [ - 'recursive' => -1, - 'conditions' => $conditions, - 'fields' => ['id', 'meta_category', 'name', 'description'], - 'order' => ['ObjectTemplate.name asc'] + 'recursive' => -1, + 'conditions' => $conditions, + 'fields' => ['id', 'meta_category', 'name', 'description'], + 'order' => ['ObjectTemplate.name asc'] ] ); @@ -105,11 +107,11 @@ class ObjectTemplatesController extends AppController $this->set( 'options', [ - 'functionName' => 'redirectAddObject', - 'multiple' => 0, - 'select_options' => [ - 'additionalData' => ['event_id' => $event_id], - ], + 'functionName' => 'redirectAddObject', + 'multiple' => 0, + 'select_options' => [ + 'additionalData' => ['event_id' => $event_id], + ], ] ); $this->render('/Elements/generic_picker'); @@ -121,10 +123,10 @@ class ObjectTemplatesController extends AppController $temp = $this->ObjectTemplates->find( 'all', [ - 'recursive' => -1, - 'conditions' => ['ObjectTemplates.uuid' => $id], - 'fields' => ['ObjectTemplates.id', 'ObjectTemplates.uuid'], - 'order' => ['ObjectTemplates.version desc'] + 'recursive' => -1, + 'conditions' => ['ObjectTemplates.uuid' => $id], + 'fields' => ['ObjectTemplates.id', 'ObjectTemplates.uuid'], + 'order' => ['ObjectTemplates.version desc'] ] )->first(); if (empty($temp)) { @@ -191,12 +193,14 @@ class ObjectTemplatesController extends AppController $conditions['ObjectTemplates.active'] = 1; } - $this->CRUD->index([ - 'filters' => $this->filterFields, - 'quickFilters' => $this->quickFilterFields, - 'quickFilterForMetaField' => ['enabled' => true, 'wildcard_search' => true], - 'conditions' => $conditions - ]); + $this->CRUD->index( + [ + 'filters' => $this->filterFields, + 'quickFilters' => $this->quickFilterFields, + 'quickFilterForMetaField' => ['enabled' => true, 'wildcard_search' => true], + 'conditions' => $conditions + ] + ); $responsePayload = $this->CRUD->getResponsePayload(); @@ -234,14 +238,14 @@ class ObjectTemplatesController extends AppController } $logEntry = $this->Log->newEntity( [ - 'org' => $this->ACL->getUser()->Organisation->name, - 'model' => 'ObjectTemplate', - 'model_id' => $id, - 'email' => $this->ACL->getUser()->email, - 'action' => 'update', - 'user_id' => $this->ACL->getUser()->id, - 'title' => 'Object template updated', - 'change' => $change, + 'org' => $this->ACL->getUser()->Organisation->name, + 'model' => 'ObjectTemplate', + 'model_id' => $id, + 'email' => $this->ACL->getUser()->email, + 'action' => 'update', + 'user_id' => $this->ACL->getUser()->id, + 'title' => 'Object template updated', + 'change' => $change, ] ); $this->Log->save($logEntry); @@ -252,14 +256,14 @@ class ObjectTemplatesController extends AppController foreach ($result['fails'] as $id => $fail) { $logEntry = $this->Log->newEntity( [ - 'org' => $this->ACL->getUser()->Organisation->name, - 'model' => 'ObjectTemplate', - 'model_id' => $id, - 'email' => $this->ACL->getUser()->email, - 'action' => 'update', - 'user_id' => $this->Auth->user('id'), - 'title' => 'Object template failed to update', - 'change' => $fail['name'] . ' could not be installed/updated. Error: ' . $fail['fail'], + 'org' => $this->ACL->getUser()->Organisation->name, + 'model' => 'ObjectTemplate', + 'model_id' => $id, + 'email' => $this->ACL->getUser()->email, + 'action' => 'update', + 'user_id' => $this->Auth->user('id'), + 'title' => 'Object template failed to update', + 'change' => $fail['name'] . ' could not be installed/updated. Error: ' . $fail['fail'], ] ); $this->Log->save($logEntry); @@ -269,14 +273,14 @@ class ObjectTemplatesController extends AppController } else { $logEntry = $this->Log->newEntity( [ - 'org' => $this->ACL->getUser()->Organisation->name, - 'model' => 'ObjectTemplate', - 'model_id' => 0, - 'email' => $this->ACL->getUser()->email, - 'action' => 'update', - 'user_id' => $this->ACL->getUser()->id, - 'title' => 'Object template update (nothing to update)', - 'change' => 'Executed an update of the Object Template library, but there was nothing to update.', + 'org' => $this->ACL->getUser()->Organisation->name, + 'model' => 'ObjectTemplate', + 'model_id' => 0, + 'email' => $this->ACL->getUser()->email, + 'action' => 'update', + 'user_id' => $this->ACL->getUser()->id, + 'title' => 'Object template update (nothing to update)', + 'change' => 'Executed an update of the Object Template library, but there was nothing to update.', ] ); $this->Log->save($logEntry); diff --git a/src/Controller/SharingGroupsController.php b/src/Controller/SharingGroupsController.php index 7c5858d76..16c0201a8 100644 --- a/src/Controller/SharingGroupsController.php +++ b/src/Controller/SharingGroupsController.php @@ -28,7 +28,10 @@ class SharingGroupsController extends AppController public $filterFields = [ 'name', 'uuid', 'releasability', 'description', 'active', 'created', 'modified', 'SharingGroups.local', 'roaming', ['name' => 'Organisations.name', 'multiple' => true], ]; - public $containFields = [ + public $statisticsFields = ['active', 'roaming']; + + protected $fields = ['id', 'uuid', 'name', 'description', 'releasability', 'local', 'active', 'roaming']; + protected $contain = [ 'SharingGroupOrgs' => [ 'Organisations' => ['fields' => ['name', 'id', 'uuid']] ], @@ -42,29 +45,11 @@ class SharingGroupsController extends AppController ] ] ]; - public $statisticsFields = ['active', 'roaming']; - public $paginate = [ 'limit' => 60, - 'maxLimit' => 9999, 'order' => [ 'SharingGroup.name' => 'ASC' ], - 'fields' => ['id', 'uuid', 'name', 'description', 'releasability', 'local', 'active', 'roaming'], - 'contain' => [ - 'SharingGroupOrgs' => [ - 'Organisations' => ['fields' => ['name', 'id', 'uuid']] - ], - 'Organisations' => [ - 'fields' => ['id', 'name', 'uuid'], - ], - 'SharingGroupServers' => [ - 'fields' => ['sharing_group_id', 'all_orgs'], - 'Servers' => [ - 'fields' => ['name', 'id'] - ] - ] - ], ]; public function add() @@ -269,7 +254,7 @@ class SharingGroupsController extends AppController $this->render('add'); } - public function delete($id=false) + public function delete($id = false) { $this->request->allowMethod(['get', 'post', 'delete']); $toggleParams = [ @@ -280,9 +265,10 @@ class SharingGroupsController extends AppController ['path' => 'releasability', 'label' => __('Releasability')], ['path' => 'active', 'label' => __('Active'), 'element' => 'boolean',], ['path' => 'roaming', 'label' => __('Roaming'), 'element' => 'boolean',], - ['path' => 'org_count', 'label' => __('Org. count'), 'formatter' => function ($field, $row) { - return count($row['SharingGroupOrg']); - } + [ + 'path' => 'org_count', 'label' => __('Org. count'), 'formatter' => function ($field, $row) { + return count($row['SharingGroupOrg']); + } ], ], ]; @@ -320,10 +306,10 @@ class SharingGroupsController extends AppController ] ]; - $containFields = $this->containFields; + $containFields = $this->contain; $validFilterFields = $this->CRUD->getFilterFieldsName($this->filterFields); if (!$this->__showOrgs()) { - $validFilterFields = array_filter($validFilterFields, fn($filter) => $filter != 'Organisations.name'); + $validFilterFields = array_filter($validFilterFields, fn ($filter) => $filter != 'Organisations.name'); unset($containFields['SharingGroupOrgs']); unset($containFields['SharingGroupServers']); } @@ -355,6 +341,7 @@ class SharingGroupsController extends AppController 'custom' => $customContextFilters, ], 'contain' => $containFields, + 'fields' => $this->fields, 'afterFind' => $afterFindHandler, 'statisticsFields' => $this->statisticsFields, 'wrapResponse' => true, @@ -402,9 +389,10 @@ class SharingGroupsController extends AppController ['path' => 'releasability', 'label' => __('Releasability')], ['path' => 'active', 'label' => __('Active'), 'element' => 'boolean',], ['path' => 'roaming', 'label' => __('Roaming'), 'element' => 'boolean',], - ['path' => 'org_count', 'label' => __('Org. count'), 'formatter' => function ($field, $row) { - return count($row['SharingGroupOrg']); - } + [ + 'path' => 'org_count', 'label' => __('Org. count'), 'formatter' => function ($field, $row) { + return count($row['SharingGroupOrg']); + } ], ], ]; @@ -468,7 +456,7 @@ class SharingGroupsController extends AppController unset($contain['SharingGroupServers']); } - $afterFindHandler = function(SharingGroup $sg) { + $afterFindHandler = function (SharingGroup $sg) { if (isset($sg->SharingGroupServer)) { foreach ($sg->SharingGroupServer as $key => $sgs) { if ($sgs['server_id'] == 0) { @@ -487,9 +475,10 @@ class SharingGroupsController extends AppController 'conditions' => ['Users.id' => $sg->sync_user_id], 'recursive' => -1, 'fields' => ['Users.id'], - 'contain' => ['Organisations' => [ - 'fields' => ['Organisations.id', 'Organisations.name', 'Organisations.uuid'], - ] + 'contain' => [ + 'Organisations' => [ + 'fields' => ['Organisations.id', 'Organisations.name', 'Organisations.uuid'], + ] ] ] )->first(); @@ -508,7 +497,7 @@ class SharingGroupsController extends AppController return $sg; }; - $conditions= []; + $conditions = []; $params = [ 'contain' => $contain, 'conditions' => $conditions, diff --git a/src/Controller/TaxonomiesController.php b/src/Controller/TaxonomiesController.php index ee9e60e50..cd410c4f4 100644 --- a/src/Controller/TaxonomiesController.php +++ b/src/Controller/TaxonomiesController.php @@ -13,14 +13,16 @@ class TaxonomiesController extends AppController { use LocatorAwareTrait; + protected $conditions = []; + protected $contain = [ + 'TaxonomyPredicates' => [ + 'fields' => ['TaxonomyPredicates.id', 'TaxonomyPredicates.taxonomy_id', 'TaxonomyPredicates.value'], + 'TaxonomyEntries' => ['fields' => ['TaxonomyEntries.id', 'TaxonomyEntries.taxonomy_predicate_id', 'TaxonomyEntries.value']] + ] + ]; + protected $fields = []; public $paginate = [ 'limit' => 60, - 'contain' => [ - 'TaxonomyPredicates' => [ - 'fields' => ['TaxonomyPredicates.id', 'TaxonomyPredicates.taxonomy_id', 'TaxonomyPredicates.value'], - 'TaxonomyEntries' => ['fields' => ['TaxonomyEntries.id', 'TaxonomyEntries.taxonomy_predicate_id', 'TaxonomyEntries.value']] - ] - ], 'order' => [ 'Taxonomies.id' => 'DESC' ], @@ -31,24 +33,26 @@ class TaxonomiesController extends AppController $this->paginate['recursive'] = -1; if (!empty($this->request->getQueryParams()['value'])) { - $this->paginate['conditions']['id'] = $this->__search($this->request->getQueryParams()['value']); + $this->conditions['id'] = $this->__search($this->request->getQueryParams()['value']); } if (isset($this->request->getQueryParams()['enabled'])) { - $this->paginate['conditions']['enabled'] = $this->request->getQueryParams()['enabled'] ? 1 : 0; + $this->conditions['enabled'] = $this->request->getQueryParams()['enabled'] ? 1 : 0; } + $query = $this->Taxonomies->find( + 'all', + [ + 'conditions' => $this->conditions, + 'contain' => $this->contain, + 'fields' => $this->fields + ] + ); + if ($this->ParamHandler->isRest()) { - $keepFields = ['conditions', 'contain', 'recursive', 'sort']; - $searchParams = []; - foreach ($keepFields as $field) { - if (!empty($this->paginate[$field])) { - $searchParams[$field] = $this->paginate[$field]; - } - } - $taxonomies = $this->Taxonomies->find('all', $searchParams); + $taxonomies = $query; } else { - $taxonomies = $this->paginate(); + $taxonomies = $this->paginate($query); } $taxonomies = $this->__tagCount($taxonomies->toArray()); diff --git a/tests/TestCase/Controller/Users/UsersControllerTest.php b/tests/TestCase/Controller/Users/UsersControllerTest.php index 8c6d1c191..155ce71c0 100644 --- a/tests/TestCase/Controller/Users/UsersControllerTest.php +++ b/tests/TestCase/Controller/Users/UsersControllerTest.php @@ -14,7 +14,8 @@ class UsersControllerTest extends TestCase protected $fixtures = [ 'app.Organisations', - 'app.Users' + 'app.Users', + 'app.Roles', ]; public function testLogin(): void