Skip to content

Commit c4284a4

Browse files
committed
feat(lexicon): preset()
Signed-off-by: Maxence Lange <[email protected]>
1 parent 3acc4f0 commit c4284a4

File tree

10 files changed

+133
-3
lines changed

10 files changed

+133
-3
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'NCU\\Config\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/unstable/Config/Exceptions/UnknownKeyException.php',
1313
'NCU\\Config\\IUserConfig' => $baseDir . '/lib/unstable/Config/IUserConfig.php',
1414
'NCU\\Config\\Lexicon\\ConfigLexiconEntry' => $baseDir . '/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php',
15+
'NCU\\Config\\Lexicon\\ConfigLexiconPreset' => $baseDir . '/lib/unstable/Config/Lexicon/ConfigLexiconPreset.php',
1516
'NCU\\Config\\Lexicon\\ConfigLexiconStrictness' => $baseDir . '/lib/unstable/Config/Lexicon/ConfigLexiconStrictness.php',
1617
'NCU\\Config\\Lexicon\\IConfigLexicon' => $baseDir . '/lib/unstable/Config/Lexicon/IConfigLexicon.php',
1718
'NCU\\Config\\ValueType' => $baseDir . '/lib/unstable/Config/ValueType.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
5353
'NCU\\Config\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/unstable/Config/Exceptions/UnknownKeyException.php',
5454
'NCU\\Config\\IUserConfig' => __DIR__ . '/../../..' . '/lib/unstable/Config/IUserConfig.php',
5555
'NCU\\Config\\Lexicon\\ConfigLexiconEntry' => __DIR__ . '/../../..' . '/lib/unstable/Config/Lexicon/ConfigLexiconEntry.php',
56+
'NCU\\Config\\Lexicon\\ConfigLexiconPreset' => __DIR__ . '/../../..' . '/lib/unstable/Config/Lexicon/ConfigLexiconPreset.php',
5657
'NCU\\Config\\Lexicon\\ConfigLexiconStrictness' => __DIR__ . '/../../..' . '/lib/unstable/Config/Lexicon/ConfigLexiconStrictness.php',
5758
'NCU\\Config\\Lexicon\\IConfigLexicon' => __DIR__ . '/../../..' . '/lib/unstable/Config/Lexicon/IConfigLexicon.php',
5859
'NCU\\Config\\ValueType' => __DIR__ . '/../../..' . '/lib/unstable/Config/ValueType.php',

lib/private/AppConfig.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use InvalidArgumentException;
1313
use JsonException;
1414
use NCU\Config\Lexicon\ConfigLexiconEntry;
15+
use NCU\Config\Lexicon\ConfigLexiconPreset;
1516
use NCU\Config\Lexicon\ConfigLexiconStrictness;
1617
use NCU\Config\Lexicon\IConfigLexicon;
1718
use OC\AppFramework\Bootstrap\Coordinator;
@@ -70,6 +71,7 @@ class AppConfig implements IAppConfig {
7071

7172
public function __construct(
7273
protected IDBConnection $connection,
74+
protected IConfig $config,
7375
protected LoggerInterface $logger,
7476
protected ICrypto $crypto,
7577
) {
@@ -1147,6 +1149,7 @@ public function deleteApp(string $app): void {
11471149
public function clearCache(bool $reload = false): void {
11481150
$this->lazyLoaded = $this->fastLoaded = false;
11491151
$this->lazyCache = $this->fastCache = $this->valueTypes = [];
1152+
$this->configLexiconDetails = [];
11501153

11511154
if (!$reload) {
11521155
return;
@@ -1682,6 +1685,9 @@ private function applyLexiconStrictness(
16821685
*/
16831686
public function getConfigDetailsFromLexicon(string $appId): array {
16841687
if (!array_key_exists($appId, $this->configLexiconDetails)) {
1688+
if (ConfigManager::$preset === null) {
1689+
ConfigManager::$preset = ConfigLexiconPreset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? ConfigLexiconPreset::NONE;
1690+
}
16851691
$entries = $aliases = [];
16861692
$bootstrapCoordinator = \OCP\Server::get(Coordinator::class);
16871693
$configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId);

lib/private/Config/ConfigManager.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
use NCU\Config\Exceptions\TypeConflictException;
1313
use NCU\Config\IUserConfig;
1414
use NCU\Config\Lexicon\ConfigLexiconEntry;
15+
use NCU\Config\Lexicon\ConfigLexiconPreset;
1516
use NCU\Config\ValueType;
1617
use OC\AppConfig;
1718
use OCP\App\IAppManager;
1819
use OCP\IAppConfig;
20+
use OCP\IConfig;
1921
use OCP\Server;
2022
use Psr\Log\LoggerInterface;
2123

@@ -25,12 +27,18 @@
2527
* @since 32.0.0
2628
*/
2729
class ConfigManager {
30+
/** @since 32.0.0 */
31+
public const PRESET_CONFIGKEY = 'config_preset';
32+
/** @since 32.0.0 */
33+
public static ?ConfigLexiconPreset $preset = null;
34+
2835
/** @var AppConfig|null $appConfig */
2936
private ?IAppConfig $appConfig = null;
3037
/** @var UserConfig|null $userConfig */
3138
private ?IUserConfig $userConfig = null;
3239

3340
public function __construct(
41+
private readonly IConfig $config,
3442
private readonly LoggerInterface $logger,
3543
) {
3644
}
@@ -74,6 +82,11 @@ public function migrateConfigLexiconKeys(?string $appId = null): void {
7482
$this->userConfig->ignoreLexiconAliases(false);
7583
}
7684

85+
public function setLexiconPreset(ConfigLexiconPreset $preset): void {
86+
$this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value);
87+
self::$preset = null;
88+
}
89+
7790
/**
7891
* config services cannot be load at __construct() or install will fail
7992
*/

lib/private/Config/UserConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use NCU\Config\Exceptions\UnknownKeyException;
1717
use NCU\Config\IUserConfig;
1818
use NCU\Config\Lexicon\ConfigLexiconEntry;
19+
use NCU\Config\Lexicon\ConfigLexiconPreset;
1920
use NCU\Config\Lexicon\ConfigLexiconStrictness;
2021
use NCU\Config\ValueType;
2122
use OC\AppFramework\Bootstrap\Coordinator;
@@ -1626,6 +1627,7 @@ public function clearCache(string $userId, bool $reload = false): void {
16261627
public function clearCacheAll(): void {
16271628
$this->lazyLoaded = $this->fastLoaded = [];
16281629
$this->lazyCache = $this->fastCache = $this->valueDetails = [];
1630+
$this->configLexiconDetails = [];
16291631
}
16301632

16311633
/**
@@ -1993,6 +1995,9 @@ private function applyLexiconStrictness(?ConfigLexiconStrictness $strictness, st
19931995
*/
19941996
public function getConfigDetailsFromLexicon(string $appId): array {
19951997
if (!array_key_exists($appId, $this->configLexiconDetails)) {
1998+
if (ConfigManager::$preset === null) {
1999+
ConfigManager::$preset = ConfigLexiconPreset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? ConfigLexiconPreset::NONE;
2000+
}
19962001
$entries = $aliases = [];
19972002
$bootstrapCoordinator = \OCP\Server::get(Coordinator::class);
19982003
$configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId);

lib/unstable/Config/Lexicon/ConfigLexiconEntry.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace NCU\Config\Lexicon;
1010

1111
use NCU\Config\ValueType;
12+
use OC\Config\ConfigManager;
1213

1314
/**
1415
* Model that represent config values within an app config lexicon.
@@ -24,7 +25,7 @@ class ConfigLexiconEntry {
2425
private ?string $default = null;
2526

2627
/**
27-
* @param string $key config key
28+
* @param string $key config key, can only contain alphanumerical chars and -._
2829
* @param ValueType $type type of config value
2930
* @param string $definition optional description of config key available when using occ command
3031
* @param bool $lazy set config value as lazy
@@ -47,12 +48,35 @@ public function __construct(
4748
private readonly ?string $rename = null,
4849
private readonly int $options = 0,
4950
) {
51+
// key can only contain alphanumeric chars and _-.
52+
if (preg_match("/[^[:alnum:]\-._]/", $key)) {
53+
throw new \Exception();
54+
}
5055
/** @psalm-suppress UndefinedClass */
5156
if (\OC::$CLI) { // only store definition if ran from CLI
5257
$this->definition = $definition;
5358
}
5459
}
5560

61+
/**
62+
* @param ConfigLexiconPreset|ConfigLexiconPreset[] $preset
63+
* @experimental 32.0.0
64+
*/
65+
public function preset(ConfigLexiconPreset|array $preset, string|int|float|bool|array $defaultRaw): self {
66+
if (!is_array($preset)) {
67+
$preset = [$preset];
68+
}
69+
70+
foreach ($preset as $entry) {
71+
if ($entry === ConfigManager::$preset) {
72+
$this->defaultRaw = $defaultRaw;
73+
break;
74+
}
75+
}
76+
77+
return $this;
78+
}
79+
5680
/**
5781
* returns the config key
5882
*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-only
7+
*/
8+
9+
namespace NCU\Config\Lexicon;
10+
11+
/**
12+
* list of preset to handle the default behavior of the instance
13+
*
14+
* @see ConfigLexiconEntry::preset
15+
*
16+
* - **ConfigLexiconPreset::LARGE** - Large size organisation (> 50k accounts)
17+
* - **ConfigLexiconPreset::MEDIUM** - Medium size organisation (> 100 accounts)
18+
* - **ConfigLexiconPreset::SMALL** - Small size organisation (< 100 accounts)
19+
* - **ConfigLexiconPreset::SHARED** - Shared hosting
20+
* - **ConfigLexiconPreset::EDUCATION** - School/University
21+
* - **ConfigLexiconPreset::CLUB** - Club/Association
22+
* - **ConfigLexiconPreset::FAMILY** - Family
23+
* - **ConfigLexiconPreset::PRIVATE** - Private
24+
*
25+
* @experimental 32.0.0
26+
*/
27+
enum ConfigLexiconPreset: int {
28+
/** @experimental 32.0.0 */
29+
case LARGE = 8;
30+
/** @experimental 32.0.0 */
31+
case MEDIUM = 7;
32+
/** @experimental 32.0.0 */
33+
case SMALL = 6;
34+
/** @experimental 32.0.0 */
35+
case SHARED = 5;
36+
/** @experimental 32.0.0 */
37+
case EDUCATION = 4;
38+
/** @experimental 32.0.0 */
39+
case CLUB = 3;
40+
/** @experimental 32.0.0 */
41+
case FAMILY = 2;
42+
/** @experimental 32.0.0 */
43+
case PRIVATE = 1;
44+
/** @experimental 32.0.0 */
45+
case NONE = 0;
46+
}

tests/lib/AppConfigTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCP\Exceptions\AppConfigTypeConflictException;
1313
use OCP\Exceptions\AppConfigUnknownKeyException;
1414
use OCP\IAppConfig;
15+
use OCP\IConfig;
1516
use OCP\IDBConnection;
1617
use OCP\Security\ICrypto;
1718
use OCP\Server;
@@ -27,6 +28,7 @@
2728
class AppConfigTest extends TestCase {
2829
protected IAppConfig $appConfig;
2930
protected IDBConnection $connection;
31+
private IConfig $config;
3032
private LoggerInterface $logger;
3133
private ICrypto $crypto;
3234

@@ -89,6 +91,7 @@ protected function setUp(): void {
8991
parent::setUp();
9092

9193
$this->connection = Server::get(IDBConnection::class);
94+
$this->config = Server::get(IConfig::class);
9295
$this->logger = Server::get(LoggerInterface::class);
9396
$this->crypto = Server::get(ICrypto::class);
9497

@@ -179,6 +182,7 @@ private function generateAppConfig(bool $preLoading = true): IAppConfig {
179182
/** @var AppConfig $config */
180183
$config = new AppConfig(
181184
$this->connection,
185+
$this->config,
182186
$this->logger,
183187
$this->crypto,
184188
);

tests/lib/Config/LexiconTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use NCU\Config\Exceptions\TypeConflictException;
1111
use NCU\Config\Exceptions\UnknownKeyException;
1212
use NCU\Config\IUserConfig;
13+
use NCU\Config\Lexicon\ConfigLexiconPreset;
1314
use OC\AppConfig;
1415
use OC\AppFramework\Bootstrap\Coordinator;
1516
use OC\Config\ConfigManager;
@@ -203,4 +204,30 @@ public function testAppConfigLexiconRenameInvertBoolean() {
203204
$this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID);
204205
$this->assertSame(false, $this->appConfig->getValueBool(TestConfigLexicon_I::APPID, 'key4'));
205206
}
207+
208+
public function testAppConfigLexiconPreset() {
209+
$this->configManager->setLexiconPreset(ConfigLexiconPreset::FAMILY);
210+
$this->assertSame('family', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3'));
211+
}
212+
213+
public function testAppConfigLexiconPresets() {
214+
$this->configManager->setLexiconPreset(ConfigLexiconPreset::MEDIUM);
215+
$this->assertSame('club+medium', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3'));
216+
$this->configManager->setLexiconPreset(ConfigLexiconPreset::CLUB);
217+
$this->appConfig->clearCache();
218+
$this->assertSame('club+medium', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3'));
219+
}
220+
221+
public function testUserConfigLexiconPreset() {
222+
$this->configManager->setLexiconPreset(ConfigLexiconPreset::FAMILY);
223+
$this->assertSame('family', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3'));
224+
}
225+
226+
public function testUserConfigLexiconPresets() {
227+
$this->configManager->setLexiconPreset(ConfigLexiconPreset::MEDIUM);
228+
$this->assertSame('club+medium', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3'));
229+
$this->configManager->setLexiconPreset(ConfigLexiconPreset::CLUB);
230+
$this->userConfig->clearCacheAll();
231+
$this->assertSame('club+medium', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3'));
232+
}
206233
}

tests/lib/Config/TestConfigLexicon_E.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use NCU\Config\IUserConfig;
1212
use NCU\Config\Lexicon\ConfigLexiconEntry;
13+
use NCU\Config\Lexicon\ConfigLexiconPreset;
1314
use NCU\Config\Lexicon\ConfigLexiconStrictness;
1415
use NCU\Config\Lexicon\IConfigLexicon;
1516
use NCU\Config\ValueType;
@@ -25,14 +26,16 @@ public function getStrictness(): ConfigLexiconStrictness {
2526
public function getAppConfigs(): array {
2627
return [
2728
new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IAppConfig::FLAG_SENSITIVE),
28-
new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false)
29+
new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false),
30+
(new ConfigLexiconEntry('key3', ValueType::STRING, 'none', 'test key'))->preset(ConfigLexiconPreset::FAMILY, 'family')->preset([ConfigLexiconPreset::CLUB, ConfigLexiconPreset::MEDIUM], 'club+medium'),
2931
];
3032
}
3133

3234
public function getUserConfigs(): array {
3335
return [
3436
new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IUserConfig::FLAG_SENSITIVE),
35-
new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false)
37+
new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false),
38+
(new ConfigLexiconEntry('key3', ValueType::STRING, 'none', 'test key'))->preset(ConfigLexiconPreset::FAMILY, 'family')->preset([ConfigLexiconPreset::CLUB, ConfigLexiconPreset::MEDIUM], 'club+medium'),
3639
];
3740
}
3841
}

0 commit comments

Comments
 (0)