diff options
author | Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com> | 2024-10-30 16:06:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-30 16:06:20 +0100 |
commit | 5787e5f27d21e36f18d000e8fe5328b8d5fed952 (patch) | |
tree | 501893d6ac82ec964a0b156c3deea8810cdbd4ab /lib/private | |
parent | 9fde83cbeb3caaffc0a1a40edb4b8b3b6b199522 (diff) | |
parent | 0cd1eeb509669c2fcd69e3f327766f5c05c57d16 (diff) | |
download | nextcloud-server-5787e5f27d21e36f18d000e8fe5328b8d5fed952.tar.gz nextcloud-server-5787e5f27d21e36f18d000e8fe5328b8d5fed952.zip |
Merge pull request #49001 from nextcloud/backport/48559/stable30
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php | 2 | ||||
-rw-r--r-- | lib/private/User/Manager.php | 65 |
2 files changed, 38 insertions, 29 deletions
diff --git a/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php b/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php index e5b66470de0..4caab8aec15 100644 --- a/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php +++ b/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php @@ -277,7 +277,7 @@ class ShardedQueryBuilder extends ExtendedQueryBuilder { public function addOrderBy($sort, $order = null) { $this->registerOrder((string)$sort, (string)$order ?? 'ASC'); - return parent::orderBy($sort, $order); + return parent::addOrderBy($sort, $order); } public function orderBy($sort, $order = null) { diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index dbfa2f8351c..2ca0359e4d4 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -7,6 +7,7 @@ */ namespace OC\User; +use Doctrine\DBAL\Platforms\OraclePlatform; use OC\Hooks\PublicEmitter; use OC\Memcache\WithLocalCache; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -742,44 +743,52 @@ class Manager extends PublicEmitter implements IUserManager { /** * 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|null $limit how many users to fetch (default: 25, max: 100) * @param int $offset from which offset to fetch * @param string $search search users based on search params * @return list<string> list of user IDs */ public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array { + // We can't load all users who already logged in + $limit = min(100, $limit ?: 25); + $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') + $queryBuilder->select('pref_login.userid') + ->from('preferences', 'pref_login') + ->where($queryBuilder->expr()->eq('pref_login.appid', $queryBuilder->expr()->literal('login'))) + ->andWhere($queryBuilder->expr()->eq('pref_login.configkey', $queryBuilder->expr()->literal('lastLogin'))) ->setFirstResult($offset) - ->setMaxResults($limit); + ->setMaxResults($limit) + ; - $result = $queryBuilder->executeQuery(); - /** @var list<string> $uids */ - $uids = $result->fetchAll(\PDO::FETCH_COLUMN); - $result->closeCursor(); + // Oracle don't want to run ORDER BY on CLOB column + $loginOrder = $connection->getDatabasePlatform() instanceof OraclePlatform + ? $queryBuilder->expr()->castColumn('pref_login.configvalue', IQueryBuilder::PARAM_INT) + : 'pref_login.configvalue'; + $queryBuilder + ->orderBy($loginOrder, 'DESC') + ->addOrderBy($queryBuilder->func()->lower('pref_login.userid'), 'ASC'); + + if ($search !== '') { + $displayNameMatches = $this->searchDisplayName($search); + $matchedUids = array_map(static fn (IUser $u): string => $u->getUID(), $displayNameMatches); + + $queryBuilder + ->leftJoin('pref_login', 'preferences', 'pref_email', $queryBuilder->expr()->andX( + $queryBuilder->expr()->eq('pref_login.userid', 'pref_email.userid'), + $queryBuilder->expr()->eq('pref_email.appid', $queryBuilder->expr()->literal('settings')), + $queryBuilder->expr()->eq('pref_email.configkey', $queryBuilder->expr()->literal('email')), + )) + ->andWhere($queryBuilder->expr()->orX( + $queryBuilder->expr()->in('pref_login.userid', $queryBuilder->createNamedParameter($matchedUids, IQueryBuilder::PARAM_STR_ARRAY)), + )); + } + + /** @var list<string> */ + $list = $queryBuilder->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); - return $uids; + return $list; } private function verifyUid(string $uid, bool $checkDataDirectory = false): bool { |