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

Restrict who can use the lastSeenAt user sort #2634

Merged
merged 9 commits into from
Mar 2, 2021
7 changes: 7 additions & 0 deletions src/Api/Controller/ListUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ protected function data(ServerRequestInterface $request, Document $document)

$actor->assertCan('viewUserList');

if (! $actor->hasPermission('user.viewLastSeenAt')) {
// If a user cannot see everyone's last online date, we prevent them from sorting by it
// Otherwise this sort field would defeat the privacy setting discloseOnline
// We use remove instead of add so that extensions can still completely disable the sort using the extender
$this->removeSortField('lastSeenAt');
}

$filters = $this->extractFilter($request);
$sort = $this->extractSort($request);

Expand Down
43 changes: 43 additions & 0 deletions tests/integration/api/users/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,49 @@ public function shows_full_results_without_search_or_filter()
$this->assertEquals(['1', '2'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function allows_last_seen_sorting_with_permission()
{
$this->prepareDatabase([
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 2],
['permission' => 'user.viewLastSeenAt', 'group_id' => 2],
],
]);

$response = $this->send(
$this->request('GET', '/api/users')
->withQueryParams([
'sort' => 'lastSeenAt',
])
);

$this->assertEquals(200, $response->getStatusCode());
}

/**
* @test
*/
public function disallows_last_seen_sorting_without_permission()
{
$this->prepareDatabase([
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 2],
],
]);

$response = $this->send(
$this->request('GET', '/api/users')
->withQueryParams([
'sort' => 'lastSeenAt',
])
);

$this->assertEquals(400, $response->getStatusCode());
}

/**
* @test
*/
Expand Down