From 3287967b15e499b76d79554111291e2947f795fb Mon Sep 17 00:00:00 2001 From: Benjamin Gaussorgues Date: Thu, 3 Oct 2024 16:33:28 +0200 Subject: [PATCH] fix(users): use correct active user count Signed-off-by: Benjamin Gaussorgues --- apps/settings/lib/Controller/UsersController.php | 2 +- lib/private/User/Manager.php | 5 ++++- tests/lib/User/ManagerTest.php | 13 +++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/apps/settings/lib/Controller/UsersController.php b/apps/settings/lib/Controller/UsersController.php index d99319471fff9..5af16878e8eeb 100644 --- a/apps/settings/lib/Controller/UsersController.php +++ b/apps/settings/lib/Controller/UsersController.php @@ -168,7 +168,7 @@ public function usersList(): TemplateResponse { $recentUsersGroup = [ 'id' => '__nc_internal_recent', 'name' => $this->l10n->t('Recently active'), - 'usercount' => $userCount, + 'usercount' => $this->userManager->countSeenUsers(), ]; $disabledUsersGroup = [ diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index cc89ee61dd159..b48d5cb10f917 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -743,12 +743,15 @@ 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|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 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->select('login.userid') diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php index 39f1f1cef227e..53f57eee086a8 100644 --- a/tests/lib/User/ManagerTest.php +++ b/tests/lib/User/ManagerTest.php @@ -16,6 +16,7 @@ use OCP\ICacheFactory; use OCP\IConfig; use OCP\IUser; +use OCP\IUserManager; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -579,7 +580,7 @@ public function testCountUsersTwoBackends(): void { } public function testCountUsersOnlyDisabled(): void { - $manager = \OC::$server->getUserManager(); + $manager = \OCP\Server::get(IUserManager::class); // count other users in the db before adding our own $countBefore = $manager->countDisabledUsers(); @@ -604,7 +605,7 @@ public function testCountUsersOnlyDisabled(): void { } public function testCountUsersOnlySeen(): void { - $manager = \OC::$server->getUserManager(); + $manager = \OCP\Server::get(IUserManager::class); // count other users in the db before adding our own $countBefore = $manager->countSeenUsers(); @@ -630,7 +631,7 @@ public function testCountUsersOnlySeen(): void { } public function testCallForSeenUsers(): void { - $manager = \OC::$server->getUserManager(); + $manager = \OCP\Server::get(IUserManager::class); // count other users in the db before adding our own $count = 0; $function = function (IUser $user) use (&$count) { @@ -663,9 +664,13 @@ public function testCallForSeenUsers(): void { $user4->delete(); } + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ public function testRecentlyActive(): void { - $manager = \OCP\Server::get(IUserManager::class); $config = \OCP\Server::get(IConfig::class); + $manager = \OCP\Server::get(IUserManager::class); // Create some users $now = (string)time();