Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sort roles by name #5285

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions application/controllers/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -53,8 +51,6 @@ public function indexAction()

/**
* List roles
*
* @TODO(el): Rename to indexAction()
*/
public function listAction()
{
Expand All @@ -63,22 +59,25 @@ 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']);
$this->setupSortControl($sortAndFilterColumns, $this->view->roles);
}

/**
* Create a new role
*
* @TODO(el): Rename to newAction()
*/
public function addAction()
{
Expand All @@ -93,8 +92,6 @@ public function addAction()

/**
* Update a role
*
* @TODO(el): Rename to updateAction()
*/
public function editAction()
{
Expand Down
2 changes: 2 additions & 0 deletions library/Icinga/Authentication/RolesConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class RolesConfig extends IniRepository
{
protected $sortRules = ['name' => ['order' => 'asc']];

Check failure on line 11 in library/Icinga/Authentication/RolesConfig.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Property Icinga\Authentication\RolesConfig::$sortRules type has no value type specified in iterable type array.

Check failure on line 11 in library/Icinga/Authentication/RolesConfig.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Property Icinga\Authentication\RolesConfig::$sortRules type has no value type specified in iterable type array.

Check failure on line 11 in library/Icinga/Authentication/RolesConfig.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property Icinga\Authentication\RolesConfig::$sortRules type has no value type specified in iterable type array.

Check failure on line 11 in library/Icinga/Authentication/RolesConfig.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Authentication\RolesConfig::$sortRules type has no value type specified in iterable type array.

Check failure on line 11 in library/Icinga/Authentication/RolesConfig.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Property Icinga\Authentication\RolesConfig::$sortRules type has no value type specified in iterable type array.

Check failure on line 11 in library/Icinga/Authentication/RolesConfig.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Property Icinga\Authentication\RolesConfig::$sortRules type has no value type specified in iterable type array.

Check failure on line 11 in library/Icinga/Authentication/RolesConfig.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Property Icinga\Authentication\RolesConfig::$sortRules type has no value type specified in iterable type array.

protected $configs = [
'roles' => [
'name' => 'roles',
Expand Down
16 changes: 10 additions & 6 deletions library/Icinga/Data/DataArray/ArrayDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions test/php/library/Icinga/Data/DataArray/ArrayDatasourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
}
Loading