Skip to content

Commit

Permalink
Fix sort roles by name (#5285)
Browse files Browse the repository at this point in the history
fixes #4789
  • Loading branch information
nilmerg authored Nov 5, 2024
2 parents eea50d7 + 8551fff commit 9f64509
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
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']];

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'
);
}
}

0 comments on commit 9f64509

Please sign in to comment.