diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 1d3c1006a59ff..1cdc139259693 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -293,7 +293,7 @@ public function getLastLoggedInUsers(string $search = '', $users = []; // For Admin alone user sorting based on lastLogin. For sub admin and groups this is not supported - $users = $this->config->getLastLoggedInUsers($limit, $offset, $search); + $users = $this->userManager->getLastLoggedInUsers($limit, $offset, $search); $usersDetails = []; foreach ($users as $userId) { diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php index c2fff700e10f3..d05fe440202dd 100644 --- a/lib/private/AllConfig.php +++ b/lib/private/AllConfig.php @@ -491,51 +491,6 @@ public function getUsersForUserValue($appName, $key, $value) { return $userIDs; } - /** - * Gets the list of user ids sorted by lastLogin, from most recent to least recent - * - * @param int|null $limit how many users to fetch - * @param int $offset from which offset to fetch - * @param string $search search users based on search params - * @return list list of user IDs - */ - public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array { - // TODO - FIXME - $this->fixDIInit(); - - $query = $this->connection->getQueryBuilder(); - $query->selectDistinct('uid') - ->from('users', 'u') - ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( - $query->expr()->eq('p.userid', 'uid'), - $query->expr()->eq('p.appid', $query->expr()->literal('login')), - $query->expr()->eq('p.configkey', $query->expr()->literal('lastLogin'))) - ); - if($search !== '') { - $query->leftJoin('u', 'preferences', 'p1', $query->expr()->andX( - $query->expr()->eq('p1.userid', 'uid'), - $query->expr()->eq('p1.appid', $query->expr()->literal('settings')), - $query->expr()->eq('p1.configkey', $query->expr()->literal('email'))) - ) - // 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('p1.configvalue', $query->createPositionalParameter('%' . $this->connection->escapeLikeParameter($search) . '%')) - ); - } - $query->orderBy($query->func()->lower('p.configvalue'), 'DESC') - ->addOrderBy('uid_lower', 'ASC') - ->setFirstResult($offset) - ->setMaxResults($limit); - - $result = $query->executeQuery(); - /** @var list $uids */ - $uids = $result->fetchAll(\PDO::FETCH_COLUMN); - $result->closeCursor(); - - return $uids; - } - /** * Determines the users that have the given value set for a specific app-key-pair * diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index d93431a2699f4..639ce507f4d35 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -733,6 +733,49 @@ public function validateUserId(string $uid, bool $checkDataDirectory = false): v } } + /** + * Gets the list of user ids sorted by lastLogin, from most recent to least recent + * + * @param int|null $limit how many users to fetch + * @param int $offset from which offset to fetch + * @param string $search search users based on search params + * @return list list of user IDs + */ + public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array { + $connection = \OC::$server->getDatabaseConnection(); + $queryBuilder = $connection->getQueryBuilder(); + $queryBuilder->selectDistinct('uid') + ->from('users', 'u') + ->leftJoin('u', 'preferences', 'p', $queryBuilder->expr()->andX( + $queryBuilder->expr()->eq('p.userid', 'uid'), + $queryBuilder->expr()->eq('p.appid', $queryBuilder->expr()->literal('login')), + $queryBuilder->expr()->eq('p.configkey', $queryBuilder->expr()->literal('lastLogin'))) + ); + if($search !== '') { + $queryBuilder->leftJoin('u', 'preferences', 'p1', $queryBuilder->expr()->andX( + $queryBuilder->expr()->eq('p1.userid', 'uid'), + $queryBuilder->expr()->eq('p1.appid', $queryBuilder->expr()->literal('settings')), + $queryBuilder->expr()->eq('p1.configkey', $queryBuilder->expr()->literal('email'))) + ) + // sqlite doesn't like re-using a single named parameter here + ->where($queryBuilder->expr()->iLike('uid', $queryBuilder->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) + ->orWhere($queryBuilder->expr()->iLike('displayname', $queryBuilder->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) + ->orWhere($queryBuilder->expr()->iLike('p1.configvalue', $queryBuilder->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%')) + ); + } + $queryBuilder->orderBy($queryBuilder->func()->lower('p.configvalue'), 'DESC') + ->addOrderBy('uid_lower', 'ASC') + ->setFirstResult($offset) + ->setMaxResults($limit); + + $result = $queryBuilder->executeQuery(); + /** @var list $uids */ + $uids = $result->fetchAll(\PDO::FETCH_COLUMN); + $result->closeCursor(); + + return $uids; + } + private function verifyUid(string $uid, bool $checkDataDirectory = false): bool { $appdata = 'appdata_' . $this->config->getSystemValueString('instanceid'); diff --git a/lib/public/IConfig.php b/lib/public/IConfig.php index a2973dbe3baa0..b7feabd0ef573 100644 --- a/lib/public/IConfig.php +++ b/lib/public/IConfig.php @@ -249,15 +249,4 @@ public function deleteAppFromAllUsers($appName); * @since 8.0.0 */ public function getUsersForUserValue($appName, $key, $value); - - /** - * Gets the list of users sorted by lastLogin, from most recent to least recent - * - * @param int|null $limit how many records to fetch - * @param int $offset from which offset to fetch - * @param string $search search users based on search params - * @return list list of user IDs - * @since 30.0.0 - */ - public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array; } diff --git a/lib/public/IUserManager.php b/lib/public/IUserManager.php index 851b565f61746..091ccd8904889 100644 --- a/lib/public/IUserManager.php +++ b/lib/public/IUserManager.php @@ -210,4 +210,15 @@ public function getByEmail($email); * @since 26.0.0 */ public function validateUserId(string $uid, bool $checkDataDirectory = false): void; + + /** + * Gets the list of users sorted by lastLogin, from most recent to least recent + * + * @param int|null $limit how many records to fetch + * @param int $offset from which offset to fetch + * @param string $search search users based on search params + * @return list list of user IDs + * @since 30.0.0 + */ + public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array; }