Skip to content

Commit 8e1cf04

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

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

lib/private/AppConfig.php

Lines changed: 5 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
) {
@@ -1682,6 +1684,9 @@ private function applyLexiconStrictness(
16821684
*/
16831685
public function getConfigDetailsFromLexicon(string $appId): array {
16841686
if (!array_key_exists($appId, $this->configLexiconDetails)) {
1687+
if (!defined(ConfigManager::PRESET_CONSTANT)) {
1688+
define(ConfigManager::PRESET_CONSTANT, ConfigLexiconPreset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)));
1689+
}
16851690
$entries = $aliases = [];
16861691
$bootstrapCoordinator = \OCP\Server::get(Coordinator::class);
16871692
$configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId);

lib/private/Config/ConfigManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
* @since 32.0.0
2626
*/
2727
class ConfigManager {
28+
/** @since 32.0.0 */
29+
public const PRESET_CONFIGKEY = 'config.preset';
30+
/** @since 32.0.0 */
31+
public const PRESET_CONSTANT = '_LEXICON_PRESET_';
32+
2833
/** @var AppConfig|null $appConfig */
2934
private ?IAppConfig $appConfig = null;
3035
/** @var UserConfig|null $userConfig */

lib/private/Config/Lexicon/CoreConfigLexicon.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OC\Config\Lexicon;
1010

1111
use NCU\Config\Lexicon\ConfigLexiconEntry;
12+
use NCU\Config\Lexicon\ConfigLexiconPreset;
1213
use NCU\Config\Lexicon\ConfigLexiconStrictness;
1314
use NCU\Config\Lexicon\IConfigLexicon;
1415
use NCU\Config\ValueType;

lib/private/Config/UserConfig.php

Lines changed: 4 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;
@@ -1993,6 +1994,9 @@ private function applyLexiconStrictness(?ConfigLexiconStrictness $strictness, st
19931994
*/
19941995
public function getConfigDetailsFromLexicon(string $appId): array {
19951996
if (!array_key_exists($appId, $this->configLexiconDetails)) {
1997+
if (!defined(ConfigManager::PRESET_CONSTANT)) {
1998+
define(ConfigManager::PRESET_CONSTANT, ConfigLexiconPreset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)));
1999+
}
19962000
$entries = $aliases = [];
19972001
$bootstrapCoordinator = \OCP\Server::get(Coordinator::class);
19982002
$configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId);

lib/unstable/Config/Lexicon/ConfigLexiconEntry.php

Lines changed: 24 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,34 @@ 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+
*/
64+
public function preset(ConfigLexiconPreset|array $preset, string|int|float|bool|array $defaultRaw): self {
65+
if (!is_array($preset)) {
66+
$preset = [$preset];
67+
}
68+
69+
foreach($preset as $entry) {
70+
if ($entry === constant(ConfigManager::PRESET_CONSTANT)) {
71+
$this->defaultRaw = $defaultRaw;
72+
break;
73+
}
74+
}
75+
76+
return $this;
77+
}
78+
5679
/**
5780
* returns the config key
5881
*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

0 commit comments

Comments
 (0)