Skip to content

Commit a288ada

Browse files
fix: automatically disable sab
Signed-off-by: SebastianKrupinski <[email protected]>
1 parent 509a53c commit a288ada

File tree

2 files changed

+166
-6
lines changed

2 files changed

+166
-6
lines changed

apps/dav/lib/CardDAV/SyncService.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ public function getLocalSystemAddressBook() {
288288
* @return void
289289
*/
290290
public function syncInstance(?\Closure $progressCallback = null) {
291+
292+
if ($this->config->getAppValue('dav', 'system_addressbook_exposed', 'yes') === 'yes') {
293+
$limit = (int)$this->config->getAppValue('dav', 'system_addressbook_limit', '5000');
294+
if ($this->userManager->countUsersTotal($limit) >= $limit) {
295+
$this->config->setAppValue('dav', 'system_addressbook_exposed', 'no');
296+
return;
297+
}
298+
}
299+
291300
$systemAddressBook = $this->getLocalSystemAddressBook();
292301
$this->userManager->callForAllUsers(function ($user) use ($systemAddressBook, $progressCallback): void {
293302
$this->updateUser($user);

apps/dav/tests/unit/CardDAV/SyncServiceTest.php

Lines changed: 157 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use OCP\IDBConnection;
2121
use OCP\IUser;
2222
use OCP\IUserManager;
23+
use PHPUnit\Framework\MockObject\MockObject;
2324
use Psr\Http\Client\ClientExceptionInterface;
2425
use Psr\Log\LoggerInterface;
2526
use Psr\Log\NullLogger;
@@ -28,14 +29,15 @@
2829

2930
class SyncServiceTest extends TestCase {
3031

31-
protected CardDavBackend $backend;
32-
protected IUserManager $userManager;
33-
protected IDBConnection $dbConnection;
32+
protected CardDavBackend&MockObject $backend;
33+
protected IUserManager&MockObject $userManager;
34+
protected IDBConnection&MockObject $dbConnection;
3435
protected LoggerInterface $logger;
35-
protected Converter $converter;
36-
protected IClient $client;
37-
protected IConfig $config;
36+
protected Converter&MockObject $converter;
37+
protected IClient&MockObject $client;
38+
protected IConfig&MockObject $config;
3839
protected SyncService $service;
40+
3941
public function setUp(): void {
4042
$addressBook = [
4143
'id' => 1,
@@ -487,4 +489,153 @@ public function providerUseAbsoluteUriReport(): array {
487489
['https://server.internal:8080/nextcloud/', 'https://server.internal:8080/nextcloud/remote.php/dav/addressbooks/system/system/system'],
488490
];
489491
}
492+
493+
public function testSyncInstance(): void {
494+
495+
$this->config->method('getAppValue')
496+
->willReturnMap([
497+
['dav', 'system_addressbook_exposed', 'yes', 'yes'],
498+
['dav', 'system_addressbook_limit', '5000', '5000']
499+
]);
500+
501+
$this->userManager->method('countUsersTotal')
502+
->willReturn(4999);
503+
504+
$this->userManager->method('callForAllUsers')
505+
->willReturnCallback(function ($callback) {
506+
$user = $this->createMock(IUser::class);
507+
$user->method('getBackendClassName')->willReturn('unittest');
508+
$user->method('getUID')->willReturn('test-user');
509+
$user->method('isEnabled')->willReturn(true);
510+
$callback($user);
511+
});
512+
513+
$this->backend->method('getCards')
514+
->willReturn([
515+
['carddata' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:test-user\r\nFN:test-user\r\nEND:VCARD\r\n", 'uri' => 'unittest:test-user.vcf']
516+
]);
517+
518+
$clientService = $this->createMock(IClientService::class);
519+
520+
$service = $this->getMockBuilder(SyncService::class)
521+
->setConstructorArgs([
522+
$this->backend,
523+
$this->userManager,
524+
$this->dbConnection,
525+
$this->logger,
526+
$this->converter,
527+
$clientService,
528+
$this->config
529+
])
530+
->onlyMethods(['ensureSystemAddressBookExists', 'updateUser'])
531+
->getMock();
532+
533+
$service->expects($this->once())
534+
->method('ensureSystemAddressBookExists')
535+
->willReturn([
536+
'id' => 1,
537+
'uri' => 'system',
538+
'principaluri' => 'principals/system/system',
539+
'{DAV:}displayname' => 'system',
540+
]);
541+
$service->expects($this->any())
542+
->method('updateUser');
543+
544+
$service->syncInstance();
545+
}
546+
547+
public function testSyncInstanceWithProgressCallback(): void {
548+
$this->config->method('getAppValue')
549+
->willReturnMap([
550+
['dav', 'system_addressbook_exposed', 'yes', 'yes'],
551+
['dav', 'system_addressbook_limit', '5000', '5000']
552+
]);
553+
554+
$this->userManager->method('countUsersTotal')
555+
->willReturn(4999);
556+
557+
$this->userManager->method('callForAllUsers')
558+
->willReturnCallback(function ($callback) {
559+
$user = $this->createMock(IUser::class);
560+
$user->method('getBackendClassName')->willReturn('unittest');
561+
$user->method('getUID')->willReturn('test-user');
562+
$user->method('isEnabled')->willReturn(true);
563+
$callback($user);
564+
});
565+
566+
$this->backend->method('getCards')
567+
->willReturn([
568+
['carddata' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:test-user\r\nFN:test-user\r\nEND:VCARD\r\n", 'uri' => 'unittest:test-user.vcf']
569+
]);
570+
571+
$clientService = $this->createMock(IClientService::class);
572+
573+
$service = $this->getMockBuilder(SyncService::class)
574+
->setConstructorArgs([
575+
$this->backend,
576+
$this->userManager,
577+
$this->dbConnection,
578+
$this->logger,
579+
$this->converter,
580+
$clientService,
581+
$this->config
582+
])
583+
->onlyMethods(['ensureSystemAddressBookExists', 'updateUser'])
584+
->getMock();
585+
586+
$service->expects($this->once())
587+
->method('ensureSystemAddressBookExists')
588+
->willReturn([
589+
'id' => 1,
590+
'uri' => 'system',
591+
'principaluri' => 'principals/system/system',
592+
'{DAV:}displayname' => 'system',
593+
]);
594+
$service->expects($this->any())
595+
->method('updateUser');
596+
597+
$progressCalled = false;
598+
$progressCallback = function () use (&$progressCalled) {
599+
$progressCalled = true;
600+
};
601+
602+
$service->syncInstance($progressCallback);
603+
604+
$this->assertTrue($progressCalled);
605+
}
606+
607+
public function testSyncInstanceWithUserLimitExceeded(): void {
608+
$this->config->method('getAppValue')
609+
->willReturnMap([
610+
['dav', 'system_addressbook_exposed', 'yes', 'yes'],
611+
['dav', 'system_addressbook_limit', '5000', '5000']
612+
]);
613+
614+
$this->userManager->method('countUsersTotal')
615+
->willReturn(5000);
616+
617+
$this->config->expects($this->once())
618+
->method('setAppValue')
619+
->with('dav', 'system_addressbook_exposed', 'no');
620+
621+
$clientService = $this->createMock(IClientService::class);
622+
623+
$service = $this->getMockBuilder(SyncService::class)
624+
->setConstructorArgs([
625+
$this->backend,
626+
$this->userManager,
627+
$this->dbConnection,
628+
$this->logger,
629+
$this->converter,
630+
$clientService,
631+
$this->config
632+
])
633+
->onlyMethods(['getLocalSystemAddressBook'])
634+
->getMock();
635+
636+
$service->expects($this->never())
637+
->method('getLocalSystemAddressBook');
638+
639+
$service->syncInstance();
640+
}
490641
}

0 commit comments

Comments
 (0)