Skip to content
Open
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
43 changes: 37 additions & 6 deletions apps/provisioning_api/lib/Controller/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Provisioning_API\Controller;

use OC\AppFramework\Http\PaginationTrait;
use OC\Group\DisplayNameCache as GroupDisplayNameCache;
use OCA\Provisioning_API\ResponseDefinitions;
use OCA\Settings\Settings\Admin\Sharing;
Expand All @@ -30,6 +31,7 @@
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
Expand All @@ -42,6 +44,7 @@
* @psalm-import-type Provisioning_APIUserDetailsGroupDisplayname from ResponseDefinitions
*/
class GroupsController extends AUserDataOCSController {
use PaginationTrait;

public function __construct(
string $appName,
Expand All @@ -56,6 +59,7 @@ public function __construct(
IRootFolder $rootFolder,
private LoggerInterface $logger,
GroupDisplayNameCache $groupDisplayNameCache,
private IURLGenerator $urlGenerator,
) {
parent::__construct($appName,
$request,
Expand All @@ -77,19 +81,28 @@ public function __construct(
* @param string $search Text to search for
* @param ?int $limit Limit the amount of groups returned
* @param int $offset Offset for searching for groups
* @return DataResponse<Http::STATUS_OK, array{groups: list<string>}, array{}>
* @return DataResponse<Http::STATUS_OK, array{groups: list<string>}, array{Link?: string}>
*
* 200: Groups returned
*/
#[NoAdminRequired]
public function getGroups(string $search = '', ?int $limit = null, int $offset = 0): DataResponse {
$groups = $this->groupManager->search($search, $limit, $offset);
$hasMoreResults = $this->hasMoreResults($groups, $limit);
$groups = array_map(function ($group) {
/** @var IGroup $group */
return $group->getGID();
}, $groups);

return new DataResponse(['groups' => $groups]);
$headers = [];
if ($hasMoreResults) {
$headers['Link'] = $this->buildNextPageLinkHeader($this->request, $this->urlGenerator, [
'search' => $search,
'limit' => $limit,
'offset' => $offset + $limit,
]);
}
return new DataResponse(['groups' => $groups], headers: $headers);
}

/**
Expand All @@ -98,7 +111,7 @@ public function getGroups(string $search = '', ?int $limit = null, int $offset =
* @param string $search Text to search for
* @param ?int $limit Limit the amount of groups returned
* @param int $offset Offset for searching for groups
* @return DataResponse<Http::STATUS_OK, array{groups: list<Provisioning_APIGroupDetails>}, array{}>
* @return DataResponse<Http::STATUS_OK, array{groups: list<Provisioning_APIGroupDetails>}, array{Link?: string}>
*
* 200: Groups details returned
*/
Expand All @@ -107,6 +120,7 @@ public function getGroups(string $search = '', ?int $limit = null, int $offset =
#[AuthorizedAdminSetting(settings: Users::class)]
public function getGroupsDetails(string $search = '', ?int $limit = null, int $offset = 0): DataResponse {
$groups = $this->groupManager->search($search, $limit, $offset);
$hasMoreResults = $this->hasMoreResults($groups, $limit);
$groups = array_map(function ($group) {
/** @var IGroup $group */
return [
Expand All @@ -119,7 +133,15 @@ public function getGroupsDetails(string $search = '', ?int $limit = null, int $o
];
}, $groups);

return new DataResponse(['groups' => $groups]);
$headers = [];
if ($hasMoreResults) {
$headers['Link'] = $this->buildNextPageLinkHeader($this->request, $this->urlGenerator, [
'search' => $search,
'limit' => $limit,
'offset' => $offset + $limit,
]);
}
return new DataResponse(['groups' => $groups], headers: $headers);
}

/**
Expand Down Expand Up @@ -191,7 +213,7 @@ public function getGroupUsers(string $groupId): DataResponse {
* @param int|null $limit Limit the amount of groups returned
* @param int $offset Offset for searching for groups
*
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>, groups: list<Provisioning_APIUserDetailsGroupDisplayname>}, array{}>
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>, groups: list<Provisioning_APIUserDetailsGroupDisplayname>}, array{Link?: string}>
* @throws OCSException
*
* 200: Group users details returned
Expand All @@ -214,6 +236,7 @@ public function getGroupUsersDetails(string $groupId, string $search = '', ?int
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($currentUser->getUID());
if ($isAdmin || $isDelegatedAdmin || $isSubadminOfGroup) {
$users = $group->searchUsers($search, $limit, $offset);
$hasMoreResults = $this->hasMoreResults($users, $limit);

// Extract required number
$usersDetails = [];
Expand All @@ -234,10 +257,18 @@ public function getGroupUsersDetails(string $groupId, string $search = '', ?int
// continue if a users ceased to exist.
}
}
$headers = [];
if ($hasMoreResults) {
$headers['Link'] = $this->buildNextPageLinkHeader($this->request, $this->urlGenerator, [
'search' => $search,
'limit' => $limit,
'offset' => $offset + $limit,
]);
}
return new DataResponse([
'users' => $usersDetails,
'groups' => $this->findGroupsWithDisplayname($usersDetails),
]);
], headers: $headers);
}

throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
Expand Down
58 changes: 50 additions & 8 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace OCA\Provisioning_API\Controller;

use InvalidArgumentException;
use OC\AppFramework\Http\PaginationTrait;
use OC\Authentication\Token\RemoteWipe;
use OC\Group\DisplayNameCache as GroupDisplayNameCache;
use OC\Group\Group;
Expand Down Expand Up @@ -62,6 +63,7 @@
* @psalm-import-type Provisioning_APIUserDetailsGroupDisplayname from ResponseDefinitions
*/
class UsersController extends AUserDataOCSController {
use PaginationTrait;

private IL10N $l10n;

Expand Down Expand Up @@ -111,7 +113,7 @@ public function __construct(
* @param string $search Text to search for
* @param int|null $limit Limit the amount of groups returned
* @param int $offset Offset for searching for groups
* @return DataResponse<Http::STATUS_OK, array{users: list<string>}, array{}>
* @return DataResponse<Http::STATUS_OK, array{users: list<string>}, array{Link?: string}>
*
* 200: Users returned
*/
Expand Down Expand Up @@ -139,12 +141,22 @@ public function getUsers(string $search = '', ?int $limit = null, int $offset =
}
}

$hasMoreResults = $this->hasMoreResults($users, $limit);

/** @var list<string> $users */
$users = array_keys($users);

$headers = [];
if ($hasMoreResults) {
$headers['Link'] = $this->buildNextPageLinkHeader($this->request, $this->urlGenerator, [
'search' => $search,
'limit' => $limit,
'offset' => $offset + $limit,
]);
}
return new DataResponse([
'users' => $users
]);
], headers: $headers);
}

/**
Expand All @@ -153,7 +165,7 @@ public function getUsers(string $search = '', ?int $limit = null, int $offset =
* @param string $search Text to search for
* @param int|null $limit Limit the amount of groups returned
* @param int $offset Offset for searching for groups
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>, groups: list<Provisioning_APIUserDetailsGroupDisplayname>}, array{}>
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>, groups: list<Provisioning_APIUserDetailsGroupDisplayname>}, array{Link?: string}>
*
* 200: Users details returned
*/
Expand Down Expand Up @@ -183,6 +195,8 @@ public function getUsersDetails(string $search = '', ?int $limit = null, int $of
$users = array_merge(...$users);
}

$hasMoreResults = $this->hasMoreResults($users, $limit);

$usersDetails = [];
foreach ($users as $userId) {
$userId = (string)$userId;
Expand All @@ -204,10 +218,18 @@ public function getUsersDetails(string $search = '', ?int $limit = null, int $of
}
}

$headers = [];
if ($hasMoreResults) {
$headers['Link'] = $this->buildNextPageLinkHeader($this->request, $this->urlGenerator, [
'search' => $search,
'limit' => $limit,
'offset' => $offset + $limit,
]);
}
return new DataResponse([
'users' => $usersDetails,
'groups' => $this->findGroupsWithDisplayname($usersDetails),
]);
], headers: $headers);
}

/**
Expand All @@ -216,7 +238,7 @@ public function getUsersDetails(string $search = '', ?int $limit = null, int $of
* @param string $search Text to search for
* @param ?int $limit Limit the amount of users returned
* @param int $offset Offset
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>}, array{}>
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>}, array{Link?: string}>
*
* 200: Disabled users details returned
*/
Expand Down Expand Up @@ -267,6 +289,8 @@ public function getDisabledUsersDetails(string $search = '', ?int $limit = null,
$users = array_slice($users, $offset, $limit);
}

$hasMoreResults = $this->hasMoreResults($users, $limit);

$usersDetails = [];
foreach ($users as $userId) {
try {
Expand All @@ -287,9 +311,17 @@ public function getDisabledUsersDetails(string $search = '', ?int $limit = null,
}
}

$headers = [];
if ($hasMoreResults) {
$headers['Link'] = $this->buildNextPageLinkHeader($this->request, $this->urlGenerator, [
'search' => $search,
'limit' => $limit,
'offset' => $offset + $limit,
]);
}
return new DataResponse([
'users' => $usersDetails
]);
], headers: $headers);
}

/**
Expand All @@ -298,7 +330,7 @@ public function getDisabledUsersDetails(string $search = '', ?int $limit = null,
* @param string $search Text to search for
* @param ?int $limit Limit the amount of users returned
* @param int $offset Offset
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>}, array{}>
* @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>}, array{Link?: string}>
*
* 200: Users details returned based on last logged in information
*/
Expand All @@ -324,6 +356,8 @@ public function getLastLoggedInUsers(
// For Admin alone user sorting based on lastLogin. For sub admin and groups this is not supported
$users = $this->userManager->getLastLoggedInUsers($limit, $offset, $search);

$hasMoreResults = $this->hasMoreResults($users, $limit);

$usersDetails = [];
foreach ($users as $userId) {
try {
Expand All @@ -344,9 +378,17 @@ public function getLastLoggedInUsers(
}
}

$headers = [];
if ($hasMoreResults) {
$headers['Link'] = $this->buildNextPageLinkHeader($this->request, $this->urlGenerator, [
'search' => $search,
'limit' => $limit,
'offset' => $offset + $limit,
]);
}
return new DataResponse([
'users' => $usersDetails
]);
], headers: $headers);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions apps/provisioning_api/openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,13 @@
"responses": {
"200": {
"description": "Users details returned based on last logged in information",
"headers": {
"Link": {
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
Expand Down
Loading
Loading