From be84419192cb6f719bfc99cc1cb2998ce0594260 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 13 Dec 2024 13:52:45 -0100 Subject: [PATCH] feat(lexicon): configurable default value Signed-off-by: Maxence Lange --- core/Command/Config/App/GetConfig.php | 4 +- .../Bootstrap/RegistrationContext.php | 3 +- .../Config/Lexicon/CoreConfigLexicon.php | 52 +++++++++++++++++++ lib/private/Config/UserConfig.php | 5 +- tests/lib/Config/UserConfigTest.php | 4 ++ 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 lib/private/Config/Lexicon/CoreConfigLexicon.php diff --git a/core/Command/Config/App/GetConfig.php b/core/Command/Config/App/GetConfig.php index f64efd3feaa7d..366e5f5c136cf 100644 --- a/core/Command/Config/App/GetConfig.php +++ b/core/Command/Config/App/GetConfig.php @@ -79,7 +79,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (!$input->hasParameterOption('--default-value')) { return 1; } - $configValue = $defaultValue; + + // get the default value defined from lexicon + $configValue = $this->appConfig->getValueMixed($appName, $configName, $defaultValue ?? ''); } $this->writeMixedInOutputFormat($input, $output, $configValue); diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index f3b612edc38bb..77cc58c6468a7 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -11,6 +11,7 @@ use Closure; use NCU\Config\Lexicon\IConfigLexicon; +use OC\Config\Lexicon\CoreConfigLexicon; use OC\Support\CrashReport\Registry; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IRegistrationContext; @@ -143,7 +144,7 @@ class RegistrationContext { private array $declarativeSettings = []; /** @var array */ - private array $configLexiconClasses = []; + private array $configLexiconClasses = ['core' => CoreConfigLexicon::class]; /** @var ServiceRegistration[] */ private array $teamResourceProviders = []; diff --git a/lib/private/Config/Lexicon/CoreConfigLexicon.php b/lib/private/Config/Lexicon/CoreConfigLexicon.php new file mode 100644 index 0000000000000..776eb59d46002 --- /dev/null +++ b/lib/private/Config/Lexicon/CoreConfigLexicon.php @@ -0,0 +1,52 @@ +isLazy(); - $default = $configValue->getDefault() ?? $default; // default from Lexicon got priority + // default from Lexicon got priority but it can still be overwritten by admin + // with 'lexicon.default.userconfig' => [. => 'my value'], in config.php + $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$app . '.' . $key] ?? $configValue->getDefault() ?? $default; $flags = $configValue->getFlags(); if ($configValue->isDeprecated()) { diff --git a/tests/lib/Config/UserConfigTest.php b/tests/lib/Config/UserConfigTest.php index e727116d9a82e..53ca9cdda98f5 100644 --- a/tests/lib/Config/UserConfigTest.php +++ b/tests/lib/Config/UserConfigTest.php @@ -12,6 +12,7 @@ use NCU\Config\IUserConfig; use NCU\Config\ValueType; use OC\Config\UserConfig; +use OCP\IConfig; use OCP\IDBConnection; use OCP\Security\ICrypto; use Psr\Log\LoggerInterface; @@ -26,6 +27,7 @@ */ class UserConfigTest extends TestCase { protected IDBConnection $connection; + private IConfig $config; private LoggerInterface $logger; private ICrypto $crypto; private array $originalPreferences; @@ -169,6 +171,7 @@ protected function setUp(): void { parent::setUp(); $this->connection = \OCP\Server::get(IDBConnection::class); + $this->config = \OCP\Server::get(IConfig::class); $this->logger = \OCP\Server::get(LoggerInterface::class); $this->crypto = \OCP\Server::get(ICrypto::class); @@ -277,6 +280,7 @@ protected function tearDown(): void { private function generateUserConfig(array $preLoading = []): IUserConfig { $userConfig = new \OC\Config\UserConfig( $this->connection, + $this->config, $this->logger, $this->crypto, );