diff options
author | yemkareems <yemkareems@gmail.com> | 2024-06-06 16:35:44 +0530 |
---|---|---|
committer | yemkareems <yemkareems@gmail.com> | 2024-07-08 15:42:55 +0530 |
commit | 4cb85f7c9e37af742d170373602709f8db2d525d (patch) | |
tree | 8d63eaabb24787cda4afc4c8b44b3914c4351dec /apps/provisioning_api/lib | |
parent | 76c875a5882753e37ef39015a1711b68e7ea0d0b (diff) | |
download | nextcloud-server-4cb85f7c9e37af742d170373602709f8db2d525d.tar.gz nextcloud-server-4cb85f7c9e37af742d170373602709f8db2d525d.zip |
fix: rebased the branch with master and resolved conflicts
fix: added a new endpoint users/recent and getting users based on last login info in the same. Reverted old code that was breaking LDAP
Signed-off-by: yemkareems <yemkareems@gmail.com>
Diffstat (limited to 'apps/provisioning_api/lib')
-rw-r--r-- | apps/provisioning_api/lib/Controller/UsersController.php | 102 |
1 files changed, 95 insertions, 7 deletions
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index eced881516f..ec57d3e9b23 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -94,7 +94,7 @@ class UsersController extends AUserData { * * 200: Users returned */ - public function getUsers(string $search = '', ?int $limit = null, int $offset = 0, string $sortMode = 'uid', string $sortOrder = 'asc'): DataResponse { + public function getUsers(string $search = '', ?int $limit = null, int $offset = 0): DataResponse { $user = $this->userSession->getUser(); $users = []; @@ -102,7 +102,7 @@ class UsersController extends AUserData { $uid = $user->getUID(); $subAdminManager = $this->groupManager->getSubAdmin(); if ($this->groupManager->isAdmin($uid)) { - $users = $this->userManager->search($search, $limit, $offset, $sortMode, $sortOrder); + $users = $this->userManager->search($search, $limit, $offset); } elseif ($subAdminManager->isSubAdmin($user)) { $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); foreach ($subAdminOfGroups as $key => $group) { @@ -131,13 +131,11 @@ class UsersController extends AUserData { * @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 - * @param string $sortMode Field to order the results with - * @param string $sortOrder asc or desc * @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>}, array{}> * * 200: Users details returned */ - public function getUsersDetails(string $search = '', ?int $limit = null, int $offset = 0, string $sortMode = 'uid', string $sortOrder = 'asc'): DataResponse { + public function getUsersDetails(string $search = '', ?int $limit = null, int $offset = 0): DataResponse { $currentUser = $this->userSession->getUser(); $users = []; @@ -145,7 +143,7 @@ class UsersController extends AUserData { $uid = $currentUser->getUID(); $subAdminManager = $this->groupManager->getSubAdmin(); if ($this->groupManager->isAdmin($uid)) { - $users = $this->userManager->search($search, $limit, $offset, $sortMode, $sortOrder); + $users = $this->userManager->search($search, $limit, $offset); $users = array_keys($users); } elseif ($subAdminManager->isSubAdmin($currentUser)) { $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser); @@ -155,7 +153,7 @@ class UsersController extends AUserData { $users = []; foreach ($subAdminOfGroups as $group) { - $users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset, $sortMode, $sortOrder)); + $users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); } $users = array_merge(...$users); } @@ -267,6 +265,96 @@ class UsersController extends AUserData { ]); } + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * Get the list of disabled users and their details + * + * @param string $search Text to search for + * @param ?int $limit Limit the amount of users returned + * @param int $offset Offset + * @param string $sortMode Field to order the results with + * @param string $sortOrder asc or desc + * @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>}, array{}> + * + * 200: Users details returned based on last logged in information + */ + public function getLastLoggedInUsers(string $search = '', + ?int $limit = null, + int $offset = 0, + string $sortMode = 'lastLogin', + string $sortOrder = 'desc' + ): DataResponse { + $currentUser = $this->userSession->getUser(); + if ($currentUser === null) { + return new DataResponse(['users' => []]); + } + if ($limit !== null && $limit < 0) { + throw new InvalidArgumentException("Invalid limit value: $limit"); + } + if ($offset < 0) { + throw new InvalidArgumentException("Invalid offset value: $offset"); + } + + $users = []; + + // Admin? Or SubAdmin? + $uid = $currentUser->getUID(); + $subAdminManager = $this->groupManager->getSubAdmin(); + if ($this->groupManager->isAdmin($uid)) { + $users = $this->userManager->getUsersSortedByLastLogin($limit, $offset, $search, $sortMode, $sortOrder); + $users = array_map(fn (IUser $user): string => $user->getUID(), $users); + } elseif ($subAdminManager->isSubAdmin($currentUser)) { + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser); + + $users = []; + /* We have to handle offset ourselve for correctness */ + $tempLimit = ($limit === null ? null : $limit + $offset); + foreach ($subAdminOfGroups as $group) { + $users = array_merge( + $users, + array_map( + fn (IUser $user): string => $user->getUID(), + array_filter( + $group->searchUsers($search, ($tempLimit === null ? null : $tempLimit - count($users))), + fn (IUser $user): bool => !$user->isEnabled() + ) + ) + ); + if (($tempLimit !== null) && (count($users) >= $tempLimit)) { + break; + } + } + $users = array_slice($users, $offset); + } + + $usersDetails = []; + foreach ($users as $userId) { + try { + $userData = $this->getUserData($userId); + } catch (OCSNotFoundException $e) { + // We still want to return all other accounts, but this one was removed from the backends + // yet they are still in our database. Might be a LDAP remnant. + $userData = null; + $this->logger->warning('Found one disabled account that was removed from its backend, but still exists in Nextcloud database', ['accountId' => $userId]); + } + // Do not insert empty entry + if ($userData !== null) { + $usersDetails[$userId] = $userData; + } else { + // Currently logged in user does not have permissions to see this user + // only showing its id + $usersDetails[$userId] = ['id' => $userId]; + } + } + + return new DataResponse([ + 'users' => $usersDetails + ]); + } + + /** * @NoAdminRequired |