Merge pull request #9475 from righel/3.x-pagination-settings

fix: paginator component deprecation notices
3.x-inbox
Luciano Righetti 2024-01-04 14:23:22 +01:00 committed by GitHub
commit d3126af70e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 298 additions and 163 deletions

View File

@ -3,19 +3,19 @@
namespace App\Controller\Admin; namespace App\Controller\Admin;
use App\Controller\AppController; use App\Controller\AppController;
use App\Model\Entity\AccessLog;
use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Http\Exception\NotFoundException; use Cake\Http\Exception\NotFoundException;
class AccessLogsController extends AppController 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 = [ public $paginate = [
'recursive' => -1,
'limit' => 60, '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' => [ 'order' => [
'AccessLogs.id' => 'DESC' 'AccessLogs.id' => 'DESC'
], ],
@ -77,7 +77,7 @@ class AccessLogsController extends AppController
] ]
); );
// $conditions = $this->__searchConditions($params); $conditions = $this->__searchConditions($params);
$afterFindHandler = function ($entry) { $afterFindHandler = function ($entry) {
if (!empty($entry['request'])) { if (!empty($entry['request'])) {
@ -91,6 +91,9 @@ class AccessLogsController extends AppController
'filters' => $this->filterFields, 'filters' => $this->filterFields,
'quickFilters' => $this->quickFilterFields, 'quickFilters' => $this->quickFilterFields,
'afterFind' => $afterFindHandler, 'afterFind' => $afterFindHandler,
'conditions' => $conditions,
'contain' => $this->contain,
'fields' => $this->fields,
] ]
); );
@ -108,8 +111,8 @@ class AccessLogsController extends AppController
$request = $this->AccessLogs->find( $request = $this->AccessLogs->find(
'all', 'all',
[ [
'conditions' => ['AccessLogs.id' => $id], 'conditions' => ['id' => $id],
'fields' => ['AccessLogs.request'], 'fields' => ['request'],
] ]
)->first(); )->first();
if (empty($request)) { if (empty($request)) {
@ -164,4 +167,111 @@ class AccessLogsController extends AppController
{ {
$this->CRUD->filtering(); $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;
}
} }

View File

@ -59,13 +59,15 @@ class AuditLogsController extends AppController
'WorkflowBlueprint', '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 = [ public $paginate = [
'limit' => 60, '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' => [ 'order' => [
'id' => 'DESC' 'id' => 'DESC'
], ],
@ -110,12 +112,14 @@ class AuditLogsController extends AppController
public function index() 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()) { if ($this->ParamHandler->isRest()) {
$this->paginate['fields'][] = 'request_id'; $this->fields[] = 'request_id';
} }
if (!Configure::read('MISP.log_new_audit')) { 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)")); $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()); $acl = $this->__applyAuditACL($this->ACL->getUser()->toArray());
if ($acl) { 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()) { if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData($list, 'json'); return $this->RestResponse->viewData($list, 'json');

View File

@ -18,30 +18,30 @@ class GalaxyClustersController extends AppController
{ {
use LocatorAwareTrait; 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 = [ public $paginate = [
'limit' => 60, 'limit' => 60,
'recursive' => -1,
'order' => [ 'order' => [
'GalaxyClusters.version' => 'DESC', 'GalaxyClusters.version' => 'DESC',
'GalaxyClusters.value' => 'ASC' '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 public function initialize(): void
@ -124,11 +124,19 @@ class GalaxyClustersController extends AppController
return $this->RestResponse->viewData($clusters, $this->response->getType()); return $this->RestResponse->viewData($clusters, $this->response->getType());
} }
$this->paginate['conditions']['AND'][] = $contextConditions; $this->conditions['AND'][] = $contextConditions;
$this->paginate['conditions']['AND'][] = $searchConditions; $this->conditions['AND'][] = $searchConditions;
$this->paginate['conditions']['AND'][] = $aclConditions; $this->conditions['AND'][] = $aclConditions;
$this->paginate['contain'] = array_merge($this->paginate['contain'], ['Org', 'Orgc', 'SharingGroup', 'GalaxyClusterRelation', 'TargetingClusterRelation']); $this->contain = array_merge($this->contain, ['Org', 'Orgc', 'SharingGroup', 'GalaxyClusterRelation', 'TargetingClusterRelation']);
$clusters = $this->paginate();
$query = $this->GalaxyClusters->find(
'all',
[
'conditions' => $this->conditions,
'contain' => $this->contain
]
);
$clusters = $this->paginate($query);
$this->GalaxyClusters->attachExtendByInfo($this->ACL->getUser()->toArray(), $clusters); $this->GalaxyClusters->attachExtendByInfo($this->ACL->getUser()->toArray(), $clusters);

View File

@ -10,7 +10,6 @@ class GalaxyElementsController extends AppController
{ {
public $paginate = [ public $paginate = [
'limit' => 20, 'limit' => 20,
'recursive' => -1,
'order' => [ 'order' => [
'GalaxyElement.key' => 'ASC' 'GalaxyElement.key' => 'ASC'
] ]

View File

@ -15,17 +15,17 @@ class JobsController extends AppController
{ {
use LocatorAwareTrait; use LocatorAwareTrait;
protected $conditions = [];
protected $contain = [
'Organisations' => [
'fields' => ['id', 'name', 'uuid'],
],
];
public $paginate = [ public $paginate = [
'limit' => 20, 'limit' => 20,
'recursive' => 0,
'order' => [ 'order' => [
'Job.id' => 'DESC' 'Job.id' => 'DESC'
], ],
'contain' => [
'Organisations' => [
'fields' => ['id', 'name', 'uuid'],
],
]
]; ];
public function beforeFilter(EventInterface $event) public function beforeFilter(EventInterface $event)
@ -46,9 +46,16 @@ class JobsController extends AppController
$workers = $ServerTable->workerDiagnostics($issueCount); $workers = $ServerTable->workerDiagnostics($issueCount);
$queues = ['email', 'default', 'cache', 'prio', 'update']; $queues = ['email', 'default', 'cache', 'prio', 'update'];
if ($queue && in_array($queue, $queues, true)) { 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) { foreach ($jobs as &$job) {
if (!empty($job['process_id'])) { if (!empty($job['process_id'])) {
$job['job_status'] = $this->getJobStatus($job['process_id']); $job['job_status'] = $this->getJobStatus($job['process_id']);

View File

@ -6,17 +6,16 @@ use App\Controller\AppController;
class ObjectTemplateElementsController extends AppController class ObjectTemplateElementsController extends AppController
{ {
public $paginate = array( public $paginate = [
'limit' => 60, 'limit' => 60,
'order' => array( 'order' => [
'ObjectTemplateElement.id' => 'desc' 'ObjectTemplateElement.id' => 'desc'
), ],
'recursive' => -1 ];
);
public function viewElements($id, $context = 'all') 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(); $elements = $this->paginate();
$this->set('list', $elements); $this->set('list', $elements);
$this->layout = false; $this->layout = false;

View File

@ -22,7 +22,6 @@ class ObjectTemplatesController extends AppController
'order' => [ 'order' => [
'Object.id' => 'desc' 'Object.id' => 'desc'
], ],
'recursive' => -1
]; ];
public function beforeFilter(EventInterface $event) public function beforeFilter(EventInterface $event)
@ -41,17 +40,20 @@ class ObjectTemplatesController extends AppController
$metas = $this->ObjectTemplate->find( $metas = $this->ObjectTemplate->find(
'column', 'column',
[ [
'conditions' => ['ObjectTemplate.active' => 1], 'conditions' => ['ObjectTemplate.active' => 1],
'fields' => ['ObjectTemplate.meta_category'], 'fields' => ['ObjectTemplate.meta_category'],
'order' => ['ObjectTemplate.meta_category asc'], 'order' => ['ObjectTemplate.meta_category asc'],
'unique' => true, 'unique' => true,
] ]
); );
$items = [[ $items = [
'name' => __('All Objects'), [
'value' => $this->baseurl . "/ObjectTemplates/objectChoice/$eventId/0" 'name' => __('All Objects'),
]]; 'value' => $this->baseurl . "/ObjectTemplates/objectChoice/$eventId/0"
]
];
foreach ($metas as $meta) { foreach ($metas as $meta) {
$items[] = [ $items[] = [
'name' => $meta, 'name' => $meta,
@ -63,7 +65,7 @@ class ObjectTemplatesController extends AppController
$this->set( $this->set(
'options', 'options',
[ [
'multiple' => 0, 'multiple' => 0,
] ]
); );
$this->render('/Elements/generic_picker'); $this->render('/Elements/generic_picker');
@ -80,10 +82,10 @@ class ObjectTemplatesController extends AppController
$templates_raw = $this->ObjectTemplate->find( $templates_raw = $this->ObjectTemplate->find(
'all', 'all',
[ [
'recursive' => -1, 'recursive' => -1,
'conditions' => $conditions, 'conditions' => $conditions,
'fields' => ['id', 'meta_category', 'name', 'description'], 'fields' => ['id', 'meta_category', 'name', 'description'],
'order' => ['ObjectTemplate.name asc'] 'order' => ['ObjectTemplate.name asc']
] ]
); );
@ -105,11 +107,11 @@ class ObjectTemplatesController extends AppController
$this->set( $this->set(
'options', 'options',
[ [
'functionName' => 'redirectAddObject', 'functionName' => 'redirectAddObject',
'multiple' => 0, 'multiple' => 0,
'select_options' => [ 'select_options' => [
'additionalData' => ['event_id' => $event_id], 'additionalData' => ['event_id' => $event_id],
], ],
] ]
); );
$this->render('/Elements/generic_picker'); $this->render('/Elements/generic_picker');
@ -121,10 +123,10 @@ class ObjectTemplatesController extends AppController
$temp = $this->ObjectTemplates->find( $temp = $this->ObjectTemplates->find(
'all', 'all',
[ [
'recursive' => -1, 'recursive' => -1,
'conditions' => ['ObjectTemplates.uuid' => $id], 'conditions' => ['ObjectTemplates.uuid' => $id],
'fields' => ['ObjectTemplates.id', 'ObjectTemplates.uuid'], 'fields' => ['ObjectTemplates.id', 'ObjectTemplates.uuid'],
'order' => ['ObjectTemplates.version desc'] 'order' => ['ObjectTemplates.version desc']
] ]
)->first(); )->first();
if (empty($temp)) { if (empty($temp)) {
@ -191,12 +193,14 @@ class ObjectTemplatesController extends AppController
$conditions['ObjectTemplates.active'] = 1; $conditions['ObjectTemplates.active'] = 1;
} }
$this->CRUD->index([ $this->CRUD->index(
'filters' => $this->filterFields, [
'quickFilters' => $this->quickFilterFields, 'filters' => $this->filterFields,
'quickFilterForMetaField' => ['enabled' => true, 'wildcard_search' => true], 'quickFilters' => $this->quickFilterFields,
'conditions' => $conditions 'quickFilterForMetaField' => ['enabled' => true, 'wildcard_search' => true],
]); 'conditions' => $conditions
]
);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
@ -234,14 +238,14 @@ class ObjectTemplatesController extends AppController
} }
$logEntry = $this->Log->newEntity( $logEntry = $this->Log->newEntity(
[ [
'org' => $this->ACL->getUser()->Organisation->name, 'org' => $this->ACL->getUser()->Organisation->name,
'model' => 'ObjectTemplate', 'model' => 'ObjectTemplate',
'model_id' => $id, 'model_id' => $id,
'email' => $this->ACL->getUser()->email, 'email' => $this->ACL->getUser()->email,
'action' => 'update', 'action' => 'update',
'user_id' => $this->ACL->getUser()->id, 'user_id' => $this->ACL->getUser()->id,
'title' => 'Object template updated', 'title' => 'Object template updated',
'change' => $change, 'change' => $change,
] ]
); );
$this->Log->save($logEntry); $this->Log->save($logEntry);
@ -252,14 +256,14 @@ class ObjectTemplatesController extends AppController
foreach ($result['fails'] as $id => $fail) { foreach ($result['fails'] as $id => $fail) {
$logEntry = $this->Log->newEntity( $logEntry = $this->Log->newEntity(
[ [
'org' => $this->ACL->getUser()->Organisation->name, 'org' => $this->ACL->getUser()->Organisation->name,
'model' => 'ObjectTemplate', 'model' => 'ObjectTemplate',
'model_id' => $id, 'model_id' => $id,
'email' => $this->ACL->getUser()->email, 'email' => $this->ACL->getUser()->email,
'action' => 'update', 'action' => 'update',
'user_id' => $this->Auth->user('id'), 'user_id' => $this->Auth->user('id'),
'title' => 'Object template failed to update', 'title' => 'Object template failed to update',
'change' => $fail['name'] . ' could not be installed/updated. Error: ' . $fail['fail'], 'change' => $fail['name'] . ' could not be installed/updated. Error: ' . $fail['fail'],
] ]
); );
$this->Log->save($logEntry); $this->Log->save($logEntry);
@ -269,14 +273,14 @@ class ObjectTemplatesController extends AppController
} else { } else {
$logEntry = $this->Log->newEntity( $logEntry = $this->Log->newEntity(
[ [
'org' => $this->ACL->getUser()->Organisation->name, 'org' => $this->ACL->getUser()->Organisation->name,
'model' => 'ObjectTemplate', 'model' => 'ObjectTemplate',
'model_id' => 0, 'model_id' => 0,
'email' => $this->ACL->getUser()->email, 'email' => $this->ACL->getUser()->email,
'action' => 'update', 'action' => 'update',
'user_id' => $this->ACL->getUser()->id, 'user_id' => $this->ACL->getUser()->id,
'title' => 'Object template update (nothing to update)', 'title' => 'Object template update (nothing to update)',
'change' => 'Executed an update of the Object Template library, but there was nothing to update.', 'change' => 'Executed an update of the Object Template library, but there was nothing to update.',
] ]
); );
$this->Log->save($logEntry); $this->Log->save($logEntry);

View File

@ -28,7 +28,10 @@ class SharingGroupsController extends AppController
public $filterFields = [ public $filterFields = [
'name', 'uuid', 'releasability', 'description', 'active', 'created', 'modified', 'SharingGroups.local', 'roaming', ['name' => 'Organisations.name', 'multiple' => true], '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' => [ 'SharingGroupOrgs' => [
'Organisations' => ['fields' => ['name', 'id', 'uuid']] 'Organisations' => ['fields' => ['name', 'id', 'uuid']]
], ],
@ -42,29 +45,11 @@ class SharingGroupsController extends AppController
] ]
] ]
]; ];
public $statisticsFields = ['active', 'roaming'];
public $paginate = [ public $paginate = [
'limit' => 60, 'limit' => 60,
'maxLimit' => 9999,
'order' => [ 'order' => [
'SharingGroup.name' => 'ASC' '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() public function add()
@ -269,7 +254,7 @@ class SharingGroupsController extends AppController
$this->render('add'); $this->render('add');
} }
public function delete($id=false) public function delete($id = false)
{ {
$this->request->allowMethod(['get', 'post', 'delete']); $this->request->allowMethod(['get', 'post', 'delete']);
$toggleParams = [ $toggleParams = [
@ -280,9 +265,10 @@ class SharingGroupsController extends AppController
['path' => 'releasability', 'label' => __('Releasability')], ['path' => 'releasability', 'label' => __('Releasability')],
['path' => 'active', 'label' => __('Active'), 'element' => 'boolean',], ['path' => 'active', 'label' => __('Active'), 'element' => 'boolean',],
['path' => 'roaming', 'label' => __('Roaming'), '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); $validFilterFields = $this->CRUD->getFilterFieldsName($this->filterFields);
if (!$this->__showOrgs()) { 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['SharingGroupOrgs']);
unset($containFields['SharingGroupServers']); unset($containFields['SharingGroupServers']);
} }
@ -355,6 +341,7 @@ class SharingGroupsController extends AppController
'custom' => $customContextFilters, 'custom' => $customContextFilters,
], ],
'contain' => $containFields, 'contain' => $containFields,
'fields' => $this->fields,
'afterFind' => $afterFindHandler, 'afterFind' => $afterFindHandler,
'statisticsFields' => $this->statisticsFields, 'statisticsFields' => $this->statisticsFields,
'wrapResponse' => true, 'wrapResponse' => true,
@ -402,9 +389,10 @@ class SharingGroupsController extends AppController
['path' => 'releasability', 'label' => __('Releasability')], ['path' => 'releasability', 'label' => __('Releasability')],
['path' => 'active', 'label' => __('Active'), 'element' => 'boolean',], ['path' => 'active', 'label' => __('Active'), 'element' => 'boolean',],
['path' => 'roaming', 'label' => __('Roaming'), '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']); unset($contain['SharingGroupServers']);
} }
$afterFindHandler = function(SharingGroup $sg) { $afterFindHandler = function (SharingGroup $sg) {
if (isset($sg->SharingGroupServer)) { if (isset($sg->SharingGroupServer)) {
foreach ($sg->SharingGroupServer as $key => $sgs) { foreach ($sg->SharingGroupServer as $key => $sgs) {
if ($sgs['server_id'] == 0) { if ($sgs['server_id'] == 0) {
@ -487,9 +475,10 @@ class SharingGroupsController extends AppController
'conditions' => ['Users.id' => $sg->sync_user_id], 'conditions' => ['Users.id' => $sg->sync_user_id],
'recursive' => -1, 'recursive' => -1,
'fields' => ['Users.id'], 'fields' => ['Users.id'],
'contain' => ['Organisations' => [ 'contain' => [
'fields' => ['Organisations.id', 'Organisations.name', 'Organisations.uuid'], 'Organisations' => [
] 'fields' => ['Organisations.id', 'Organisations.name', 'Organisations.uuid'],
]
] ]
] ]
)->first(); )->first();
@ -508,7 +497,7 @@ class SharingGroupsController extends AppController
return $sg; return $sg;
}; };
$conditions= []; $conditions = [];
$params = [ $params = [
'contain' => $contain, 'contain' => $contain,
'conditions' => $conditions, 'conditions' => $conditions,

View File

@ -13,14 +13,16 @@ class TaxonomiesController extends AppController
{ {
use LocatorAwareTrait; 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 = [ public $paginate = [
'limit' => 60, 'limit' => 60,
'contain' => [
'TaxonomyPredicates' => [
'fields' => ['TaxonomyPredicates.id', 'TaxonomyPredicates.taxonomy_id', 'TaxonomyPredicates.value'],
'TaxonomyEntries' => ['fields' => ['TaxonomyEntries.id', 'TaxonomyEntries.taxonomy_predicate_id', 'TaxonomyEntries.value']]
]
],
'order' => [ 'order' => [
'Taxonomies.id' => 'DESC' 'Taxonomies.id' => 'DESC'
], ],
@ -31,24 +33,26 @@ class TaxonomiesController extends AppController
$this->paginate['recursive'] = -1; $this->paginate['recursive'] = -1;
if (!empty($this->request->getQueryParams()['value'])) { 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'])) { 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()) { if ($this->ParamHandler->isRest()) {
$keepFields = ['conditions', 'contain', 'recursive', 'sort']; $taxonomies = $query;
$searchParams = [];
foreach ($keepFields as $field) {
if (!empty($this->paginate[$field])) {
$searchParams[$field] = $this->paginate[$field];
}
}
$taxonomies = $this->Taxonomies->find('all', $searchParams);
} else { } else {
$taxonomies = $this->paginate(); $taxonomies = $this->paginate($query);
} }
$taxonomies = $this->__tagCount($taxonomies->toArray()); $taxonomies = $this->__tagCount($taxonomies->toArray());

View File

@ -14,7 +14,8 @@ class UsersControllerTest extends TestCase
protected $fixtures = [ protected $fixtures = [
'app.Organisations', 'app.Organisations',
'app.Users' 'app.Users',
'app.Roles',
]; ];
public function testLogin(): void public function testLogin(): void