Skip to content

Commit

Permalink
feat(lexicon): enforce default userconfig
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Dec 17, 2024
1 parent 122f203 commit 422dcb2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lib/private/Config/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
26 changes: 18 additions & 8 deletions lib/unstable/Config/Lexicon/ConfigLexiconEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
}

/**
* @inheritDoc
* returns the config key
*
* @return string config key
* @experimental 31.0.0
Expand All @@ -58,7 +58,7 @@ public function getKey(): string {
}

/**
* @inheritDoc
* get expected type for config value
*
* @return ValueType
* @experimental 31.0.0
Expand Down Expand Up @@ -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
Expand All @@ -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()) {
Expand All @@ -147,7 +157,7 @@ public function convertToString(string|int|float|bool|array $entry): string {
}

/**
* @inheritDoc
* returns definition
*
* @return string
* @experimental 31.0.0
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/Command/Config/App/GetConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 422dcb2

Please sign in to comment.