Skip to content

Commit 0c02fa0

Browse files
committed
feat(preset): add lexicon entry for custom share tokens
Signed-off-by: Maxence Lange <[email protected]>
1 parent 3b4a268 commit 0c02fa0

File tree

9 files changed

+160
-14
lines changed

9 files changed

+160
-14
lines changed

core/AppInfo/ConfigLexicon.php

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

1111
use OCP\Config\Lexicon\Entry;
1212
use OCP\Config\Lexicon\ILexicon;
13+
use OCP\Config\Lexicon\Preset;
1314
use OCP\Config\Lexicon\Strictness;
1415
use OCP\Config\ValueType;
1516

@@ -20,6 +21,7 @@
2021
*/
2122
class ConfigLexicon implements ILexicon {
2223
public const SHAREAPI_ALLOW_FEDERATION_ON_PUBLIC_SHARES = 'shareapi_allow_federation_on_public_shares';
24+
public const SHARE_CUSTOM_TOKEN = 'shareapi_allow_custom_tokens';
2325

2426
public function getStrictness(): Strictness {
2527
return Strictness::IGNORE;
@@ -34,6 +36,17 @@ public function getAppConfigs(): array {
3436
definition: 'adds share permission to public shares to allow adding them to your Nextcloud (federation)',
3537
lazy: true,
3638
),
39+
new Entry(
40+
key: self::SHARE_CUSTOM_TOKEN,
41+
type: ValueType::BOOL,
42+
defaultRaw: fn (Preset $p): bool => match ($p) {
43+
Preset::FAMILY, Preset::PRIVATE => true,
44+
default => false,
45+
},
46+
definition: 'Allow users to set custom share link tokens',
47+
lazy: true,
48+
note: 'Shares with guessable tokens may be accessed easily. Shares with custom tokens will continue to be accessible after this setting has been disabled.',
49+
),
3750
];
3851
}
3952

core/Command/Base.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ protected function valueToString($value, bool $returnNull = true): ?string {
170170
return 'true';
171171
} elseif ($value === null) {
172172
return $returnNull ? null : 'null';
173+
} if ($value instanceof \UnitEnum) {
174+
return $value->value;
173175
} else {
174176
return $value;
175177
}

core/Command/Config/App/GetConfig.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ protected function configure() {
3737
InputOption::VALUE_NONE,
3838
'returns complete details about the app config value'
3939
)
40+
->addOption(
41+
'--key-details',
42+
null,
43+
InputOption::VALUE_NONE,
44+
'returns complete details about the app config key'
45+
)
4046
->addOption(
4147
'default-value',
4248
null,
@@ -66,6 +72,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6672
return 0;
6773
}
6874

75+
if ($input->getOption('key-details')) {
76+
$details = $this->appConfig->getKeyDetails($appName, $configName);
77+
$this->writeArrayInOutputFormat($input, $output, $details);
78+
return 0;
79+
}
80+
6981
try {
7082
$configValue = $this->appConfig->getDetails($appName, $configName)['value'];
7183
} catch (AppConfigUnknownKeyException $e) {

core/Command/Config/App/SetConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
199199
$current['lazy'] ? 'lazy cache' : 'fast cache'
200200
)
201201
);
202+
$keyDetails = $this->appConfig->getKeyDetails($appName, $configName);
203+
if (($keyDetails['note'] ?? '') !== '') {
204+
$output->writeln('<comment>Note:</comment> ' . $keyDetails['note']);
205+
}
206+
202207
} else {
203208
$output->writeln('<info>Config value were not updated</info>');
204209
}

lib/private/AppConfig.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,49 @@ public function getDetails(string $app, string $key): array {
10941094
];
10951095
}
10961096

1097+
/**
1098+
* @inheritDoc
1099+
*
1100+
* @param string $app id of the app
1101+
* @param string $key config key
1102+
*
1103+
* @return array
1104+
* @since 32.0.0
1105+
*/
1106+
public function getKeyDetails(string $app, string $key): array {
1107+
$this->assertParams($app, $key);
1108+
try {
1109+
$details = $this->getDetails($app, $key);
1110+
} catch (AppConfigUnknownKeyException $e) {
1111+
$details = [
1112+
'app' => $app,
1113+
'key' => $key
1114+
];
1115+
}
1116+
1117+
/** @var Entry $lexiconEntry */
1118+
try {
1119+
$lazy = false;
1120+
$this->matchAndApplyLexiconDefinition($app, $key, $lazy, lexiconEntry: $lexiconEntry);
1121+
} catch (AppConfigTypeConflictException|AppConfigUnknownKeyException) {
1122+
// can be ignored
1123+
}
1124+
1125+
if ($lexiconEntry !== null) {
1126+
$details = array_merge($details, [
1127+
'lazy' => $lexiconEntry->isLazy(),
1128+
'valueType' => $lexiconEntry->getValueType(),
1129+
'valueTypeName' => $lexiconEntry->getValueType()->name,
1130+
'sensitive' => $lexiconEntry->isFlagged(self::FLAG_SENSITIVE),
1131+
'default' => $lexiconEntry->getDefault($this->getLexiconPreset()),
1132+
'definition' => $lexiconEntry->getDefinition(),
1133+
'note' => $lexiconEntry->getNote(),
1134+
]);
1135+
}
1136+
1137+
return array_filter($details);
1138+
}
1139+
10971140
/**
10981141
* @param string $type
10991142
*
@@ -1631,6 +1674,7 @@ private function matchAndApplyLexiconDefinition(
16311674
?bool &$lazy = null,
16321675
int &$type = self::VALUE_MIXED,
16331676
?string &$default = null,
1677+
?Entry &$lexiconEntry = null,
16341678
): bool {
16351679
if (in_array($key,
16361680
[
@@ -1655,27 +1699,27 @@ private function matchAndApplyLexiconDefinition(
16551699
return true;
16561700
}
16571701

1658-
/** @var Entry $configValue */
1659-
$configValue = $configDetails['entries'][$key];
1702+
/** @var Entry $lexiconEntry */
1703+
$lexiconEntry = $configDetails['entries'][$key];
16601704
$type &= ~self::VALUE_SENSITIVE;
16611705

1662-
$appConfigValueType = $configValue->getValueType()->toAppConfigFlag();
1706+
$appConfigValueType = $lexiconEntry->getValueType()->toAppConfigFlag();
16631707
if ($type === self::VALUE_MIXED) {
16641708
$type = $appConfigValueType; // we overwrite if value was requested as mixed
16651709
} elseif ($appConfigValueType !== $type) {
16661710
throw new AppConfigTypeConflictException('The app config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon');
16671711
}
16681712

1669-
$lazy = $configValue->isLazy();
1713+
$lazy = $lexiconEntry->isLazy();
16701714
// only look for default if needed, default from Lexicon got priority
16711715
if ($default !== null) {
1672-
$default = $configValue->getDefault($this->getLexiconPreset()) ?? $default;
1716+
$default = $lexiconEntry->getDefault($this->getLexiconPreset()) ?? $default;
16731717
}
16741718

1675-
if ($configValue->isFlagged(self::FLAG_SENSITIVE)) {
1719+
if ($lexiconEntry->isFlagged(self::FLAG_SENSITIVE)) {
16761720
$type |= self::VALUE_SENSITIVE;
16771721
}
1678-
if ($configValue->isDeprecated()) {
1722+
if ($lexiconEntry->isDeprecated()) {
16791723
$this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.');
16801724
}
16811725

lib/private/Share20/Manager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace OC\Share20;
99

10+
use OC\Core\AppInfo\ConfigLexicon;
1011
use OC\Files\Mount\MoveableMount;
1112
use OC\KnownUser\KnownUserService;
1213
use OC\Share20\Exception\ProviderException;
@@ -1939,7 +1940,7 @@ public function ignoreSecondDisplayName(): bool {
19391940
}
19401941

19411942
public function allowCustomTokens(): bool {
1942-
return $this->appConfig->getValueBool('core', 'shareapi_allow_custom_tokens', false);
1943+
return $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_CUSTOM_TOKEN);
19431944
}
19441945

19451946
public function allowViewWithoutDownload(): bool {

lib/public/Config/Lexicon/Entry.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ class Entry {
2323
public const RENAME_INVERT_BOOLEAN = 1;
2424

2525
private string $definition = '';
26+
private string $note = '';
2627
private ?string $default = null;
2728

2829
/**
29-
* @param string $key config key, can only contain alphanumerical chars and -._
30+
* @param string $key config key; can only contain alphanumerical chars and underscore "_"
3031
* @param ValueType $type type of config value
32+
* @param string|int|float|bool|array|Closure|null $defaultRaw default value to be used in case none known
3133
* @param string $definition optional description of config key available when using occ command
3234
* @param bool $lazy set config value as lazy
3335
* @param int $flags set flags
34-
* @param string|null $rename previous config key to migrate config value from
3536
* @param bool $deprecated set config key as deprecated
37+
* @param string|null $rename source in case of a rename of a config key.
38+
* @param int $options additional bitflag options {@see self::RENAME_INVERT_BOOLEAN}
39+
* @param string $note additional note and warning related to the use of the config key.
3640
*
3741
* @since 32.0.0
3842
* @psalm-suppress PossiblyInvalidCast
@@ -48,6 +52,7 @@ public function __construct(
4852
private readonly bool $deprecated = false,
4953
private readonly ?string $rename = null,
5054
private readonly int $options = 0,
55+
string $note = '',
5156
) {
5257
// key can only contain alphanumeric chars and underscore "_"
5358
if (preg_match('/[^[:alnum:]_]/', $key)) {
@@ -57,6 +62,7 @@ public function __construct(
5762
/** @psalm-suppress UndefinedClass */
5863
if (\OC::$CLI) { // only store definition if ran from CLI
5964
$this->definition = $definition;
65+
$this->note = $note;
6066
}
6167
}
6268

@@ -187,6 +193,16 @@ public function getDefinition(): string {
187193
return $this->definition;
188194
}
189195

196+
/**
197+
* returns eventual note
198+
*
199+
* @return string
200+
* @since 32.0.0
201+
*/
202+
public function getNote(): string {
203+
return $this->note;
204+
}
205+
190206
/**
191207
* returns if config key is set as lazy
192208
*

lib/public/IAppConfig.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,34 @@ public function updateLazy(string $app, string $key, bool $lazy): bool;
449449
*/
450450
public function getDetails(string $app, string $key): array;
451451

452+
/**
453+
* returns an array containing details about a config key.
454+
* key/value pair are available only if it exists.
455+
*
456+
* ```
457+
* [
458+
* "app" => "myapp",
459+
* "key" => "mykey",
460+
* "value" => "current_value",
461+
* "default" => "default_if_available",
462+
* "definition" => "this is what it does",
463+
* "note" => "enabling this is not compatible with that",
464+
* "warning" => "enabling this can do this other bad thing",
465+
* "lazy" => false,
466+
* "type" => 4,
467+
* "typeString" => "string",
468+
* 'sensitive' => false
469+
* ]
470+
* ```
471+
*
472+
* @param string $app id of the app
473+
* @param string $key config key
474+
*
475+
* @return array
476+
* @since 32.0.0
477+
*/
478+
public function getKeyDetails(string $app, string $key): array;
479+
452480
/**
453481
* Convert string like 'string', 'integer', 'float', 'bool' or 'array' to
454482
* to bitflag {@see VALUE_STRING}, {@see VALUE_INT}, {@see VALUE_FLOAT},

lib/unstable/Config/Lexicon/ConfigLexiconEntry.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class ConfigLexiconEntry {
2828
*/
2929
public const RENAME_INVERT_BOOLEAN = 1;
3030

31-
private string $definition = '';
31+
private array|string $definition = '';
3232
private ?string $default = null;
3333

3434
/**
3535
* @param string $key config key, can only contain alphanumerical chars and -._
3636
* @param ValueType $type type of config value
37-
* @param string $definition optional description of config key available when using occ command
37+
* @param array|string $definition optional description of config key available when using occ command
3838
* @param bool $lazy set config value as lazy
3939
* @param int $flags set flags
4040
* @param string|null $rename previous config key to migrate config value from
@@ -50,7 +50,7 @@ public function __construct(
5050
private readonly string $key,
5151
private readonly ValueType $type,
5252
private null|string|int|float|bool|array|Closure $defaultRaw = null,
53-
string $definition = '',
53+
string|array $definition = '',
5454
private readonly bool $lazy = false,
5555
private readonly int $flags = 0,
5656
private readonly bool $deprecated = false,
@@ -218,9 +218,34 @@ public function convertToString(string|int|float|bool|array $entry): string {
218218
* @see \OCP\Config\Lexicon\Entry
219219
*/
220220
public function getDefinition(): string {
221-
return $this->definition;
221+
if (is_string($this->definition)) {
222+
return $this->definition;
223+
}
224+
225+
return (string)($this->definition['definition'] ?? '');
226+
}
227+
228+
/**
229+
* returns eventual note
230+
*
231+
* @return string
232+
* @experimental 32.0.0
233+
*/
234+
public function getNote(): string {
235+
return (string)($this->definition['note'] ?? '');
222236
}
223237

238+
/**
239+
* returns eventual warning
240+
*
241+
* @return string
242+
* @experimental 32.0.0
243+
*/
244+
public function getWarning(): string {
245+
return (string)($this->definition['warning'] ?? '');
246+
}
247+
248+
224249
/**
225250
* returns if config key is set as lazy
226251
*

0 commit comments

Comments
 (0)