Skip to content

Commit df7d451

Browse files
ArtificialOwlicewind1991
authored andcommitted
feat: implementing Lexicon and Preset
Signed-off-by: Maxence Lange <maxence@artificial-owl.com> # Conflicts: # composer.lock # lib/AppInfo/Application.php # lib/Config.php # lib/Controller/SettingsController.php # lib/GuestManager.php # tests/unit/ConfigTest.php # tests/unit/Controller/UsersControllerTest.php # tests/unit/GuestManagerTest.php
1 parent 3bf8801 commit df7d451

8 files changed

Lines changed: 189 additions & 85 deletions

File tree

lib/AppInfo/Application.php

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

1111
use OCA\Files\Event\LoadAdditionalScriptsEvent;
1212
use OCA\Guests\Capabilities;
13+
use OCA\Guests\ConfigLexicon;
1314
use OCA\Guests\GroupBackend;
1415
use OCA\Guests\Listener\BeforeTemplateRenderedListener;
1516
use OCA\Guests\Listener\BeforeUserManagementRenderedListener;
@@ -57,6 +58,7 @@ public function register(IRegistrationContext $context): void {
5758
$context->registerEventListener(UserFirstTimeLoggedInEvent::class, UserFirstTimeLoggedInListener::class);
5859

5960
$context->registerNotifierService(Notifier::class);
61+
$context->registerConfigLexicon(ConfigLexicon::class);
6062
}
6163

6264
#[\Override]

lib/Config.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
use OCP\AppFramework\Services\IAppConfig;
1313
use OCP\Group\ISubAdmin;
14+
use OCP\IAppConfig as IGlobalAppConfig;
1415
use OCP\IConfig;
1516
use OCP\IGroupManager;
1617
use OCP\IUserSession;
1718

1819
class Config {
1920
public function __construct(
2021
private readonly IConfig $config,
22+
private readonly IGlobalAppConfig $globalAppConfig,
2123
private readonly IAppConfig $appConfig,
2224
private readonly ISubAdmin $subAdmin,
2325
private readonly IUserSession $userSession,
@@ -26,11 +28,11 @@ public function __construct(
2628
}
2729

2830
public function allowExternalStorage(): bool {
29-
return $this->appConfig->getAppValueBool('allow_external_storage', false);
31+
return $this->appConfig->getAppValueBool(ConfigLexicon::EXTERNAL_STORAGE_ENABLED);
3032
}
3133

32-
public function setAllowExternalStorage(string|bool $allow): void {
33-
$this->appConfig->setAppValueBool('allow_external_storage', $allow === true || $allow === 'true') ;
34+
public function setAllowExternalStorage(bool $allow): void {
35+
$this->appConfig->setAppValueBool(ConfigLexicon::EXTERNAL_STORAGE_ENABLED, $allow);
3436
}
3537

3638
public function useHashedEmailAsUserID(): bool {
@@ -42,43 +44,38 @@ public function setUseHashedEmailAsUserID(bool $useHash): void {
4244
}
4345

4446
public function hideOtherUsers(): bool {
45-
return $this->appConfig->getAppValueBool('hide_users', true);
47+
return $this->appConfig->getAppValueBool(ConfigLexicon::HIDE_OTHER_ACCOUNTS);
4648
}
4749

48-
public function setHideOtherUsers(string|bool $hide): void {
49-
$this->appConfig->setAppValueBool('hide_users', $hide === true || $hide === 'true') ;
50+
public function setHideOtherUsers(bool $hide): void {
51+
$this->appConfig->setAppValueBool(ConfigLexicon::HIDE_OTHER_ACCOUNTS, $hide);
5052
}
5153

5254
public function getHome(string $uid): string {
5355
return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid;
5456
}
5557

5658
public function useWhitelist(): bool {
57-
return $this->appConfig->getAppValueBool('usewhitelist', true);
59+
return $this->appConfig->getAppValueBool(ConfigLexicon::WHITE_LIST_ENABLED);
5860
}
5961

60-
public function setUseWhitelist(string|bool $use): void {
61-
$this->appConfig->setAppValueBool('usewhitelist', $use === true || $use === 'true') ;
62+
public function setUseWhitelist(bool $use): void {
63+
$this->appConfig->setAppValueBool(ConfigLexicon::WHITE_LIST_ENABLED, $use);
6264
}
6365

6466
/**
6567
* @return list<string>
6668
*/
6769
public function getAppWhitelist(): array {
68-
$whitelist = $this->appConfig->getAppValueString('whitelist', AppWhitelist::DEFAULT_WHITELIST);
69-
return explode(',', $whitelist);
70+
return explode(',', $this->appConfig->getAppValueString(ConfigLexicon::WHITE_LIST));
7071
}
7172

72-
public function setAppWhitelist(array|string $whitelist): void {
73-
if (is_array($whitelist)) {
74-
$whitelist = implode(',', $whitelist);
75-
}
76-
77-
$this->appConfig->setAppValueString('whitelist', $whitelist);
73+
public function setAppWhitelist(array $whitelist): void {
74+
$this->appConfig->setAppValueString(ConfigLexicon::WHITE_LIST, implode(',', $whitelist));
7875
}
7976

8077
public function isSharingRestrictedToGroup(): bool {
81-
return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
78+
return $this->globalAppConfig->getValueBool('core', 'shareapi_only_share_with_group_members');
8279
}
8380

8481
public function canCreateGuests(): bool {
@@ -111,7 +108,7 @@ public function canCreateGuests(): bool {
111108
* @return list<string>
112109
*/
113110
public function getCreateRestrictedToGroup(): array {
114-
$groups = $this->appConfig->getAppValueArray('create_restricted_to_group', []);
111+
$groups = $this->appConfig->getAppValueArray(ConfigLexicon::GROUP_LIMITATION);
115112
// If empty, it means there is no restriction
116113
if (empty($groups)) {
117114
return [];
@@ -120,7 +117,7 @@ public function getCreateRestrictedToGroup(): array {
120117
// It does not matter at this point if the admin
121118
// group is in the list or not. We are checking it
122119
// anyway in the canCreateGuests method.
123-
return array_values(array_unique($this->appConfig->getAppValueArray('create_restricted_to_group', [])));
120+
return array_values(array_unique($this->appConfig->getAppValueArray(ConfigLexicon::GROUP_LIMITATION)));
124121
}
125122

126123
/**

lib/ConfigLexicon.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Guests;
10+
11+
use OCP\Config\Lexicon\Entry;
12+
use OCP\Config\Lexicon\ILexicon;
13+
use OCP\Config\Lexicon\Preset;
14+
use OCP\Config\Lexicon\Strictness;
15+
use OCP\Config\ValueType;
16+
17+
/**
18+
* Config Lexicon for guests app.
19+
*
20+
* Please Add & Manage your Config Keys in that file and keep the Lexicon up to date!
21+
*
22+
* {@see ILexicon}
23+
*/
24+
class ConfigLexicon implements ILexicon {
25+
public const HIDE_OTHER_ACCOUNTS = 'hide_users';
26+
public const EXTERNAL_STORAGE_ENABLED = 'allow_external_storage';
27+
public const WHITE_LIST_ENABLED = 'usewhitelist';
28+
public const WHITE_LIST = 'whitelist';
29+
public const GROUP_LIMITATION = 'create_restricted_to_group';
30+
public const GUEST_DISK_QUOTA = 'guest_quota';
31+
public const USER_CREATED_BY = 'created_by';
32+
33+
public function getStrictness(): Strictness {
34+
return Strictness::NOTICE;
35+
}
36+
37+
public function getAppConfigs(): array {
38+
return [
39+
new Entry(self::EXTERNAL_STORAGE_ENABLED, ValueType::BOOL, false, 'permit guests to access to external storage'),
40+
new Entry(self::HIDE_OTHER_ACCOUNTS, ValueType::BOOL, true, 'hide other accounts to guests'),
41+
new Entry(self::WHITE_LIST_ENABLED, ValueType::BOOL, true, 'enable a white listing of limited apps made available to guests'),
42+
new Entry(self::WHITE_LIST, ValueType::STRING, AppWhitelist::DEFAULT_WHITELIST, 'list of white listed apps available to guests'),
43+
new Entry(self::GUEST_DISK_QUOTA, ValueType::STRING, defaultRaw: fn (Preset $p): string => match ($p) {
44+
Preset::PRIVATE, Preset::FAMILY => '1 GB',
45+
Preset::SMALL, Preset::MEDIUM, Preset::LARGE => '10 GB',
46+
default => '0 B',
47+
}, definition: 'set default disk quota assigned to guest account at its creation'),
48+
new Entry(self::GROUP_LIMITATION, ValueType::ARRAY, []),
49+
];
50+
}
51+
52+
public function getUserConfigs(): array {
53+
return [
54+
new Entry(self::USER_CREATED_BY, ValueType::STRING, null, 'user that generated this guest account'),
55+
];
56+
}
57+
}

lib/Controller/SettingsController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use OCA\Guests\AppInfo\Application;
1414
use OCA\Guests\AppWhitelist;
1515
use OCA\Guests\Config;
16+
use OCA\Guests\ConfigLexicon;
1617
use OCP\AppFramework\Controller;
1718
use OCP\AppFramework\Http\DataResponse;
19+
use OCP\AppFramework\Services\IAppConfig;
1820
use OCP\IRequest;
1921

2022
/**
@@ -24,10 +26,10 @@
2426
* @package OCA\Guests\Controller
2527
*/
2628
class SettingsController extends Controller {
27-
2829
public function __construct(
2930
IRequest $request,
3031
private readonly Config $config,
32+
private readonly IAppConfig $appConfig,
3133
private readonly AppWhitelist $appWhitelist,
3234
) {
3335
parent::__construct(Application::APP_ID, $request);
@@ -98,10 +100,9 @@ public function getWhitelist(): DataResponse {
98100
* @return DataResponse with the reset whitelist
99101
*/
100102
public function resetWhitelist(): DataResponse {
101-
$this->config->setAppWhitelist(AppWhitelist::DEFAULT_WHITELIST);
102-
103+
$this->appConfig->deleteAppValue(ConfigLexicon::WHITE_LIST);
103104
return new DataResponse([
104-
'whitelist' => explode(',', AppWhitelist::DEFAULT_WHITELIST),
105+
'whitelist' => $this->config->getAppWhitelist(),
105106
]);
106107
}
107108
}

lib/GuestManager.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
namespace OCA\Guests;
1111

12+
use OCA\Guests\AppInfo\Application;
13+
use OCP\AppFramework\Services\IAppConfig;
14+
use OCP\Config\IUserConfig;
15+
use OCP\Config\ValueType;
1216
use OCP\DB\QueryBuilder\IQueryBuilder;
1317
use OCP\EventDispatcher\IEventDispatcher;
1418
use OCP\IConfig;
@@ -25,6 +29,8 @@
2529
class GuestManager {
2630
public function __construct(
2731
private readonly IConfig $config,
32+
private readonly IAppConfig $appConfig,
33+
private readonly IUserConfig $userConfig,
2834
private readonly UserBackend $userBackend,
2935
private readonly ISecureRandom $secureRandom,
3036
private readonly ICrypto $crypto,
@@ -72,7 +78,7 @@ public function createGuest(?IUser $createdBy, string $userId, string $email, st
7278
$this->userBackend->setInitialEmail($userId, $email);
7379
$user->setSystemEMailAddress($email);
7480
if ($createdBy instanceof IUser) {
75-
$this->config->setUserValue($userId, 'guests', 'created_by', $createdBy->getUID());
81+
$this->userConfig->setValueString($userId, Application::APP_ID, ConfigLexicon::USER_CREATED_BY, $createdBy->getUID());
7682
}
7783

7884
if ($displayName !== '') {
@@ -104,7 +110,7 @@ public function createGuest(?IUser $createdBy, string $userId, string $email, st
104110
);
105111
}
106112

107-
$user->setQuota('0 B');
113+
$user->setQuota($this->appConfig->getAppValueString(ConfigLexicon::GUEST_DISK_QUOTA));
108114

109115
return $user;
110116
}
@@ -129,7 +135,8 @@ public function getGuestsInfo(): array {
129135
$guestsInfo = $this->userBackend->getAllGuestAccounts();
130136
$guests = array_keys($guestsInfo);
131137
$shareCounts = $this->getShareCountForUsers($guests);
132-
$createdBy = $this->config->getUserValueForUsers('guests', 'created_by', $guests);
138+
$createdBy = $this->userConfig->getValuesByUsers(Application::APP_ID, ConfigLexicon::USER_CREATED_BY, ValueType::STRING, $guests);
139+
133140
return array_map(function (string $uid) use ($createdBy, $guestsInfo, $shareCounts): array {
134141
$allSharesCount = count(array_merge(
135142
$this->shareManager->getSharedWith($uid, IShare::TYPE_USER, null, -1, 0),

0 commit comments

Comments
 (0)