diff options
Diffstat (limited to 'lib/private/Config')
-rw-r--r-- | lib/private/Config/ConfigManager.php | 38 | ||||
-rw-r--r-- | lib/private/Config/Lexicon/CoreConfigLexicon.php | 43 | ||||
-rw-r--r-- | lib/private/Config/UserConfig.php | 76 |
3 files changed, 77 insertions, 80 deletions
diff --git a/lib/private/Config/ConfigManager.php b/lib/private/Config/ConfigManager.php index 1980269e2ca..ed516abdcbf 100644 --- a/lib/private/Config/ConfigManager.php +++ b/lib/private/Config/ConfigManager.php @@ -9,13 +9,15 @@ declare(strict_types=1); namespace OC\Config; use JsonException; -use NCU\Config\Exceptions\TypeConflictException; -use NCU\Config\IUserConfig; -use NCU\Config\Lexicon\ConfigLexiconEntry; -use NCU\Config\ValueType; use OC\AppConfig; use OCP\App\IAppManager; +use OCP\Config\Exceptions\TypeConflictException; +use OCP\Config\IUserConfig; +use OCP\Config\Lexicon\Entry; +use OCP\Config\Lexicon\Preset; +use OCP\Config\ValueType; use OCP\IAppConfig; +use OCP\IConfig; use OCP\Server; use Psr\Log\LoggerInterface; @@ -25,12 +27,16 @@ use Psr\Log\LoggerInterface; * @since 32.0.0 */ class ConfigManager { + /** @since 32.0.0 */ + public const PRESET_CONFIGKEY = 'config_preset'; + /** @var AppConfig|null $appConfig */ private ?IAppConfig $appConfig = null; /** @var UserConfig|null $userConfig */ private ?IUserConfig $userConfig = null; public function __construct( + private readonly IConfig $config, private readonly LoggerInterface $logger, ) { } @@ -44,10 +50,11 @@ class ConfigManager { * * This method should be mainly called during a new upgrade or when a new app is enabled. * - * @see ConfigLexiconEntry + * @param string|null $appId when set to NULL the method will be executed for all enabled apps of the instance + * * @internal * @since 32.0.0 - * @param string|null $appId when set to NULL the method will be executed for all enabled apps of the instance + * @see Entry */ public function migrateConfigLexiconKeys(?string $appId = null): void { if ($appId === null) { @@ -75,6 +82,17 @@ class ConfigManager { } /** + * store in config.php the new preset + * refresh cached preset + */ + public function setLexiconPreset(Preset $preset): void { + $this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value); + $this->loadConfigServices(); + $this->appConfig->clearCache(); + $this->userConfig->clearCacheAll(); + } + + /** * config services cannot be load at __construct() or install will fail */ private function loadConfigServices(): void { @@ -149,7 +167,7 @@ class ConfigManager { * * @throws TypeConflictException if previous value does not fit the expected type */ - private function migrateAppConfigValue(string $appId, ConfigLexiconEntry $entry): void { + private function migrateAppConfigValue(string $appId, Entry $entry): void { $value = $this->appConfig->getValueMixed($appId, $entry->getRename(), lazy: null); switch ($entry->getValueType()) { case ValueType::STRING: @@ -179,7 +197,7 @@ class ConfigManager { * * @throws TypeConflictException if previous value does not fit the expected type */ - private function migrateUserConfigValue(string $userId, string $appId, ConfigLexiconEntry $entry): void { + private function migrateUserConfigValue(string $userId, string $appId, Entry $entry): void { $value = $this->userConfig->getValueMixed($userId, $appId, $entry->getRename(), lazy: null); switch ($entry->getValueType()) { case ValueType::STRING: @@ -220,7 +238,7 @@ class ConfigManager { return (float)$value; } - public function convertToBool(string $value, ?ConfigLexiconEntry $entry = null): bool { + public function convertToBool(string $value, ?Entry $entry = null): bool { if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { $valueBool = true; } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { @@ -228,7 +246,7 @@ class ConfigManager { } else { throw new TypeConflictException('Value cannot be converted to boolean'); } - if ($entry?->hasOption(ConfigLexiconEntry::RENAME_INVERT_BOOLEAN) === true) { + if ($entry?->hasOption(Entry::RENAME_INVERT_BOOLEAN) === true) { $valueBool = !$valueBool; } diff --git a/lib/private/Config/Lexicon/CoreConfigLexicon.php b/lib/private/Config/Lexicon/CoreConfigLexicon.php deleted file mode 100644 index 34a0b883c54..00000000000 --- a/lib/private/Config/Lexicon/CoreConfigLexicon.php +++ /dev/null @@ -1,43 +0,0 @@ -<?php - -declare(strict_types=1); -/** - * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -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 { - public function getStrictness(): ConfigLexiconStrictness { - return ConfigLexiconStrictness::IGNORE; - } - - /** - * @inheritDoc - * @return ConfigLexiconEntry[] - */ - public function getAppConfigs(): array { - return [ - new ConfigLexiconEntry('lastcron', ValueType::INT, 0, 'timestamp of last cron execution'), - ]; - } - - /** - * @inheritDoc - * @return ConfigLexiconEntry[] - */ - 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 7848f1728e3..04ba0e29db0 100644 --- a/lib/private/Config/UserConfig.php +++ b/lib/private/Config/UserConfig.php @@ -11,14 +11,16 @@ namespace OC\Config; use Generator; use InvalidArgumentException; use JsonException; -use NCU\Config\Exceptions\IncorrectTypeException; -use NCU\Config\Exceptions\TypeConflictException; -use NCU\Config\Exceptions\UnknownKeyException; -use NCU\Config\IUserConfig; -use NCU\Config\Lexicon\ConfigLexiconEntry; -use NCU\Config\Lexicon\ConfigLexiconStrictness; -use NCU\Config\ValueType; use OC\AppFramework\Bootstrap\Coordinator; +use OCP\Config\Exceptions\IncorrectTypeException; +use OCP\Config\Exceptions\TypeConflictException; +use OCP\Config\Exceptions\UnknownKeyException; +use OCP\Config\IUserConfig; +use OCP\Config\Lexicon\Entry; +use OCP\Config\Lexicon\ILexicon; +use OCP\Config\Lexicon\Preset; +use OCP\Config\Lexicon\Strictness; +use OCP\Config\ValueType; use OCP\DB\Exception as DBException; use OCP\DB\IResult; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -63,9 +65,10 @@ class UserConfig implements IUserConfig { private array $fastLoaded = []; /** @var array<string, boolean> ['user_id' => bool] */ private array $lazyLoaded = []; - /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ + /** @var array<string, array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ private array $configLexiconDetails = []; private bool $ignoreLexiconAliases = false; + private ?Preset $configLexiconPreset = null; public function __construct( protected IDBConnection $connection, @@ -721,10 +724,17 @@ class UserConfig implements IUserConfig { ): string { $this->assertParams($userId, $app, $key); $origKey = $key; - if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default)) { - // returns default if strictness of lexicon is set to WARNING (block and report) + $matched = $this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default); + if ($default === null) { + // there is no logical reason for it to be null + throw new \Exception('default cannot be null'); + } + + // returns default if strictness of lexicon is set to WARNING (block and report) + if (!$matched) { return $default; } + $this->loadConfig($userId, $lazy); /** @@ -1625,7 +1635,8 @@ class UserConfig implements IUserConfig { */ public function clearCacheAll(): void { $this->lazyLoaded = $this->fastLoaded = []; - $this->lazyCache = $this->fastCache = $this->valueDetails = []; + $this->lazyCache = $this->fastCache = $this->valueDetails = $this->configLexiconDetails = []; + $this->configLexiconPreset = null; } /** @@ -1886,7 +1897,7 @@ class UserConfig implements IUserConfig { ?bool &$lazy = null, ValueType &$type = ValueType::MIXED, int &$flags = 0, - string &$default = '', + ?string &$default = null, ): bool { $configDetails = $this->getConfigDetailsFromLexicon($app); if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { @@ -1903,7 +1914,7 @@ class UserConfig implements IUserConfig { return true; } - /** @var ConfigLexiconEntry $configValue */ + /** @var Entry $configValue */ $configValue = $configDetails['entries'][$key]; if ($type === ValueType::MIXED) { // we overwrite if value was requested as mixed @@ -1924,8 +1935,10 @@ class UserConfig implements IUserConfig { return true; } - // default from Lexicon got priority but it can still be overwritten by admin - $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault() ?? $default; + // only look for default if needed, default from Lexicon got priority if not overwritten by admin + if ($default !== null) { + $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault($this->getLexiconPreset()) ?? $default; + } // returning false will make get() returning $default and set() not changing value in database return !$enforcedValue; @@ -1942,7 +1955,7 @@ class UserConfig implements IUserConfig { * * The entry is converted to string to fit the expected type when managing default value */ - private function getSystemDefault(string $appId, ConfigLexiconEntry $configValue): ?string { + private function getSystemDefault(string $appId, Entry $configValue): ?string { $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$appId][$configValue->getKey()] ?? null; if ($default === null) { // no system default, using default default. @@ -1955,28 +1968,28 @@ class UserConfig implements IUserConfig { /** * manage ConfigLexicon behavior based on strictness set in IConfigLexicon * - * @see IConfigLexicon::getStrictness() - * @param ConfigLexiconStrictness|null $strictness + * @param Strictness|null $strictness * @param string $line * * @return bool TRUE if conflict can be fully ignored * @throws UnknownKeyException + *@see ILexicon::getStrictness() */ - private function applyLexiconStrictness(?ConfigLexiconStrictness $strictness, string $line = ''): bool { + private function applyLexiconStrictness(?Strictness $strictness, string $line = ''): bool { if ($strictness === null) { return true; } switch ($strictness) { - case ConfigLexiconStrictness::IGNORE: + case Strictness::IGNORE: return true; - case ConfigLexiconStrictness::NOTICE: + case Strictness::NOTICE: $this->logger->notice($line); return true; - case ConfigLexiconStrictness::WARNING: + case Strictness::WARNING: $this->logger->warning($line); return false; - case ConfigLexiconStrictness::EXCEPTION: + case Strictness::EXCEPTION: throw new UnknownKeyException($line); } @@ -1987,9 +2000,10 @@ class UserConfig implements IUserConfig { * extract details from registered $appId's config lexicon * * @param string $appId - * @internal * - * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} + * @return array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness} + *@internal + * */ public function getConfigDetailsFromLexicon(string $appId): array { if (!array_key_exists($appId, $this->configLexiconDetails)) { @@ -2006,14 +2020,14 @@ class UserConfig implements IUserConfig { $this->configLexiconDetails[$appId] = [ 'entries' => $entries, 'aliases' => $aliases, - 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE + 'strictness' => $configLexicon?->getStrictness() ?? Strictness::IGNORE ]; } return $this->configLexiconDetails[$appId]; } - private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { + private function getLexiconEntry(string $appId, string $key): ?Entry { return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; } @@ -2025,4 +2039,12 @@ class UserConfig implements IUserConfig { public function ignoreLexiconAliases(bool $ignore): void { $this->ignoreLexiconAliases = $ignore; } + + private function getLexiconPreset(): Preset { + if ($this->configLexiconPreset === null) { + $this->configLexiconPreset = Preset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? Preset::NONE; + } + + return $this->configLexiconPreset; + } } |