Merge pull request #50171 from nextcloud/enh/limit-ldap-user-count

Limit ldap user count
pull/50216/head
Côme Chilliet 2025-01-16 17:25:01 +07:00 committed by GitHub
commit 626bc7220b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 106 additions and 204 deletions

@ -33,7 +33,7 @@ class Version1027Date20230504122946 extends SimpleMigrationStep {
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
if ($this->userManager->countSeenUsers() > 100 || array_sum($this->userManager->countUsers()) > 100) {
if ($this->userManager->countSeenUsers() > 100 || $this->userManager->countUsersTotal(100) >= 100) {
$this->config->setAppValue('dav', 'needs_system_address_book_sync', 'yes');
$output->info('Could not sync system address books during update - too many user records have been found. Please call occ dav:sync-system-addressbook manually.');
return;

@ -8,7 +8,6 @@ declare(strict_types=1);
*/
namespace OCA\UpdateNotification\Settings;
use OC\User\Backend;
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
@ -20,7 +19,6 @@ use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Settings\ISettings;
use OCP\Support\Subscription\IRegistry;
use OCP\User\Backend\ICountUsersBackend;
use OCP\Util;
use Psr\Log\LoggerInterface;
@ -141,26 +139,6 @@ class Admin implements ISettings {
}
private function isWebUpdaterRecommended(): bool {
return $this->getUserCount() < 100;
}
/**
* @see https://github.com/nextcloud/server/blob/39494fbf794d982f6f6551c984e6ca4c4e947d01/lib/private/Support/Subscription/Registry.php#L188-L216 implementation reference
*/
private function getUserCount(): int {
$userCount = 0;
$backends = $this->userManager->getBackends();
foreach ($backends as $backend) {
// TODO: change below to 'if ($backend instanceof ICountUsersBackend) {'
if ($backend->implementsActions(Backend::COUNT_USERS)) {
/** @var ICountUsersBackend $backend */
$backendUsers = $backend->countUsers();
if ($backendUsers !== false) {
$userCount += $backendUsers;
}
}
}
return $userCount;
return (int)$this->userManager->countUsersTotal(100) < 100;
}
}

@ -8,7 +8,6 @@ declare(strict_types=1);
*/
namespace OCA\UpdateNotification\Tests\Settings;
use OC\User\Backend;
use OCA\UpdateNotification\Settings\Admin;
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Http\TemplateResponse;
@ -22,8 +21,6 @@ use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\L10N\ILanguageIterator;
use OCP\Support\Subscription\IRegistry;
use OCP\User\Backend\ICountUsersBackend;
use OCP\UserInterface;
use OCP\Util;
use Psr\Log\LoggerInterface;
use Test\TestCase;
@ -81,42 +78,10 @@ class AdminTest extends TestCase {
}
public function testGetFormWithUpdate(): void {
$backend1 = $this->createMock(CountUsersBackend::class);
$backend2 = $this->createMock(CountUsersBackend::class);
$backend3 = $this->createMock(CountUsersBackend::class);
$backend1
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(false);
$backend2
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(true);
$backend3
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(true);
$backend1
->expects($this->never())
->method('countUsers');
$backend2
->expects($this->once())
->method('countUsers')
->with()
->willReturn(false);
$backend3
->expects($this->once())
->method('countUsers')
->with()
->willReturn(5);
$this->userManager
->expects($this->once())
->method('getBackends')
->with()
->willReturn([$backend1, $backend2, $backend3]);
->method('countUsersTotal')
->willReturn(5);
$channels = [
'daily',
'beta',
@ -207,42 +172,10 @@ class AdminTest extends TestCase {
}
public function testGetFormWithUpdateAndChangedUpdateServer(): void {
$backend1 = $this->createMock(CountUsersBackend::class);
$backend2 = $this->createMock(CountUsersBackend::class);
$backend3 = $this->createMock(CountUsersBackend::class);
$backend1
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(false);
$backend2
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(true);
$backend3
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(true);
$backend1
->expects($this->never())
->method('countUsers');
$backend2
->expects($this->once())
->method('countUsers')
->with()
->willReturn(false);
$backend3
->expects($this->once())
->method('countUsers')
->with()
->willReturn(5);
$this->userManager
->expects($this->once())
->method('getBackends')
->with()
->willReturn([$backend1, $backend2, $backend3]);
->method('countUsersTotal')
->willReturn(5);
$channels = [
'daily',
'beta',
@ -334,42 +267,10 @@ class AdminTest extends TestCase {
}
public function testGetFormWithUpdateAndCustomersUpdateServer(): void {
$backend1 = $this->createMock(CountUsersBackend::class);
$backend2 = $this->createMock(CountUsersBackend::class);
$backend3 = $this->createMock(CountUsersBackend::class);
$backend1
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(false);
$backend2
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(true);
$backend3
->expects($this->once())
->method('implementsActions')
->with(Backend::COUNT_USERS)
->willReturn(true);
$backend1
->expects($this->never())
->method('countUsers');
$backend2
->expects($this->once())
->method('countUsers')
->with()
->willReturn(false);
$backend3
->expects($this->once())
->method('countUsers')
->with()
->willReturn(5);
$this->userManager
->expects($this->once())
->method('getBackends')
->with()
->willReturn([$backend1, $backend2, $backend3]);
->method('countUsersTotal')
->willReturn(5);
$channels = [
'daily',
'beta',
@ -543,7 +444,3 @@ class AdminTest extends TestCase {
$this->assertSame($expectation, $result);
}
}
abstract class CountUsersBackend implements UserInterface, ICountUsersBackend {
}

@ -17,12 +17,12 @@ use OCA\User_LDAP\User\User;
use OCP\IUserBackend;
use OCP\Notification\IManager as INotificationManager;
use OCP\User\Backend\ICountMappedUsersBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Backend\ILimitAwareCountUsersBackend;
use OCP\User\Backend\IProvideEnabledStateBackend;
use OCP\UserInterface;
use Psr\Log\LoggerInterface;
class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, IUserLDAP, ILimitAwareCountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
public function __construct(
Access $access,
protected INotificationManager $notificationManager,
@ -528,20 +528,18 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
/**
* counts the users in LDAP
*
* @return int|false
*/
public function countUsers() {
public function countUsers(int $limit = 0): int|false {
if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) {
return $this->userPluginManager->countUsers();
}
$filter = $this->access->getFilterForUserCount();
$cacheKey = 'countUsers-' . $filter;
$cacheKey = 'countUsers-' . $filter . '-' . $limit;
if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) {
return $entries;
}
$entries = $this->access->countUsers($filter);
$entries = $this->access->countUsers($filter, limit:$limit);
$this->access->connection->writeToCache($cacheKey, $entries);
return $entries;
}

@ -13,12 +13,12 @@ use OCA\User_LDAP\User\User;
use OCP\IUserBackend;
use OCP\Notification\IManager as INotificationManager;
use OCP\User\Backend\ICountMappedUsersBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Backend\ILimitAwareCountUsersBackend;
use OCP\User\Backend\IProvideEnabledStateBackend;
use OCP\UserInterface;
use Psr\Log\LoggerInterface;
class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ILimitAwareCountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
/** @var User_LDAP[] */
private array $backends = [];
private ?User_LDAP $refBackend = null;
@ -350,17 +350,21 @@ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP
/**
* Count the number of users
*
* @return int|false
*/
public function countUsers() {
public function countUsers(int $limit = 0): int|false {
$this->setup();
$users = false;
foreach ($this->backends as $backend) {
$backendUsers = $backend->countUsers();
$backendUsers = $backend->countUsers($limit);
if ($backendUsers !== false) {
$users = (int)$users + $backendUsers;
if ($limit > 0) {
if ($users >= $limit) {
break;
}
$limit -= $users;
}
}
}
return $users;

@ -279,8 +279,7 @@ class OC {
}
if (!$tooBig) {
// count users
$stats = Server::get(\OCP\IUserManager::class)->countUsers();
$totalUsers = array_sum($stats);
$totalUsers = Server::get(\OCP\IUserManager::class)->countUsersTotal(51);
$tooBig = ($totalUsers > 50);
}
}

@ -892,6 +892,7 @@ return array(
'OCP\\User\\Backend\\IGetDisplayNameBackend' => $baseDir . '/lib/public/User/Backend/IGetDisplayNameBackend.php',
'OCP\\User\\Backend\\IGetHomeBackend' => $baseDir . '/lib/public/User/Backend/IGetHomeBackend.php',
'OCP\\User\\Backend\\IGetRealUIDBackend' => $baseDir . '/lib/public/User/Backend/IGetRealUIDBackend.php',
'OCP\\User\\Backend\\ILimitAwareCountUsersBackend' => $baseDir . '/lib/public/User/Backend/ILimitAwareCountUsersBackend.php',
'OCP\\User\\Backend\\IPasswordConfirmationBackend' => $baseDir . '/lib/public/User/Backend/IPasswordConfirmationBackend.php',
'OCP\\User\\Backend\\IPasswordHashBackend' => $baseDir . '/lib/public/User/Backend/IPasswordHashBackend.php',
'OCP\\User\\Backend\\IProvideAvatarBackend' => $baseDir . '/lib/public/User/Backend/IProvideAvatarBackend.php',

@ -933,6 +933,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\User\\Backend\\IGetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IGetDisplayNameBackend.php',
'OCP\\User\\Backend\\IGetHomeBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IGetHomeBackend.php',
'OCP\\User\\Backend\\IGetRealUIDBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IGetRealUIDBackend.php',
'OCP\\User\\Backend\\ILimitAwareCountUsersBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ILimitAwareCountUsersBackend.php',
'OCP\\User\\Backend\\IPasswordConfirmationBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IPasswordConfirmationBackend.php',
'OCP\\User\\Backend\\IPasswordHashBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IPasswordHashBackend.php',
'OCP\\User\\Backend\\IProvideAvatarBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IProvideAvatarBackend.php',

@ -8,7 +8,6 @@ declare(strict_types=1);
*/
namespace OC\Support\Subscription;
use OC\User\Backend;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\IGroupManager;
@ -19,8 +18,6 @@ use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
use OCP\Support\Subscription\IRegistry;
use OCP\Support\Subscription\ISubscription;
use OCP\Support\Subscription\ISupportedApps;
use OCP\User\Backend\ICountMappedUsersBackend;
use OCP\User\Backend\ICountUsersBackend;
use Psr\Log\LoggerInterface;
class Registry implements IRegistry {
@ -167,22 +164,8 @@ class Registry implements IRegistry {
}
private function getUserCount(): int {
$userCount = 0;
$backends = $this->userManager->getBackends();
foreach ($backends as $backend) {
if ($backend instanceof ICountMappedUsersBackend) {
$userCount += $backend->countMappedUsers();
} elseif ($backend->implementsActions(Backend::COUNT_USERS)) {
/** @var ICountUsersBackend $backend */
$backendUsers = $backend->countUsers();
if ($backendUsers !== false) {
$userCount += $backendUsers;
} else {
// TODO what if the user count can't be determined?
$this->logger->warning('Can not determine user count for ' . get_class($backend), ['app' => 'lib']);
}
}
}
/* We cannot limit because we substract disabled users afterward. But we limit to mapped users so should be not too expensive. */
$userCount = (int)$this->userManager->countUsersTotal(0, true);
$disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
$disabledUsersCount = count($disabledUsers);

@ -17,11 +17,11 @@ use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\User\Backend\ABackend;
use OCP\User\Backend\ICheckPasswordBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Backend\ICreateUserBackend;
use OCP\User\Backend\IGetDisplayNameBackend;
use OCP\User\Backend\IGetHomeBackend;
use OCP\User\Backend\IGetRealUIDBackend;
use OCP\User\Backend\ILimitAwareCountUsersBackend;
use OCP\User\Backend\IPasswordHashBackend;
use OCP\User\Backend\ISearchKnownUsersBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
@ -37,7 +37,7 @@ class Database extends ABackend implements
IGetDisplayNameBackend,
ICheckPasswordBackend,
IGetHomeBackend,
ICountUsersBackend,
ILimitAwareCountUsersBackend,
ISearchKnownUsersBackend,
IGetRealUIDBackend,
IPasswordHashBackend {
@ -463,10 +463,8 @@ class Database extends ABackend implements
/**
* counts the users in the database
*
* @return int|false
*/
public function countUsers() {
public function countUsers(int $limit = 0): int|false {
$this->fixDI();
$query = $this->dbConn->getQueryBuilder();

@ -24,8 +24,10 @@ use OCP\L10N\IFactory;
use OCP\Server;
use OCP\Support\Subscription\IAssertion;
use OCP\User\Backend\ICheckPasswordBackend;
use OCP\User\Backend\ICountMappedUsersBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Backend\IGetRealUIDBackend;
use OCP\User\Backend\ILimitAwareCountUsersBackend;
use OCP\User\Backend\IProvideEnabledStateBackend;
use OCP\User\Backend\ISearchKnownUsersBackend;
use OCP\User\Events\BeforeUserCreatedEvent;
@ -488,6 +490,36 @@ class Manager extends PublicEmitter implements IUserManager {
return $userCountStatistics;
}
public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false): int|false {
$userCount = false;
foreach ($this->backends as $backend) {
if ($onlyMappedUsers && $backend instanceof ICountMappedUsersBackend) {
$backendUsers = $backend->countMappedUsers();
} elseif ($backend instanceof ILimitAwareCountUsersBackend) {
$backendUsers = $backend->countUsers($limit);
} elseif ($backend instanceof ICountUsersBackend || $backend->implementsActions(Backend::COUNT_USERS)) {
/** @var ICountUsersBackend $backend */
$backendUsers = $backend->countUsers();
} else {
$this->logger->debug('Skip backend for user count: ' . get_class($backend));
continue;
}
if ($backendUsers !== false) {
$userCount = (int)$userCount + $backendUsers;
if ($limit > 0) {
if ($userCount >= $limit) {
break;
}
$limit -= $userCount;
}
} else {
$this->logger->warning('Can not determine user count for ' . get_class($backend));
}
}
return $userCount;
}
/**
* returns how many users per backend exist in the requested groups (if supported by backend)
*

@ -163,6 +163,16 @@ interface IUserManager {
*/
public function countUsers();
/**
* Get how many users exists in total, whithin limit
*
* @param int $limit Limit the count to avoid resource waste. 0 to disable
* @param bool $onlyMappedUsers Count mapped users instead of all users for compatible backends
*
* @since 31.0.0
*/
public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false): int|false;
/**
* @param \Closure $callback
* @psalm-param \Closure(\OCP\IUser):void $callback

@ -10,6 +10,7 @@ namespace OCP\User\Backend;
/**
* @since 14.0.0
* @deprecated 31.0.0 use and implement ILimitAwareCountUsersBackend instead.
*/
interface ICountUsersBackend {
/**

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\User\Backend;
/**
* @since 31.0.0
*/
interface ILimitAwareCountUsersBackend extends ICountUsersBackend {
/**
* @since 31.0.0
*
* @param int $limit Limit to stop counting users if there are more than $limit. 0 to disable limiting.
* @return int|false The number of users (may be limited to $limit) on success false on failure
*/
public function countUsers(int $limit = 0): int|false;
}

@ -8,7 +8,6 @@
namespace Test\Support\Subscription;
use OC\Support\Subscription\Registry;
use OC\User\Database;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
@ -17,33 +16,19 @@ use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Support\Subscription\ISubscription;
use OCP\Support\Subscription\ISupportedApps;
use OCP\User\Backend\ICountUsersBackend;
use OCP\UserInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class RegistryTest extends TestCase {
/** @var Registry */
private $registry;
private Registry $registry;
/** @var MockObject|IConfig */
private $config;
/** @var MockObject|IServerContainer */
private $serverContainer;
/** @var MockObject|IUserManager */
private $userManager;
/** @var MockObject|IGroupManager */
private $groupManager;
/** @var MockObject|LoggerInterface */
private $logger;
/** @var MockObject|IManager */
private $notificationManager;
private MockObject&IConfig $config;
private MockObject&IServerContainer $serverContainer;
private MockObject&IUserManager $userManager;
private MockObject&IGroupManager $groupManager;
private MockObject&LoggerInterface $logger;
private MockObject&IManager $notificationManager;
protected function setUp(): void {
parent::setUp();
@ -198,17 +183,9 @@ class RegistryTest extends TestCase {
->method('getUsersForUserValue')
->with('core', 'enabled', 'false')
->willReturn(array_fill(0, $disabledUsers, ''));
/* @var UserInterface|ICountUsersBackend|\PHPUnit\Framework\MockObject\MockObject $dummyBackend */
$dummyBackend = $this->createMock(Database::class);
$dummyBackend->expects($this->once())
->method('implementsActions')
->willReturn(true);
$dummyBackend->expects($this->once())
->method('countUsers')
->willReturn($userCount);
$this->userManager->expects($this->once())
->method('getBackends')
->willReturn([$dummyBackend]);
->method('countUsersTotal')
->willReturn($userCount);
if ($expectedResult) {
$dummyGroup = $this->createMock(IGroup::class);