-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: automatically disable sab #50605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\DAV\CardDAV\Notification; | ||
|
|
||
| use InvalidArgumentException; | ||
| use OCA\DAV\AppInfo\Application; | ||
| use OCP\IL10N; | ||
| use OCP\L10N\IFactory; | ||
| use OCP\Notification\INotification; | ||
| use OCP\Notification\INotifier; | ||
| use OCP\Notification\UnknownNotificationException; | ||
|
|
||
| class Notifier implements INotifier { | ||
|
|
||
| public function __construct( | ||
| protected IFactory $l10nFactory, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getID(): string { | ||
| return Application::APP_ID; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getName(): string { | ||
| return $this->l10nFactory->get(Application::APP_ID)->t('Contacts'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function prepare(INotification $notification, string $languageCode): INotification { | ||
| if ($notification->getApp() !== Application::APP_ID) { | ||
| throw new InvalidArgumentException(); | ||
| } | ||
|
|
||
| $l = $this->l10nFactory->get(Application::APP_ID, $languageCode); | ||
|
|
||
| return match ($notification->getSubject()) { | ||
| 'SystemAddressBookDisabled' => $this->parseSystemAddressBookDisabled($notification, $l), | ||
| default => throw new UnknownNotificationException() | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Generates a notification for the system address book being disabled. | ||
| */ | ||
| protected function parseSystemAddressBookDisabled(INotification $notification, IL10N $l): INotification { | ||
| $command = 'occ config:app:set dav system_addressbook_exposed --value="yes"'; | ||
| $notification->setParsedSubject( | ||
| $l->t('System address book disabled') | ||
| )->setParsedMessage( | ||
| $l->t('The system contacts address book has been automatically disabled during upgrade. This means that the address book will no longer be available to users in the contacts app or other clients. The system contacts address book was disabled because the amount of contacts in the address book exceeded the maximum recommended number of contacts. This limit is set to prevent performance issues. You can re-enable the system address book with the following command {command}', ['command' => $command]) | ||
| ); | ||
| return $notification; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\DAV\Migration; | ||
|
|
||
| use OCA\DAV\AppInfo\Application; | ||
| use OCP\AppFramework\Services\IAppConfig; | ||
| use OCP\IGroupManager; | ||
| use OCP\IUserManager; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\IRepairStep; | ||
| use OCP\Notification\IManager; | ||
| use OCP\ServerVersion; | ||
|
|
||
| class DisableSystemAddressBook implements IRepairStep { | ||
|
|
||
| public function __construct( | ||
| private readonly ServerVersion $serverVersion, | ||
| private readonly IAppConfig $appConfig, | ||
| private readonly IUserManager $userManager, | ||
| private readonly IGroupManager $groupManager, | ||
| private readonly IManager $notificationManager, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function getName() { | ||
| return 'Disable system address book'; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function run(IOutput $output) { | ||
| // If the system address book exposure was previously set skip the repair step | ||
| if ($this->appConfig->hasAppKey('system_addressbook_exposed') === true) { | ||
| $output->info('Skipping repair step system address book exposed was previously set'); | ||
| return; | ||
| } | ||
| // We use count seen because getting a user count from the backend can be very slow | ||
| $limit = $this->appConfig->getAppValueInt('system_addressbook_limit', 5000); | ||
| if ($this->userManager->countSeenUsers() <= $limit) { | ||
| $output->info("Skipping repair step system address book has less then the threshold $limit of contacts no need to disable"); | ||
| return; | ||
| } | ||
| $this->appConfig->setAppValueBool('system_addressbook_exposed', false); | ||
| $output->warning("System address book disabled because it has more then the threshold of $limit contacts this can be re-enabled later"); | ||
| // Notify all admin users about the system address book being disabled | ||
| foreach ($this->groupManager->get('admin')->getUsers() as $user) { | ||
| $notification = $this->notificationManager->createNotification(); | ||
| $notification->setApp(Application::APP_ID) | ||
| ->setUser($user->getUID()) | ||
| ->setDateTime(new \DateTime()) | ||
| ->setSubject('SystemSystemAddressBookDisabled', []); | ||
| $this->notificationManager->notify($notification); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\DAV\SetupChecks; | ||
|
|
||
| use OCA\DAV\AppInfo\Application; | ||
| use OCP\IAppConfig; | ||
| use OCP\IL10N; | ||
| use OCP\IUserManager; | ||
| use OCP\SetupCheck\ISetupCheck; | ||
| use OCP\SetupCheck\SetupResult; | ||
|
|
||
| class SystemAddressBookSize implements ISetupCheck { | ||
| public function __construct( | ||
| private IAppConfig $appConfig, | ||
| private IUserManager $userManager, | ||
| private IL10N $l10n, | ||
| ) { | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| return $this->l10n->t('DAV system address book size'); | ||
| } | ||
|
|
||
| public function getCategory(): string { | ||
| return 'dav'; | ||
| } | ||
|
|
||
| public function run(): SetupResult { | ||
| if (!$this->appConfig->getValueBool(Application::APP_ID, 'system_addressbook_exposed', true)) { | ||
| return SetupResult::success($this->l10n->t('The system address book is disabled')); | ||
| } | ||
|
|
||
| // We use count seen because getting a user count from the backend can be very slow | ||
| $count = $this->userManager->countSeenUsers(); | ||
| $limit = $this->appConfig->getValueInt(Application::APP_ID, 'system_addressbook_limit', 5000); | ||
|
|
||
| if ($count > $limit) { | ||
| return SetupResult::warning($this->l10n->t('The system address book is enabled, but contains more than the configured limit of %d contacts', [$limit])); | ||
| } else { | ||
| return SetupResult::success($this->l10n->t('The system address book is enabled and contains less than the configured limit of %d contacts', [$limit])); | ||
| } | ||
| } | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
apps/dav/tests/unit/SetupChecks/SystemAddressBookSizeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\DAV\Tests\SetupChecks; | ||
|
|
||
| use OCA\DAV\AppInfo\Application; | ||
| use OCA\DAV\SetupChecks\SystemAddressBookSize; | ||
| use OCP\IAppConfig; | ||
| use OCP\IL10N; | ||
| use OCP\IUserManager; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class SystemAddressBookSizeTest extends TestCase { | ||
| private $appConfig; | ||
| private $userManager; | ||
| private $l10n; | ||
|
|
||
| protected function setUp(): void { | ||
| $this->appConfig = $this->createMock(IAppConfig::class); | ||
| $this->userManager = $this->createMock(IUserManager::class); | ||
| $this->l10n = $this->createMock(IL10N::class); | ||
| $this->l10n->method('t')->willReturnCallback(fn ($text, $parameters = []) => vsprintf($text, $parameters)); | ||
| } | ||
|
|
||
| public function testSystemAddressBookDisabled() { | ||
| $this->appConfig->method('getValueBool') | ||
| ->with(Application::APP_ID, 'system_addressbook_exposed', true) | ||
| ->willReturn(false); | ||
|
|
||
| $check = new SystemAddressBookSize($this->appConfig, $this->userManager, $this->l10n); | ||
| $result = $check->run(); | ||
| $this->assertEquals('success', $result->getSeverity()); | ||
| $this->assertStringContainsString('disabled', $result->getDescription()); | ||
| } | ||
|
|
||
| public function testSystemAddressBookOverLimit() { | ||
| $this->appConfig->method('getValueBool') | ||
| ->willReturn(true); | ||
| $this->userManager->method('countSeenUsers') | ||
| ->willReturn(6000); | ||
| $this->appConfig->method('getValueInt') | ||
| ->willReturn(5000); | ||
|
|
||
| $check = new SystemAddressBookSize($this->appConfig, $this->userManager, $this->l10n); | ||
| $result = $check->run(); | ||
| $this->assertEquals('warning', $result->getSeverity()); | ||
| $this->assertStringContainsString('more than the configured limit', $result->getDescription()); | ||
| } | ||
|
|
||
| public function testSystemAddressBookUnderLimit() { | ||
| $this->appConfig->method('getValueBool') | ||
| ->willReturn(true); | ||
| $this->userManager->method('countSeenUsers') | ||
| ->willReturn(1000); | ||
| $this->appConfig->method('getValueInt') | ||
| ->willReturn(5000); | ||
|
|
||
| $check = new SystemAddressBookSize($this->appConfig, $this->userManager, $this->l10n); | ||
| $result = $check->run(); | ||
| $this->assertEquals('success', $result->getSeverity()); | ||
| $this->assertStringContainsString('less than the configured limit', $result->getDescription()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.