Skip to content

Commit 59b8c38

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

File tree

9 files changed

+177
-16
lines changed

9 files changed

+177
-16
lines changed

core/AppInfo/ConfigLexicon.php

Lines changed: 16 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,20 @@ 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: [
47+
'definition' => 'Allow users to set custom share link tokens',
48+
'note' => 'Shares with custom tokens will continue to be accessible after this setting has been disabled',
49+
'warning' => 'Shares with guessable tokens may be accessed easily'
50+
],
51+
lazy: true,
52+
),
3753
];
3854
}
3955

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ 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+
if (($keyDetails['warning'] ?? '') !== '') {
207+
$output->writeln('<error>Warning:</error> ' . $keyDetails['warning']);
208+
}
209+
202210
} else {
203211
$output->writeln('<info>Config value were not updated</info>');
204212
}

lib/private/AppConfig.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,50 @@ 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+
'warning' => $lexiconEntry->getWarning()
1135+
]);
1136+
}
1137+
1138+
return array_filter($details);
1139+
}
1140+
10971141
/**
10981142
* @param string $type
10991143
*
@@ -1631,6 +1675,7 @@ private function matchAndApplyLexiconDefinition(
16311675
?bool &$lazy = null,
16321676
int &$type = self::VALUE_MIXED,
16331677
?string &$default = null,
1678+
?Entry &$lexiconEntry = null,
16341679
): bool {
16351680
if (in_array($key,
16361681
[
@@ -1655,27 +1700,27 @@ private function matchAndApplyLexiconDefinition(
16551700
return true;
16561701
}
16571702

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

1662-
$appConfigValueType = $configValue->getValueType()->toAppConfigFlag();
1707+
$appConfigValueType = $lexiconEntry->getValueType()->toAppConfigFlag();
16631708
if ($type === self::VALUE_MIXED) {
16641709
$type = $appConfigValueType; // we overwrite if value was requested as mixed
16651710
} elseif ($appConfigValueType !== $type) {
16661711
throw new AppConfigTypeConflictException('The app config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon');
16671712
}
16681713

1669-
$lazy = $configValue->isLazy();
1714+
$lazy = $lexiconEntry->isLazy();
16701715
// only look for default if needed, default from Lexicon got priority
16711716
if ($default !== null) {
1672-
$default = $configValue->getDefault($this->getLexiconPreset()) ?? $default;
1717+
$default = $lexiconEntry->getDefault($this->getLexiconPreset()) ?? $default;
16731718
}
16741719

1675-
if ($configValue->isFlagged(self::FLAG_SENSITIVE)) {
1720+
if ($lexiconEntry->isFlagged(self::FLAG_SENSITIVE)) {
16761721
$type |= self::VALUE_SENSITIVE;
16771722
}
1678-
if ($configValue->isDeprecated()) {
1723+
if ($lexiconEntry->isDeprecated()) {
16791724
$this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.');
16801725
}
16811726

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: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class Entry {
2222
/** @since 32.0.0 */
2323
public const RENAME_INVERT_BOOLEAN = 1;
2424

25-
private string $definition = '';
25+
private array|string $definition = '';
2626
private ?string $default = null;
2727

2828
/**
2929
* @param string $key config key, can only contain alphanumerical chars and -._
3030
* @param ValueType $type type of config value
31-
* @param string $definition optional description of config key available when using occ command
31+
* @param array|string $definition optional description of config key available when using occ command
3232
* @param bool $lazy set config value as lazy
3333
* @param int $flags set flags
3434
* @param string|null $rename previous config key to migrate config value from
@@ -42,7 +42,7 @@ public function __construct(
4242
private readonly string $key,
4343
private readonly ValueType $type,
4444
private null|string|int|float|bool|array|Closure $defaultRaw = null,
45-
string $definition = '',
45+
string|array $definition = '',
4646
private readonly bool $lazy = false,
4747
private readonly int $flags = 0,
4848
private readonly bool $deprecated = false,
@@ -184,7 +184,31 @@ public function convertToString(string|int|float|bool|array $entry): string {
184184
* @since 32.0.0
185185
*/
186186
public function getDefinition(): string {
187-
return $this->definition;
187+
if (is_string($this->definition)) {
188+
return $this->definition;
189+
}
190+
191+
return (string)($this->definition['definition'] ?? '');
192+
}
193+
194+
/**
195+
* returns eventual note
196+
*
197+
* @return string
198+
* @since 32.0.0
199+
*/
200+
public function getNote(): string {
201+
return (string)($this->definition['note'] ?? '');
202+
}
203+
204+
/**
205+
* returns eventual warning
206+
*
207+
* @return string
208+
* @since 32.0.0
209+
*/
210+
public function getWarning(): string {
211+
return (string)($this->definition['warning'] ?? '');
188212
}
189213

190214
/**

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)