aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/User/Manager.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/User/Manager.php')
-rw-r--r--lib/private/User/Manager.php65
1 files changed, 37 insertions, 28 deletions
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 {