Skip to content

Commit 5d5e11b

Browse files
fix: automatically disable sab
Signed-off-by: SebastianKrupinski <[email protected]>
1 parent 2582a55 commit 5d5e11b

File tree

2 files changed

+269
-6
lines changed

2 files changed

+269
-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', 'none') === 'none') {
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: 260 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,256 @@ 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 testSyncInstanceWithUsersUnderLimit(): void {
494+
495+
$this->config->method('getAppValue')
496+
->willReturnMap([
497+
['dav', 'system_addressbook_exposed', 'none', 'none'],
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 testSyncInstanceWithWithUsersOverLimit(): void {
548+
$this->config->method('getAppValue')
549+
->willReturnMap([
550+
['dav', 'system_addressbook_exposed', 'none', 'none'],
551+
['dav', 'system_addressbook_limit', '5000', '5000']
552+
]);
553+
554+
$this->userManager->method('countUsersTotal')
555+
->willReturn(5000);
556+
557+
$this->config->expects($this->once())
558+
->method('setAppValue')
559+
->with('dav', 'system_addressbook_exposed', 'no');
560+
561+
$clientService = $this->createMock(IClientService::class);
562+
563+
$service = $this->getMockBuilder(SyncService::class)
564+
->setConstructorArgs([
565+
$this->backend,
566+
$this->userManager,
567+
$this->dbConnection,
568+
$this->logger,
569+
$this->converter,
570+
$clientService,
571+
$this->config
572+
])
573+
->onlyMethods(['getLocalSystemAddressBook'])
574+
->getMock();
575+
576+
$service->expects($this->never())
577+
->method('getLocalSystemAddressBook');
578+
579+
$service->syncInstance();
580+
}
581+
582+
public function testSyncInstanceWithProgressCallback(): void {
583+
$this->config->method('getAppValue')
584+
->willReturnMap([
585+
['dav', 'system_addressbook_exposed', 'none', 'none'],
586+
['dav', 'system_addressbook_limit', '5000', '5000']
587+
]);
588+
589+
$this->userManager->method('countUsersTotal')
590+
->willReturn(4999);
591+
592+
$this->userManager->method('callForAllUsers')
593+
->willReturnCallback(function ($callback) {
594+
$user = $this->createMock(IUser::class);
595+
$user->method('getBackendClassName')->willReturn('unittest');
596+
$user->method('getUID')->willReturn('test-user');
597+
$user->method('isEnabled')->willReturn(true);
598+
$callback($user);
599+
});
600+
601+
$this->backend->method('getCards')
602+
->willReturn([
603+
['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']
604+
]);
605+
606+
$clientService = $this->createMock(IClientService::class);
607+
608+
$service = $this->getMockBuilder(SyncService::class)
609+
->setConstructorArgs([
610+
$this->backend,
611+
$this->userManager,
612+
$this->dbConnection,
613+
$this->logger,
614+
$this->converter,
615+
$clientService,
616+
$this->config
617+
])
618+
->onlyMethods(['ensureSystemAddressBookExists', 'updateUser'])
619+
->getMock();
620+
621+
$service->expects($this->once())
622+
->method('ensureSystemAddressBookExists')
623+
->willReturn([
624+
'id' => 1,
625+
'uri' => 'system',
626+
'principaluri' => 'principals/system/system',
627+
'{DAV:}displayname' => 'system',
628+
]);
629+
$service->expects($this->any())
630+
->method('updateUser');
631+
632+
$progressCalled = false;
633+
$progressCallback = function () use (&$progressCalled) {
634+
$progressCalled = true;
635+
};
636+
637+
$service->syncInstance($progressCallback);
638+
639+
$this->assertTrue($progressCalled);
640+
}
641+
642+
public function testSyncInstanceWithExposeYes() {
643+
644+
$this->config->method('getAppValue')
645+
->willReturnMap([
646+
['dav', 'system_addressbook_exposed', 'none', 'yes']
647+
]);
648+
649+
$this->userManager->method('callForAllUsers')
650+
->willReturnCallback(function ($callback) {
651+
$user = $this->createMock(IUser::class);
652+
$user->method('getBackendClassName')->willReturn('unittest');
653+
$user->method('getUID')->willReturn('test-user');
654+
$user->method('isEnabled')->willReturn(true);
655+
$callback($user);
656+
});
657+
658+
$this->backend->method('getCards')
659+
->willReturn([
660+
['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']
661+
]);
662+
663+
$clientService = $this->createMock(IClientService::class);
664+
665+
$service = $this->getMockBuilder(SyncService::class)
666+
->setConstructorArgs([
667+
$this->backend,
668+
$this->userManager,
669+
$this->dbConnection,
670+
$this->logger,
671+
$this->converter,
672+
$clientService,
673+
$this->config
674+
])
675+
->onlyMethods(['ensureSystemAddressBookExists', 'updateUser'])
676+
->getMock();
677+
678+
$service->expects($this->once())
679+
->method('ensureSystemAddressBookExists')
680+
->willReturn([
681+
'id' => 1,
682+
'uri' => 'system',
683+
'principaluri' => 'principals/system/system',
684+
'{DAV:}displayname' => 'system',
685+
]);
686+
$service->expects($this->any())
687+
->method('updateUser');
688+
689+
$service->syncInstance();
690+
691+
}
692+
693+
public function testSyncInstanceWithExposeNo() {
694+
695+
$this->config->method('getAppValue')
696+
->willReturnMap([
697+
['dav', 'system_addressbook_exposed', 'none', 'no']
698+
]);
699+
700+
$this->userManager->method('callForAllUsers')
701+
->willReturnCallback(function ($callback) {
702+
$user = $this->createMock(IUser::class);
703+
$user->method('getBackendClassName')->willReturn('unittest');
704+
$user->method('getUID')->willReturn('test-user');
705+
$user->method('isEnabled')->willReturn(true);
706+
$callback($user);
707+
});
708+
709+
$this->backend->method('getCards')
710+
->willReturn([
711+
['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']
712+
]);
713+
714+
$clientService = $this->createMock(IClientService::class);
715+
716+
$service = $this->getMockBuilder(SyncService::class)
717+
->setConstructorArgs([
718+
$this->backend,
719+
$this->userManager,
720+
$this->dbConnection,
721+
$this->logger,
722+
$this->converter,
723+
$clientService,
724+
$this->config
725+
])
726+
->onlyMethods(['ensureSystemAddressBookExists', 'updateUser'])
727+
->getMock();
728+
729+
$service->expects($this->once())
730+
->method('ensureSystemAddressBookExists')
731+
->willReturn([
732+
'id' => 1,
733+
'uri' => 'system',
734+
'principaluri' => 'principals/system/system',
735+
'{DAV:}displayname' => 'system',
736+
]);
737+
$service->expects($this->any())
738+
->method('updateUser');
739+
740+
$service->syncInstance();
741+
742+
}
743+
490744
}

0 commit comments

Comments
 (0)