Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>pull/50605/head
parent
809d87c76b
commit
cab9a5deea
@ -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; |
||||
} |
||||
|
||||
} |
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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])); |
||||
} |
||||
} |
||||
} |
@ -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()); |
||||
} |
||||
} |
Loading…
Reference in new issue