]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix: removed references to old disabled users code. refactored query as per getDispla...
authoryemkareems <yemkareems@gmail.com>
Thu, 4 Jul 2024 07:08:40 +0000 (12:38 +0530)
committeryemkareems <yemkareems@gmail.com>
Mon, 8 Jul 2024 10:12:55 +0000 (15:42 +0530)
Signed-off-by: yemkareems <yemkareems@gmail.com>
apps/provisioning_api/lib/Controller/UsersController.php
lib/private/AllConfig.php
lib/private/User/Manager.php
lib/public/IConfig.php

index 18c24f47caec475fc28a6f4452a8c2a85627fa8e..beb240681d0ae78eb11848f2d11f29334e29aae9 100644 (file)
@@ -278,7 +278,7 @@ class UsersController extends AUserData {
         * 200: Users details returned based on last logged in information
         */
        public function getLastLoggedInUsers(string $search = '',
-               ?int   $limit = null,
+               ?int   $limit = 25,
                int    $offset = 0,
        ): DataResponse {
                $currentUser = $this->userSession->getUser();
@@ -294,34 +294,11 @@ class UsersController extends AUserData {
 
                $users = [];
 
-               // Admin? Or SubAdmin?
+               // For Admin alone user sorting based on lastLogin. For sub admin and groups this is not supported
                $uid = $currentUser->getUID();
-               $subAdminManager = $this->groupManager->getSubAdmin();
                if ($this->groupManager->isAdmin($uid)) {
                        $users = $this->userManager->getUsersSortedByLastLogin($limit, $offset, $search);
                        $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 = [];
@@ -332,13 +309,13 @@ class UsersController extends AUserData {
                                // 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]);
+                               $this->logger->warning('Found one 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
+                               // Currently logged-in user does not have permissions to see this user
                                // only showing its id
                                $usersDetails[$userId] = ['id' => $userId];
                        }
index 756ceabdbea00f58d7eff2095eb968dbdc07c9a4..87b9c795f49a4751217ad89639e1f4c28ad40786 100644 (file)
@@ -494,38 +494,32 @@ class AllConfig implements IConfig {
        /**
         * Gets the list of users based on their lastLogin info asc or desc
         *
+        * @param int $limit how many users to fetch
+        * @param int $offset from which offset to fetch
         * @param string $search search users based on search params
         * @return array of user IDs
         */
-       public function getLastLoggedInUsers($search): array {
+       public function getLastLoggedInUsers(int $limit = 25, int $offset = 0, string $search = ''): array {
                // TODO - FIXME
                $this->fixDIInit();
 
                $query = $this->connection->getQueryBuilder();
 
-               $lastLoginSubSelect = $this->connection->getQueryBuilder();
-               $lastLoginSubSelect->select('configvalue')
-                       ->from('preferences', 'p2')
-                       ->where($lastLoginSubSelect->expr()->andX(
-                               $lastLoginSubSelect->expr()->eq('p2.userid', 'uid'),
-                               $lastLoginSubSelect->expr()->eq('p2.appid', $lastLoginSubSelect->expr()->literal('login')),
-                               $lastLoginSubSelect->expr()->eq('p2.configkey', $lastLoginSubSelect->expr()->literal('lastLogin')),
-                       ));
-               $orderByExpression = $query->createFunction('(' . $lastLoginSubSelect->getSQL() .')');
-
-               $query->select('uid', 'displayname', $orderByExpression)
+               $query->select('uid', 'displayname')
                        ->from('users', 'u')
                        ->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
                                $query->expr()->eq('userid', 'uid'),
-                               $query->expr()->eq('appid', $query->expr()->literal('settings')),
-                               $query->expr()->eq('configkey', $query->expr()->literal('email')))
+                               $query->expr()->eq('appid', $query->expr()->literal('login')),
+                               $query->expr()->eq('configkey', $query->expr()->literal('lastLogin')))
                        )
                        // sqlite doesn't like re-using a single named parameter here
                        ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
                        ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
                        ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')))
-                       ->orderBy($orderByExpression, 'DESC')
-                       ->addOrderBy('uid_lower', 'ASC');
+                       ->orderBy($query->func()->lower('configvalue'), 'DESC')
+                       ->addOrderBy('uid_lower', 'ASC')
+                       ->setFirstResult($offset)
+                       ->setMaxResults($limit);
 
                $result = $query->executeQuery();
                $displayNames = [];
index 0ae24397f5fbe54759652404f4a48331a81cd5b8..93ba7f0eb6e8fa9139616f83aec3c9093544bc91 100644 (file)
@@ -345,30 +345,15 @@ class Manager extends PublicEmitter implements IUserManager {
        /**
         * @return IUser[]
         */
-       public function getUsersSortedByLastLogin(?int $limit = null, int $offset = 0, $search = ''): array {
-               $users = $this->config->getLastLoggedInUsers($search);
-               $users = array_combine(
+       public function getUsersSortedByLastLogin(?int $limit = 25, int $offset = 0, $search = ''): array {
+               $users = $this->config->getLastLoggedInUsers($limit, $offset, $search);
+               return array_combine(
                        $users,
                        array_map(
                                fn (string $uid): IUser => new LazyUser($uid, $this),
                                $users
                        )
                );
-
-               $tempLimit = ($limit === null ? null : $limit + $offset);
-               foreach ($this->backends as $backend) {
-                       if (($tempLimit !== null) && (count($users) >= $tempLimit)) {
-                               break;
-                       }
-                       if ($backend instanceof IProvideEnabledStateBackend) {
-                               $backendUsers = $backend->getDisabledUserList(($tempLimit === null ? null : $tempLimit - count($users)));
-                               foreach ($backendUsers as $uid) {
-                                       $users[$uid] = new LazyUser($uid, $this, null, $backend);
-                               }
-                       }
-               }
-
-               return array_slice($users, $offset, $limit);
        }
 
 
index a91fe1e62b020d68de8d249aae0b047645a889c5..b74292855a82b9436a1dd923af6a146f8d1d8153 100644 (file)
@@ -253,9 +253,11 @@ interface IConfig {
        /**
         * Gets the list of users based on their lastLogin info asc or desc
         *
+        * @param int $limit how many records to fetch
+        * @param int $offset from which offset to fetch
         * @param string $search search users based on search params
         * @return array of user IDs
         * @since 30.0.0
         */
-       public function getLastLoggedInUsers($search);
+       public function getLastLoggedInUsers(int $limit, int $offset, string $search): array;
 }