Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/User_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,10 @@ public function countUsers(int $limit = 0): int|false {
if ($backendUsers !== false) {
$users = (int)$users + $backendUsers;
if ($limit > 0) {
if ($users >= $limit) {
if ($backendUsers >= $limit) {
break;
}
$limit -= $users;
$limit -= $backendUsers;
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions apps/user_ldap/tests/User_ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\ILDAPWrapper;
use OCA\User_LDAP\User\DeletedUsersIndex;
use OCA\User_LDAP\User_LDAP;
use OCA\User_LDAP\User_Proxy;
use OCA\User_LDAP\UserPluginManager;
use OCP\Notification\IManager as INotificationManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
Expand Down Expand Up @@ -82,4 +84,51 @@ public function testCreateUser(): void {

$this->assertTrue($this->proxy->createUser('MyUid', 'MyPassword'));
}

/**
* @param int[] $userCounts
* @param int[] $expectedLimits
*/
#[DataProvider('dataCountUsersWithLimit')]
public function testCountUsersWithLimit(array $userCounts, int $limit, int $expected, array $expectedLimits): void {
$askedLimits = [];
$backends = [];
foreach ($userCounts as $i => $userCount) {
$backend = $this->createMock(User_LDAP::class);
$backend->method('countUsers')
->willReturnCallback(function (int $limit) use ($userCount, &$askedLimits): int {
$askedLimits[] = $limit;
return $limit > 0 ? min($userCount, $limit) : $userCount;
});
$backends['s0' . $i] = $backend;
}
$this->helper->method('getServerConfigurationPrefixes')
->willReturn(array_keys($backends));

$proxy = $this->getMockBuilder(User_Proxy::class)
->setConstructorArgs([
$this->helper,
$this->ldapWrapper,
$this->accessFactory,
$this->notificationManager,
$this->userPluginManager,
$this->logger,
$this->deletedUsersIndex,
])
->onlyMethods(['newInstance'])
->getMock();
$proxy->method('newInstance')
->willReturnCallback(fn (string $prefix) => $backends[$prefix]);

$this->assertSame($expected, $proxy->countUsers($limit));
$this->assertSame($expectedLimits, $askedLimits);
}

public static function dataCountUsersWithLimit(): array {
return [
'limit spread over all configurations' => [[30, 30, 50], 100, 100, [100, 70, 40]],
'stops once the limit is reached' => [[60, 50, 10], 100, 100, [100, 40]],
'no limit' => [[30, 30, 50], 0, 110, [0, 0, 0]],
];
}
}
4 changes: 2 additions & 2 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,10 @@ public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false):
if ($backendUsers !== false) {
$userCount = (int)$userCount + $backendUsers;
if ($limit > 0) {
if ($userCount >= $limit) {
if ($backendUsers >= $limit) {
break;
}
$limit -= $userCount;
$limit -= $backendUsers;
}
} else {
$this->logger->warning('Can not determine user count for ' . get_class($backend));
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/User/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\Server;
use OCP\User\Backend\ILimitAwareCountUsersBackend;
use OCP\User\Events\UserDeletedEvent;
use OCP\UserInterface;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -515,6 +517,41 @@ public function testCountUsersTwoBackends(): void {
$this->assertEquals(7 + 16, $users);
}

/**
* Registers limit-aware backends holding the given number of users and
* records the limit each one is asked for.
*
* @param int[] $userCounts
* @param int[] $askedLimits
*/
private function registerLimitAwareBackends(array $userCounts, array &$askedLimits): void {
foreach ($userCounts as $userCount) {
$backend = $this->createMockForIntersectionOfInterfaces([UserInterface::class, ILimitAwareCountUsersBackend::class]);
$backend->method('countUsers')
->willReturnCallback(function (int $limit) use ($userCount, &$askedLimits): int {
$askedLimits[] = $limit;
return $limit > 0 ? min($userCount, $limit) : $userCount;
});
$this->manager->registerBackend($backend);
}
}

public function testCountUsersTotalWithLimitAcrossBackends(): void {
$askedLimits = [];
$this->registerLimitAwareBackends([30, 30, 50], $askedLimits);

$this->assertSame(100, $this->manager->countUsersTotal(100));
$this->assertSame([100, 70, 40], $askedLimits);
}

public function testCountUsersTotalStopsAtLimit(): void {
$askedLimits = [];
$this->registerLimitAwareBackends([60, 50, 10], $askedLimits);

$this->assertSame(100, $this->manager->countUsersTotal(100));
$this->assertSame([100, 40], $askedLimits);
}

public function testCountUsersOnlyDisabled(): void {
$this->manager = Server::get(IUserManager::class);
// count other users in the db before adding our own
Expand Down