From 01bcf71e376c305a0845c9d181eaa8f80215b2af Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 5 Nov 2024 08:47:46 +0100 Subject: [PATCH 1/4] ArrayDatasource: Restore order by key column Broke with 384d9535a990c23fef3abc73657cb15931c2eba5 --- .../Icinga/Data/DataArray/ArrayDatasource.php | 16 ++++++----- .../Data/DataArray/ArrayDatasourceTest.php | 27 +++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/library/Icinga/Data/DataArray/ArrayDatasource.php b/library/Icinga/Data/DataArray/ArrayDatasource.php index e30061634a..b67d74c171 100644 --- a/library/Icinga/Data/DataArray/ArrayDatasource.php +++ b/library/Icinga/Data/DataArray/ArrayDatasource.php @@ -196,7 +196,16 @@ protected function createResult(SimpleQuery $query) $filter = $query->getFilter(); $offset = $query->hasOffset() ? $query->getOffset() : 0; $limit = $query->hasLimit() ? $query->getLimit() : 0; - $data = $this->data; + + $data = []; + foreach ($this->data as $key => $row) { + if ($this->keyColumn !== null && ! isset($row->{$this->keyColumn})) { + $row = clone $row; // Make sure that this won't affect the actual data + $row->{$this->keyColumn} = $key; + } + + $data[$key] = $row; + } if ($query->hasOrder()) { uasort($data, [$query, 'compare']); @@ -206,11 +215,6 @@ protected function createResult(SimpleQuery $query) $result = []; $skipped = 0; foreach ($data as $key => $row) { - if ($this->keyColumn !== null && !isset($row->{$this->keyColumn})) { - $row = clone $row; // Make sure that this won't affect the actual data - $row->{$this->keyColumn} = $key; - } - if (! $filter->matches($row)) { continue; } elseif ($skipped < $offset) { diff --git a/test/php/library/Icinga/Data/DataArray/ArrayDatasourceTest.php b/test/php/library/Icinga/Data/DataArray/ArrayDatasourceTest.php index a40ad30d1d..4c1fca3a48 100644 --- a/test/php/library/Icinga/Data/DataArray/ArrayDatasourceTest.php +++ b/test/php/library/Icinga/Data/DataArray/ArrayDatasourceTest.php @@ -132,4 +132,31 @@ public function testOrderIsCorrectWithLimitAndOffset() 'ArrayDatasource does not sort limited queries correctly' ); } + + public function testOrderByKeyColumnIsCorrect() + { + $result = (new ArrayDatasource([ + 'a' => (object) [ + 'foo' => 'bar', + 'baz' => 'qux' + ], + 'b' => (object) [ + 'foo' => 'bar', + 'baz' => 'qux' + ], + 'c' => (object) [ + 'foo' => 'bar', + 'baz' => 'qux' + ] + ]))->setKeyColumn('name') + ->select() + ->order('name', 'desc') + ->fetchAll(); + + $this->assertSame( + ['c', 'b', 'a'], + array_keys($result), + 'ArrayDatasource does not sort queries correctly by key column' + ); + } } From 1612b78af6c7ba87b6a4c0d99532571a9770a63f Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 5 Nov 2024 08:59:27 +0100 Subject: [PATCH 2/4] RoleController: Remove todos, the issue is closed --- application/controllers/RoleController.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/application/controllers/RoleController.php b/application/controllers/RoleController.php index 4223d33976..18b10d494f 100644 --- a/application/controllers/RoleController.php +++ b/application/controllers/RoleController.php @@ -25,8 +25,6 @@ /** * Manage user permissions and restrictions based on roles - * - * @TODO(el): Rename to RolesController: https://dev.icinga.com/issues/10015 */ class RoleController extends AuthBackendController { @@ -53,8 +51,6 @@ public function indexAction() /** * List roles - * - * @TODO(el): Rename to indexAction() */ public function listAction() { @@ -77,8 +73,6 @@ public function listAction() /** * Create a new role - * - * @TODO(el): Rename to newAction() */ public function addAction() { @@ -93,8 +87,6 @@ public function addAction() /** * Update a role - * - * @TODO(el): Rename to updateAction() */ public function editAction() { From aba56d8ef16741def499ada118b41de24ae68c8c Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 5 Nov 2024 09:00:03 +0100 Subject: [PATCH 3/4] RoleController: Clean up sort rules * Removes `permissions` from sort rules * Adds `parent` to filter and sort rules --- application/controllers/RoleController.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/application/controllers/RoleController.php b/application/controllers/RoleController.php index 18b10d494f..2b277a9f00 100644 --- a/application/controllers/RoleController.php +++ b/application/controllers/RoleController.php @@ -59,13 +59,18 @@ public function listAction() ->select(); $sortAndFilterColumns = [ - 'name' => $this->translate('Name'), - 'users' => $this->translate('Users'), - 'groups' => $this->translate('Groups'), - 'permissions' => $this->translate('Permissions') + 'name' => $this->translate('Name'), + 'users' => $this->translate('Users'), + 'groups' => $this->translate('Groups'), + 'parent' => $this->translate('Inherits From') ]; - $this->setupFilterControl($this->view->roles, $sortAndFilterColumns, ['name']); + $this->setupFilterControl( + $this->view->roles, + $sortAndFilterColumns + [ + 'permissions' => $this->translate('Permissions') + ] + ); $this->setupLimitControl(); $this->setupPaginationControl($this->view->roles); $this->setupSortControl($sortAndFilterColumns, $this->view->roles, ['name']); From 8551fffd4f933f42840e680a220660695cb034af Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 5 Nov 2024 09:02:19 +0100 Subject: [PATCH 4/4] roles: Fix default sort rule by name --- application/controllers/RoleController.php | 2 +- library/Icinga/Authentication/RolesConfig.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/application/controllers/RoleController.php b/application/controllers/RoleController.php index 2b277a9f00..ac3412a6e1 100644 --- a/application/controllers/RoleController.php +++ b/application/controllers/RoleController.php @@ -73,7 +73,7 @@ public function listAction() ); $this->setupLimitControl(); $this->setupPaginationControl($this->view->roles); - $this->setupSortControl($sortAndFilterColumns, $this->view->roles, ['name']); + $this->setupSortControl($sortAndFilterColumns, $this->view->roles); } /** diff --git a/library/Icinga/Authentication/RolesConfig.php b/library/Icinga/Authentication/RolesConfig.php index ac5695f118..b1773598fe 100644 --- a/library/Icinga/Authentication/RolesConfig.php +++ b/library/Icinga/Authentication/RolesConfig.php @@ -8,6 +8,8 @@ class RolesConfig extends IniRepository { + protected $sortRules = ['name' => ['order' => 'asc']]; + protected $configs = [ 'roles' => [ 'name' => 'roles',