From 422dcb2ba50fbad48fbd009cc7f4bea1fd848577 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 17 Dec 2024 09:44:16 -0100 Subject: [PATCH] feat(lexicon): enforce default userconfig Signed-off-by: Maxence Lange --- lib/private/Config/UserConfig.php | 5 ++-- .../Config/Lexicon/ConfigLexiconEntry.php | 26 +++++++++++++------ .../Core/Command/Config/App/GetConfigTest.php | 6 ++--- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/private/Config/UserConfig.php b/lib/private/Config/UserConfig.php index 9e8f93f89a6e5..741070ba0979d 100644 --- a/lib/private/Config/UserConfig.php +++ b/lib/private/Config/UserConfig.php @@ -1850,14 +1850,15 @@ private function matchAndApplyLexiconDefinition( $this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.'); } - if ($this->hasKey($userId, $app, $key, $lazy)) { + $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false; + if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) { return true; // if key exists there should be no need to extract default } // default from Lexicon got priority but it can still be overwritten by admin $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault() ?? $default; - return true; + return !$enforcedValue; // returning false will make get() returning $default and set() not changing value in database } /** diff --git a/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php b/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php index b7a7fad56a863..2cf196113f7aa 100644 --- a/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php +++ b/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php @@ -48,7 +48,7 @@ public function __construct( } /** - * @inheritDoc + * returns the config key * * @return string config key * @experimental 31.0.0 @@ -58,7 +58,7 @@ public function getKey(): string { } /** - * @inheritDoc + * get expected type for config value * * @return ValueType * @experimental 31.0.0 @@ -113,7 +113,7 @@ private function convertFromArray(array $default): string { } /** - * @inheritDoc + * returns default value * * @return string|null NULL if no default is set * @experimental 31.0.0 @@ -130,10 +130,20 @@ public function getDefault(): ?string { return $this->default; } + /** + * convert $entry into string, based on the expected type for config value + * + * @param string|int|float|bool|array $entry + * + * @return string + * @experimental 31.0.0 + * @psalm-suppress PossiblyInvalidCast arrays are managed pre-cast + * @psalm-suppress RiskyCast + */ public function convertToString(string|int|float|bool|array $entry): string { // in case $default is array but is not expected to be an array... if ($this->getValueType() !== ValueType::ARRAY && is_array($entry)) { - $entry = json_encode($entry) ?? ''; + $entry = json_encode($entry, JSON_THROW_ON_ERROR); } return match ($this->getValueType()) { @@ -147,7 +157,7 @@ public function convertToString(string|int|float|bool|array $entry): string { } /** - * @inheritDoc + * returns definition * * @return string * @experimental 31.0.0 @@ -157,7 +167,7 @@ public function getDefinition(): string { } /** - * @inheritDoc + * returns if config key is set as lazy * * @see IAppConfig for details on lazy config values * @return bool TRUE if config value is lazy @@ -168,7 +178,7 @@ public function isLazy(): bool { } /** - * @inheritDoc + * returns flags * * @see IAppConfig for details on sensitive config values * @return int bitflag about the config value @@ -189,7 +199,7 @@ public function isFlagged(int $flag): bool { } /** - * @inheritDoc + * returns if config key is set as deprecated * * @return bool TRUE if config si deprecated * @experimental 31.0.0 diff --git a/tests/Core/Command/Config/App/GetConfigTest.php b/tests/Core/Command/Config/App/GetConfigTest.php index a88160e2bfb7c..33e50699bbec8 100644 --- a/tests/Core/Command/Config/App/GetConfigTest.php +++ b/tests/Core/Command/Config/App/GetConfigTest.php @@ -103,9 +103,9 @@ public function testGet($configName, $value, $configExists, $defaultValue, $hasD ->willReturn(['value' => $value]); } else { $this->config->expects($this->once()) - ->method('getValueMixed') - ->with('app-name', $configName, $defaultValue) - ->willReturn($defaultValue); + ->method('getValueMixed') + ->with('app-name', $configName, $defaultValue) + ->willReturn($defaultValue); } }