From e976a1323fc3aa23d82a8e055e16387bd6288f0d Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Mon, 29 Sep 2025 12:34:44 +0200 Subject: [PATCH] chore: Register `system_addressbook_exposed` in lexicon Signed-off-by: Louis Chmn --- .../composer/composer/autoload_classmap.php | 1 + .../dav/composer/composer/autoload_static.php | 1 + apps/dav/lib/AppInfo/Application.php | 3 ++ apps/dav/lib/CardDAV/ContactsManager.php | 4 +- apps/dav/lib/CardDAV/UserAddressBooks.php | 25 +++++------ apps/dav/lib/ConfigLexicon.php | 45 +++++++++++++++++++ .../lib/Listener/DavAdminSettingsListener.php | 9 ++-- .../Migration/DisableSystemAddressBook.php | 5 ++- .../lib/SetupChecks/SystemAddressBookSize.php | 3 +- .../SetupChecks/SystemAddressBookSizeTest.php | 3 +- build/psalm-baseline.xml | 4 -- 11 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 apps/dav/lib/ConfigLexicon.php diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index 08c5b395b2e..4eb7e248073 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -206,6 +206,7 @@ return array( 'OCA\\DAV\\Comments\\EntityCollection' => $baseDir . '/../lib/Comments/EntityCollection.php', 'OCA\\DAV\\Comments\\EntityTypeCollection' => $baseDir . '/../lib/Comments/EntityTypeCollection.php', 'OCA\\DAV\\Comments\\RootCollection' => $baseDir . '/../lib/Comments/RootCollection.php', + 'OCA\\DAV\\ConfigLexicon' => $baseDir . '/../lib/ConfigLexicon.php', 'OCA\\DAV\\Connector\\LegacyDAVACL' => $baseDir . '/../lib/Connector/LegacyDAVACL.php', 'OCA\\DAV\\Connector\\LegacyPublicAuth' => $baseDir . '/../lib/Connector/LegacyPublicAuth.php', 'OCA\\DAV\\Connector\\Sabre\\AnonymousOptionsPlugin' => $baseDir . '/../lib/Connector/Sabre/AnonymousOptionsPlugin.php', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index 4c7d43cddbf..fabf7661091 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -221,6 +221,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\Comments\\EntityCollection' => __DIR__ . '/..' . '/../lib/Comments/EntityCollection.php', 'OCA\\DAV\\Comments\\EntityTypeCollection' => __DIR__ . '/..' . '/../lib/Comments/EntityTypeCollection.php', 'OCA\\DAV\\Comments\\RootCollection' => __DIR__ . '/..' . '/../lib/Comments/RootCollection.php', + 'OCA\\DAV\\ConfigLexicon' => __DIR__ . '/..' . '/../lib/ConfigLexicon.php', 'OCA\\DAV\\Connector\\LegacyDAVACL' => __DIR__ . '/..' . '/../lib/Connector/LegacyDAVACL.php', 'OCA\\DAV\\Connector\\LegacyPublicAuth' => __DIR__ . '/..' . '/../lib/Connector/LegacyPublicAuth.php', 'OCA\\DAV\\Connector\\Sabre\\AnonymousOptionsPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/AnonymousOptionsPlugin.php', diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index dfc4216713d..a22b2cea72e 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -23,6 +23,7 @@ use OCA\DAV\Capabilities; use OCA\DAV\CardDAV\ContactsManager; use OCA\DAV\CardDAV\Notification\Notifier as NotifierCardDAV; use OCA\DAV\CardDAV\SyncService; +use OCA\DAV\ConfigLexicon; use OCA\DAV\Events\AddressBookCreatedEvent; use OCA\DAV\Events\AddressBookDeletedEvent; use OCA\DAV\Events\AddressBookShareUpdatedEvent; @@ -228,6 +229,8 @@ class Application extends App implements IBootstrap { $context->registerDeclarativeSettings(SystemAddressBookSettings::class); $context->registerEventListener(DeclarativeSettingsGetValueEvent::class, DavAdminSettingsListener::class); $context->registerEventListener(DeclarativeSettingsSetValueEvent::class, DavAdminSettingsListener::class); + + $context->registerConfigLexicon(ConfigLexicon::class); } public function boot(IBootContext $context): void { diff --git a/apps/dav/lib/CardDAV/ContactsManager.php b/apps/dav/lib/CardDAV/ContactsManager.php index 85f96bac146..89f1dbb39bd 100644 --- a/apps/dav/lib/CardDAV/ContactsManager.php +++ b/apps/dav/lib/CardDAV/ContactsManager.php @@ -7,6 +7,8 @@ */ namespace OCA\DAV\CardDAV; +use OCA\DAV\AppInfo\Application; +use OCA\DAV\ConfigLexicon; use OCA\DAV\Db\PropertyMapper; use OCP\Contacts\IManager; use OCP\IAppConfig; @@ -45,7 +47,7 @@ class ContactsManager { * @param IURLGenerator $urlGenerator */ public function setupSystemContactsProvider(IManager $cm, ?string $userId, IURLGenerator $urlGenerator) { - $systemAddressBookExposed = $this->appConfig->getValueBool('dav', 'system_addressbook_exposed', true); + $systemAddressBookExposed = $this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED); if (!$systemAddressBookExposed) { return; } diff --git a/apps/dav/lib/CardDAV/UserAddressBooks.php b/apps/dav/lib/CardDAV/UserAddressBooks.php index e29e52e77df..2175939e709 100644 --- a/apps/dav/lib/CardDAV/UserAddressBooks.php +++ b/apps/dav/lib/CardDAV/UserAddressBooks.php @@ -9,11 +9,14 @@ declare(strict_types=1); */ namespace OCA\DAV\CardDAV; +use OCA\DAV\AppInfo\Application; use OCA\DAV\AppInfo\PluginManager; use OCA\DAV\CardDAV\Integration\ExternalAddressBook; use OCA\DAV\CardDAV\Integration\IAddressBookProvider; +use OCA\DAV\ConfigLexicon; use OCA\Federation\TrustedServers; use OCP\AppFramework\QueryException; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; @@ -21,6 +24,7 @@ use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; use OCP\Server; +use OCP\Util; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Sabre\CardDAV\Backend; @@ -30,11 +34,9 @@ use Sabre\DAV\MkCol; use function array_map; class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome { - /** @var IL10N */ - protected $l10n; - - /** @var IConfig */ - protected $config; + protected IL10N $l10n; + protected IConfig $config; + protected IAppConfig $appConfig; public function __construct( Backend\BackendInterface $carddavBackend, @@ -44,6 +46,10 @@ class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome { private ?IGroupManager $groupManager, ) { parent::__construct($carddavBackend, $principalUri); + + $this->l10n = Util::getL10N('dav'); + $this->config = Server::get(IConfig::class); + $this->appConfig = Server::get(IAppConfig::class); } /** @@ -52,19 +58,12 @@ class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome { * @return IAddressBook[] */ public function getChildren() { - if ($this->l10n === null) { - $this->l10n = \OC::$server->getL10N('dav'); - } - if ($this->config === null) { - $this->config = Server::get(IConfig::class); - } - /** @var string|array $principal */ $principal = $this->principalUri; $addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri); // add the system address book $systemAddressBook = null; - $systemAddressBookExposed = $this->config->getAppValue('dav', 'system_addressbook_exposed', 'yes') === 'yes'; + $systemAddressBookExposed = $this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED); if ($systemAddressBookExposed && is_string($principal) && $principal !== 'principals/system/system' && $this->carddavBackend instanceof CardDavBackend) { $systemAddressBook = $this->carddavBackend->getAddressBooksByUri('principals/system/system', 'system'); if ($systemAddressBook !== null) { diff --git a/apps/dav/lib/ConfigLexicon.php b/apps/dav/lib/ConfigLexicon.php new file mode 100644 index 00000000000..921c6f2036b --- /dev/null +++ b/apps/dav/lib/ConfigLexicon.php @@ -0,0 +1,45 @@ +getFieldId() === 'system_addressbook_enabled') { - $event->setValue((int)$this->config->getValueBool('dav', 'system_addressbook_exposed', true)); + $event->setValue((int)$this->config->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED)); } - } private function handleSetValue(DeclarativeSettingsSetValueEvent $event): void { - if ($event->getFieldId() === 'system_addressbook_enabled') { - $this->config->setValueBool('dav', 'system_addressbook_exposed', (bool)$event->getValue()); + $this->config->setValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED, (bool)$event->getValue()); $event->stopPropagation(); } - } } diff --git a/apps/dav/lib/Migration/DisableSystemAddressBook.php b/apps/dav/lib/Migration/DisableSystemAddressBook.php index f2530c4a1b1..8c7f712275c 100644 --- a/apps/dav/lib/Migration/DisableSystemAddressBook.php +++ b/apps/dav/lib/Migration/DisableSystemAddressBook.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace OCA\DAV\Migration; use OCA\DAV\AppInfo\Application; +use OCA\DAV\ConfigLexicon; use OCP\AppFramework\Services\IAppConfig; use OCP\IGroupManager; use OCP\IUserManager; @@ -40,7 +41,7 @@ class DisableSystemAddressBook implements IRepairStep { */ 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) { + if ($this->appConfig->hasAppKey(ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED) === true) { $output->info('Skipping repair step system address book exposed was previously set'); return; } @@ -50,7 +51,7 @@ class DisableSystemAddressBook implements IRepairStep { $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); + $this->appConfig->setAppValueBool(ConfigLexicon::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) { diff --git a/apps/dav/lib/SetupChecks/SystemAddressBookSize.php b/apps/dav/lib/SetupChecks/SystemAddressBookSize.php index ad7d8b9c2cc..746ce609e47 100644 --- a/apps/dav/lib/SetupChecks/SystemAddressBookSize.php +++ b/apps/dav/lib/SetupChecks/SystemAddressBookSize.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace OCA\DAV\SetupChecks; use OCA\DAV\AppInfo\Application; +use OCA\DAV\ConfigLexicon; use OCP\IAppConfig; use OCP\IL10N; use OCP\IUserManager; @@ -33,7 +34,7 @@ class SystemAddressBookSize implements ISetupCheck { } public function run(): SetupResult { - if (!$this->appConfig->getValueBool(Application::APP_ID, 'system_addressbook_exposed', true)) { + if (!$this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED)) { return SetupResult::success($this->l10n->t('The system address book is disabled')); } diff --git a/apps/dav/tests/unit/SetupChecks/SystemAddressBookSizeTest.php b/apps/dav/tests/unit/SetupChecks/SystemAddressBookSizeTest.php index 175d91a0c7d..5d755457d18 100644 --- a/apps/dav/tests/unit/SetupChecks/SystemAddressBookSizeTest.php +++ b/apps/dav/tests/unit/SetupChecks/SystemAddressBookSizeTest.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace OCA\DAV\Tests\SetupChecks; use OCA\DAV\AppInfo\Application; +use OCA\DAV\ConfigLexicon; use OCA\DAV\SetupChecks\SystemAddressBookSize; use OCP\IAppConfig; use OCP\IL10N; @@ -30,7 +31,7 @@ class SystemAddressBookSizeTest extends TestCase { public function testSystemAddressBookDisabled() { $this->appConfig->method('getValueBool') - ->with(Application::APP_ID, 'system_addressbook_exposed', true) + ->with(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED) ->willReturn(false); $check = new SystemAddressBookSize($this->appConfig, $this->userManager, $this->l10n); diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 7d0d0c4eabe..472eac7295d 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -512,10 +512,6 @@ - - - - principalUri]]> principalUri]]>