Skip to content

Commit 9517c6d

Browse files
committed
feat: implementing Lexicon and Preset
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 348c633 commit 9517c6d

9 files changed

Lines changed: 172 additions & 91 deletions

File tree

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/AppInfo/Application.php

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

1010
use OCA\Files\Event\LoadAdditionalScriptsEvent;
1111
use OCA\Guests\Capabilities;
12+
use OCA\Guests\ConfigLexicon;
1213
use OCA\Guests\GroupBackend;
1314
use OCA\Guests\Hooks;
1415
use OCA\Guests\Listener\BeforeUserManagementRenderedListener;
@@ -47,6 +48,7 @@ public function register(IRegistrationContext $context): void {
4748
$context->registerEventListener(ShareCreatedEvent::class, ShareAutoAcceptListener::class);
4849
$context->registerEventListener(BeforeTemplateRenderedEvent::class, TalkIntegrationListener::class);
4950
$context->registerEventListener(BeforeUserManagementRenderedEvent::class, BeforeUserManagementRenderedListener::class);
51+
$context->registerConfigLexicon(ConfigLexicon::class);
5052
}
5153

5254
public function boot(IBootContext $context): void {

lib/Config.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
use OCP\AppFramework\Services\IAppConfig;
1212
use OCP\Group\ISubAdmin;
13+
use OCP\IAppConfig as IGlobalAppConfig;
1314
use OCP\IConfig;
1415
use OCP\IGroupManager;
1516
use OCP\IUserSession;
1617

1718
class Config {
1819
public function __construct(
1920
private IConfig $config,
21+
private IGlobalAppConfig $globalAppConfig,
2022
private IAppConfig $appConfig,
2123
private ISubAdmin $subAdmin,
2224
private IUserSession $userSession,
@@ -25,62 +27,46 @@ public function __construct(
2527
}
2628

2729
public function allowExternalStorage(): bool {
28-
return $this->appConfig->getAppValueBool('allow_external_storage', false);
30+
return $this->appConfig->getAppValueBool(ConfigLexicon::EXTERNAL_STORAGE_ENABLED);
2931
}
3032

31-
/**
32-
* @param string|bool $allow
33-
*/
34-
public function setAllowExternalStorage($allow) {
35-
$this->appConfig->setAppValueBool('allow_external_storage', $allow === true || $allow === 'true') ;
33+
public function setAllowExternalStorage(bool $allow): void {
34+
$this->appConfig->setAppValueBool(ConfigLexicon::EXTERNAL_STORAGE_ENABLED, $allow);
3635
}
3736

3837
public function hideOtherUsers(): bool {
39-
return $this->appConfig->getAppValueBool('hide_users', true);
38+
return $this->appConfig->getAppValueBool(ConfigLexicon::HIDE_OTHER_ACCOUNTS);
4039
}
4140

42-
/**
43-
* @param string|bool $hide
44-
*/
45-
public function setHideOtherUsers($hide): void {
46-
$this->appConfig->setAppValueBool('hide_users', $hide === true || $hide === 'true') ;
41+
public function setHideOtherUsers(bool $hide): void {
42+
$this->appConfig->setAppValueBool(ConfigLexicon::HIDE_OTHER_ACCOUNTS, $hide);
4743
}
4844

4945
public function getHome(string $uid): string {
5046
return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid;
5147
}
5248

5349
public function useWhitelist(): bool {
54-
return $this->appConfig->getAppValueBool('usewhitelist', true);
50+
return $this->appConfig->getAppValueBool(ConfigLexicon::WHITE_LIST_ENABLED);
5551
}
5652

57-
/**
58-
* @param string|bool $use
59-
*/
60-
public function setUseWhitelist($use) {
61-
$this->appConfig->setAppValueBool('usewhitelist', $use === true || $use === 'true') ;
53+
public function setUseWhitelist(bool $use): void {
54+
$this->appConfig->setAppValueBool(ConfigLexicon::WHITE_LIST_ENABLED, $use);
6255
}
6356

6457
/**
6558
* @return string[]
6659
*/
6760
public function getAppWhitelist(): array {
68-
$whitelist = $this->appConfig->getAppValueString('whitelist', AppWhitelist::DEFAULT_WHITELIST);
69-
return explode(',', $whitelist);
61+
return explode(',', $this->appConfig->getAppValueString(ConfigLexicon::WHITE_LIST));
7062
}
7163

72-
/**
73-
* @param array|string $whitelist
74-
*/
75-
public function setAppWhitelist($whitelist): void {
76-
if (is_array($whitelist)) {
77-
$whitelist = implode(',', $whitelist);
78-
}
79-
$this->appConfig->setAppValueString('whitelist', $whitelist);
64+
public function setAppWhitelist(array $whitelist): void {
65+
$this->appConfig->setAppValueString(ConfigLexicon::WHITE_LIST, implode(',', $whitelist));
8066
}
8167

8268
public function isSharingRestrictedToGroup(): bool {
83-
return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
69+
return $this->globalAppConfig->getValueBool('core', 'shareapi_only_share_with_group_members');
8470
}
8571

8672
public function canCreateGuests(): bool {
@@ -114,7 +100,7 @@ public function canCreateGuests(): bool {
114100
* @return string[]
115101
*/
116102
public function getCreateRestrictedToGroup(): array {
117-
$groups = $this->appConfig->getAppValueArray('create_restricted_to_group', []);
103+
$groups = $this->appConfig->getAppValueArray(ConfigLexicon::GROUP_LIMITATION);
118104
// If empty, it means there is no restriction
119105
if (empty($groups)) {
120106
return [];
@@ -123,7 +109,7 @@ public function getCreateRestrictedToGroup(): array {
123109
// It does not matter at this point if the admin
124110
// group is in the list or not. We are checking it
125111
// anyway in the canCreateGuests method.
126-
return array_values(array_unique($this->appConfig->getAppValueArray('create_restricted_to_group', [])));
112+
return array_values(array_unique($this->appConfig->getAppValueArray(ConfigLexicon::GROUP_LIMITATION)));
127113
}
128114

129115
/**

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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use OCA\Guests\AppInfo\Application;
1212
use OCA\Guests\AppWhitelist;
1313
use OCA\Guests\Config;
14+
use OCA\Guests\ConfigLexicon;
1415
use OCP\AppFramework\Controller;
1516
use OCP\AppFramework\Http\DataResponse;
17+
use OCP\AppFramework\Services\IAppConfig;
1618
use OCP\IRequest;
1719

1820
/**
@@ -22,11 +24,11 @@
2224
* @package OCA\Guests\Controller
2325
*/
2426
class SettingsController extends Controller {
25-
2627
public function __construct(
2728
IRequest $request,
28-
private Config $config,
29-
private AppWhitelist $appWhitelist,
29+
private readonly Config $config,
30+
private readonly IAppConfig $appConfig,
31+
private readonly AppWhitelist $appWhitelist,
3032
) {
3133
parent::__construct(Application::APP_ID, $request);
3234
}
@@ -94,9 +96,9 @@ public function getWhitelist(): DataResponse {
9496
* @return DataResponse with the reset whitelist
9597
*/
9698
public function resetWhitelist(): DataResponse {
97-
$this->config->setAppWhitelist(AppWhitelist::DEFAULT_WHITELIST);
99+
$this->appConfig->deleteAppValue(ConfigLexicon::WHITE_LIST);
98100
return new DataResponse([
99-
'whitelist' => explode(',', AppWhitelist::DEFAULT_WHITELIST),
101+
'whitelist' => $this->config->getAppWhitelist(),
100102
]);
101103
}
102104
}

lib/GuestManager.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
namespace OCA\Guests;
99

10+
use OCA\Guests\AppInfo\Application;
11+
use OCP\AppFramework\Services\IAppConfig;
12+
use OCP\Config\IUserConfig;
13+
use OCP\Config\ValueType;
1014
use OCP\DB\QueryBuilder\IQueryBuilder;
1115
use OCP\EventDispatcher\IEventDispatcher;
1216
use OCP\IConfig;
@@ -23,6 +27,8 @@
2327
class GuestManager {
2428
public function __construct(
2529
private IConfig $config,
30+
private readonly IAppConfig $appConfig,
31+
private readonly IUserConfig $userConfig,
2632
private UserBackend $userBackend,
2733
private ISecureRandom $secureRandom,
2834
private ICrypto $crypto,
@@ -69,7 +75,7 @@ public function createGuest(?IUser $createdBy, string $userId, string $email, st
6975

7076
$user->setSystemEMailAddress($email);
7177
if ($createdBy) {
72-
$this->config->setUserValue($userId, 'guests', 'created_by', $createdBy->getUID());
78+
$this->userConfig->setValueString($userId, Application::APP_ID, ConfigLexicon::USER_CREATED_BY, $createdBy->getUID());
7379
}
7480

7581
if ($displayName) {
@@ -101,7 +107,7 @@ public function createGuest(?IUser $createdBy, string $userId, string $email, st
101107
);
102108
}
103109

104-
$user->setQuota('0 B');
110+
$user->setQuota($this->appConfig->getAppValueString(ConfigLexicon::GUEST_DISK_QUOTA));
105111

106112
return $user;
107113
}
@@ -114,7 +120,8 @@ public function getGuestsInfo(): array {
114120
$displayNames = $this->userBackend->getDisplayNames();
115121
$guests = array_keys($displayNames);
116122
$shareCounts = $this->getShareCountForUsers($guests);
117-
$createdBy = $this->config->getUserValueForUsers('guests', 'created_by', $guests);
123+
$createdBy = $this->userConfig->getValuesByUsers(Application::APP_ID, ConfigLexicon::USER_CREATED_BY, ValueType::STRING, $guests);
124+
118125
return array_map(function ($uid) use ($createdBy, $displayNames, $shareCounts) {
119126
$allSharesCount = count(array_merge(
120127
$this->shareManager->getSharedWith($uid, IShare::TYPE_USER, null, -1, 0),
@@ -127,7 +134,7 @@ public function getGuestsInfo(): array {
127134
'email' => $uid,
128135
'display_name' => $displayNames[$uid] ?? $uid,
129136
'created_by' => $createdBy[$uid] ?? '',
130-
'share_count' => isset($shareCounts[$uid]) ? $shareCounts[$uid] : 0,
137+
'share_count' => $shareCounts[$uid] ?? 0,
131138
'share_count_with_circles' => $allSharesCount,
132139
];
133140
}, $guests);

0 commit comments

Comments
 (0)