aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authoryemkareems <yemkareems@gmail.com>2024-07-09 13:15:31 +0530
committerGitHub <noreply@github.com>2024-07-09 13:15:31 +0530
commitf727040f508760a9f96ddeeeb1736b3940be4fb6 (patch)
treed61cda0f3e2b17e42c31e6ebf8c57bcf7146532a /lib
parent4fd2127761b2b3125aee5bfda5031d23fd77ac4b (diff)
parent68d60fcd2692cd36f10c3973986af6966493d5f4 (diff)
downloadnextcloud-server-f727040f508760a9f96ddeeeb1736b3940be4fb6.tar.gz
nextcloud-server-f727040f508760a9f96ddeeeb1736b3940be4fb6.zip
Merge pull request #45249 from nextcloud/feature/add-ability-to-sort-by-last-login
feat: add ability to sort by last login
Diffstat (limited to 'lib')
-rw-r--r--lib/private/User/Manager.php43
-rw-r--r--lib/public/IUserManager.php11
2 files changed, 54 insertions, 0 deletions
diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php
index d93431a2699..639ce507f4d 100644
--- a/lib/private/User/Manager.php
+++ b/lib/private/User/Manager.php
@@ -733,6 +733,49 @@ 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 $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 {
+ $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<string> $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/IUserManager.php b/lib/public/IUserManager.php
index 851b565f617..091ccd89048 100644
--- a/lib/public/IUserManager.php
+++ b/lib/public/IUserManager.php
@@ -210,4 +210,15 @@ interface IUserManager {
* @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<string> list of user IDs
+ * @since 30.0.0
+ */
+ public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array;
}