aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2025-01-09 09:42:53 -0100
committerMaxence Lange <maxence@artificial-owl.com>2025-01-14 10:36:07 -0100
commitbd4a154d6406e986a05522f3f71b74d4dc284fdb (patch)
tree1c5fe6bf243d3757a2e4945c7f4661a8c88a30a7 /lib
parentd4b862e513d9b548be11b55cf490d56e7f57461a (diff)
downloadnextcloud-server-bd4a154d6406e986a05522f3f71b74d4dc284fdb.tar.gz
nextcloud-server-bd4a154d6406e986a05522f3f71b74d4dc284fdb.zip
feat(lexicon): configurable default value
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/AppFramework/Bootstrap/RegistrationContext.php3
-rw-r--r--lib/private/Config/Lexicon/CoreConfigLexicon.php50
-rw-r--r--lib/private/Config/UserConfig.php48
-rw-r--r--lib/unstable/Config/Lexicon/ConfigLexiconEntry.php63
6 files changed, 138 insertions, 28 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index ffa8da43873..9809ea53508 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -1157,6 +1157,7 @@ return array(
'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => $baseDir . '/lib/private/Config.php',
+ 'OC\\Config\\Lexicon\\CoreConfigLexicon' => $baseDir . '/lib/private/Config/Lexicon/CoreConfigLexicon.php',
'OC\\Config\\UserConfig' => $baseDir . '/lib/private/Config/UserConfig.php',
'OC\\Console\\Application' => $baseDir . '/lib/private/Console/Application.php',
'OC\\Console\\TimestampFormatter' => $baseDir . '/lib/private/Console/TimestampFormatter.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 89c0cd4395b..f612e0d5fc6 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -1198,6 +1198,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php',
+ 'OC\\Config\\Lexicon\\CoreConfigLexicon' => __DIR__ . '/../../..' . '/lib/private/Config/Lexicon/CoreConfigLexicon.php',
'OC\\Config\\UserConfig' => __DIR__ . '/../../..' . '/lib/private/Config/UserConfig.php',
'OC\\Console\\Application' => __DIR__ . '/../../..' . '/lib/private/Console/Application.php',
'OC\\Console\\TimestampFormatter' => __DIR__ . '/../../..' . '/lib/private/Console/TimestampFormatter.php',
diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php
index f3b612edc38..77cc58c6468 100644
--- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php
+++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php
@@ -11,6 +11,7 @@ namespace OC\AppFramework\Bootstrap;
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<array-key, string> */
- private array $configLexiconClasses = [];
+ private array $configLexiconClasses = ['core' => CoreConfigLexicon::class];
/** @var ServiceRegistration<ITeamResourceProvider>[] */
private array $teamResourceProviders = [];
diff --git a/lib/private/Config/Lexicon/CoreConfigLexicon.php b/lib/private/Config/Lexicon/CoreConfigLexicon.php
new file mode 100644
index 00000000000..f6bc586677b
--- /dev/null
+++ b/lib/private/Config/Lexicon/CoreConfigLexicon.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace OC\Config\Lexicon;
+
+use NCU\Config\Lexicon\ConfigLexiconEntry;
+use NCU\Config\Lexicon\ConfigLexiconStrictness;
+use NCU\Config\Lexicon\IConfigLexicon;
+use NCU\Config\ValueType;
+
+/**
+ * ConfigLexicon for 'core' app/user configs
+ */
+class CoreConfigLexicon implements IConfigLexicon {
+ /**
+ * @inheritDoc
+ * @return ConfigLexiconStrictness
+ * @since 31.0.0
+ */
+ public function getStrictness(): ConfigLexiconStrictness {
+ return ConfigLexiconStrictness::IGNORE;
+ }
+
+ /**
+ * @inheritDoc
+ * @return ConfigLexiconEntry[]
+ * @since 31.0.0
+ */
+ public function getAppConfigs(): array {
+ return [
+ new ConfigLexiconEntry('lastcron', ValueType::INT, 0, 'timestamp of last cron execution'),
+ ];
+ }
+
+ /**
+ * @inheritDoc
+ * @return ConfigLexiconEntry[]
+ * @since 31.0.0
+ */
+ public function getUserConfigs(): array {
+ return [
+ new ConfigLexiconEntry('lang', ValueType::STRING, null, 'language'),
+ ];
+ }
+}
diff --git a/lib/private/Config/UserConfig.php b/lib/private/Config/UserConfig.php
index 78c43fc4321..ed225409d1c 100644
--- a/lib/private/Config/UserConfig.php
+++ b/lib/private/Config/UserConfig.php
@@ -71,6 +71,7 @@ class UserConfig implements IUserConfig {
public function __construct(
protected IDBConnection $connection,
+ protected IConfig $config,
protected LoggerInterface $logger,
protected ICrypto $crypto,
) {
@@ -711,7 +712,7 @@ class UserConfig implements IUserConfig {
ValueType $type,
): string {
$this->assertParams($userId, $app, $key);
- if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, default: $default)) {
+ if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default)) {
return $default; // returns default if strictness of lexicon is set to WARNING (block and report)
}
$this->loadConfig($userId, $lazy);
@@ -1046,7 +1047,7 @@ class UserConfig implements IUserConfig {
ValueType $type,
): bool {
$this->assertParams($userId, $app, $key);
- if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $flags)) {
+ if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, $flags)) {
return false; // returns false as database is not updated
}
$this->loadConfig($userId, $lazy);
@@ -1822,6 +1823,7 @@ class UserConfig implements IUserConfig {
* @throws TypeConflictException
*/
private function matchAndApplyLexiconDefinition(
+ string $userId,
string $app,
string $key,
bool &$lazy,
@@ -1837,20 +1839,54 @@ class UserConfig implements IUserConfig {
/** @var ConfigLexiconEntry $configValue */
$configValue = $configDetails['entries'][$key];
if ($type === ValueType::MIXED) {
- $type = $configValue->getValueType(); // we overwrite if value was requested as mixed
+ // we overwrite if value was requested as mixed
+ $type = $configValue->getValueType();
} elseif ($configValue->getValueType() !== $type) {
throw new TypeConflictException('The user config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon');
}
$lazy = $configValue->isLazy();
- $default = $configValue->getDefault() ?? $default; // default from Lexicon got priority
$flags = $configValue->getFlags();
-
if ($configValue->isDeprecated()) {
$this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.');
}
- return true;
+ $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false;
+ if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) {
+ // if key exists there should be no need to extract default
+ return true;
+ }
+
+ // default from Lexicon got priority but it can still be overwritten by admin
+ $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault() ?? $default;
+
+ // returning false will make get() returning $default and set() not changing value in database
+ return !$enforcedValue;
+ }
+
+ /**
+ * get default value set in config/config.php if stored in key:
+ *
+ * 'lexicon.default.userconfig' => [
+ * <appId> => [
+ * <configKey> => 'my value',
+ * ]
+ * ],
+ *
+ * The entry is converted to string to fit the expected type when managing default value
+ *
+ * @param string $appId
+ * @param ConfigLexiconEntry $configValue
+ *
+ * @return string|null
+ */
+ private function getSystemDefault(string $appId, ConfigLexiconEntry $configValue): ?string {
+ $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$appId][$configValue->getKey()] ?? null;
+ if ($default === null) {
+ return null; // no system default, using default default.
+ }
+
+ return $configValue->convertToString($default);
}
/**
diff --git a/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php b/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php
index e6c6579881d..2cf196113f7 100644
--- a/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php
+++ b/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php
@@ -35,25 +35,12 @@ class ConfigLexiconEntry {
public function __construct(
private readonly string $key,
private readonly ValueType $type,
- null|string|int|float|bool|array $default = null,
+ private null|string|int|float|bool|array $defaultRaw = null,
string $definition = '',
private readonly bool $lazy = false,
private readonly int $flags = 0,
private readonly bool $deprecated = false,
) {
- if ($default !== null) {
- // in case $default is array but is not expected to be an array...
- $default = ($type !== ValueType::ARRAY && is_array($default)) ? json_encode($default) : $default;
- $this->default = match ($type) {
- ValueType::MIXED => (string)$default,
- ValueType::STRING => $this->convertFromString((string)$default),
- ValueType::INT => $this->convertFromInt((int)$default),
- ValueType::FLOAT => $this->convertFromFloat((float)$default),
- ValueType::BOOL => $this->convertFromBool((bool)$default),
- ValueType::ARRAY => $this->convertFromArray((array)$default)
- };
- }
-
/** @psalm-suppress UndefinedClass */
if (\OC::$CLI) { // only store definition if ran from CLI
$this->definition = $definition;
@@ -61,7 +48,7 @@ class ConfigLexiconEntry {
}
/**
- * @inheritDoc
+ * returns the config key
*
* @return string config key
* @experimental 31.0.0
@@ -71,7 +58,7 @@ class ConfigLexiconEntry {
}
/**
- * @inheritDoc
+ * get expected type for config value
*
* @return ValueType
* @experimental 31.0.0
@@ -126,17 +113,51 @@ class ConfigLexiconEntry {
}
/**
- * @inheritDoc
+ * returns default value
*
* @return string|null NULL if no default is set
* @experimental 31.0.0
*/
public function getDefault(): ?string {
+ if ($this->defaultRaw === null) {
+ return null;
+ }
+
+ if ($this->default === null) {
+ $this->default = $this->convertToString($this->defaultRaw);
+ }
+
return $this->default;
}
/**
- * @inheritDoc
+ * 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, JSON_THROW_ON_ERROR);
+ }
+
+ return match ($this->getValueType()) {
+ ValueType::MIXED => (string)$entry,
+ ValueType::STRING => $this->convertFromString((string)$entry),
+ ValueType::INT => $this->convertFromInt((int)$entry),
+ ValueType::FLOAT => $this->convertFromFloat((float)$entry),
+ ValueType::BOOL => $this->convertFromBool((bool)$entry),
+ ValueType::ARRAY => $this->convertFromArray((array)$entry)
+ };
+ }
+
+ /**
+ * returns definition
*
* @return string
* @experimental 31.0.0
@@ -146,7 +167,7 @@ class ConfigLexiconEntry {
}
/**
- * @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
@@ -157,7 +178,7 @@ class ConfigLexiconEntry {
}
/**
- * @inheritDoc
+ * returns flags
*
* @see IAppConfig for details on sensitive config values
* @return int bitflag about the config value
@@ -178,7 +199,7 @@ class ConfigLexiconEntry {
}
/**
- * @inheritDoc
+ * returns if config key is set as deprecated
*
* @return bool TRUE if config si deprecated
* @experimental 31.0.0