Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Guests users can only access files shared to them and cannot create any files ou
<screenshot>https://raw.githubusercontent.com/nextcloud/guests/master/screenshots/settings.png</screenshot>
<screenshot>https://raw.githubusercontent.com/nextcloud/guests/master/screenshots/dropdown.png</screenshot>
<dependencies>
<nextcloud min-version="31" max-version="34" />
<nextcloud min-version="32" max-version="34" />
</dependencies>
<repair-steps>
<post-migration>
Expand Down
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Guests\Capabilities;
use OCA\Guests\ConfigLexicon;
use OCA\Guests\GroupBackend;
use OCA\Guests\Listener\BeforeTemplateRenderedListener;
use OCA\Guests\Listener\BeforeUserManagementRenderedListener;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserFirstTimeLoggedInEvent::class, UserFirstTimeLoggedInListener::class);

$context->registerNotifierService(Notifier::class);
$context->registerConfigLexicon(ConfigLexicon::class);
}

#[\Override]
Expand Down
37 changes: 17 additions & 20 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

use OCP\AppFramework\Services\IAppConfig;
use OCP\Group\ISubAdmin;
use OCP\IAppConfig as IGlobalAppConfig;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserSession;

class Config {
public function __construct(
private readonly IConfig $config,
private readonly IGlobalAppConfig $globalAppConfig,
private readonly IAppConfig $appConfig,
private readonly ISubAdmin $subAdmin,
private readonly IUserSession $userSession,
Expand All @@ -26,11 +28,11 @@ public function __construct(
}

public function allowExternalStorage(): bool {
return $this->appConfig->getAppValueBool('allow_external_storage', false);
return $this->appConfig->getAppValueBool(ConfigLexicon::EXTERNAL_STORAGE_ENABLED);
}

public function setAllowExternalStorage(string|bool $allow): void {
$this->appConfig->setAppValueBool('allow_external_storage', $allow === true || $allow === 'true') ;
public function setAllowExternalStorage(bool $allow): void {
$this->appConfig->setAppValueBool(ConfigLexicon::EXTERNAL_STORAGE_ENABLED, $allow);
}

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

public function hideOtherUsers(): bool {
return $this->appConfig->getAppValueBool('hide_users', true);
return $this->appConfig->getAppValueBool(ConfigLexicon::HIDE_OTHER_ACCOUNTS);
}

public function setHideOtherUsers(string|bool $hide): void {
$this->appConfig->setAppValueBool('hide_users', $hide === true || $hide === 'true') ;
public function setHideOtherUsers(bool $hide): void {
$this->appConfig->setAppValueBool(ConfigLexicon::HIDE_OTHER_ACCOUNTS, $hide);
}

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

public function useWhitelist(): bool {
return $this->appConfig->getAppValueBool('usewhitelist', true);
return $this->appConfig->getAppValueBool(ConfigLexicon::WHITE_LIST_ENABLED);
}

public function setUseWhitelist(string|bool $use): void {
$this->appConfig->setAppValueBool('usewhitelist', $use === true || $use === 'true') ;
public function setUseWhitelist(bool $use): void {
$this->appConfig->setAppValueBool(ConfigLexicon::WHITE_LIST_ENABLED, $use);
}

/**
* @return list<string>
*/
public function getAppWhitelist(): array {
$whitelist = $this->appConfig->getAppValueString('whitelist', AppWhitelist::DEFAULT_WHITELIST);
return explode(',', $whitelist);
return explode(',', $this->appConfig->getAppValueString(ConfigLexicon::WHITE_LIST));
}

public function setAppWhitelist(array|string $whitelist): void {
if (is_array($whitelist)) {
$whitelist = implode(',', $whitelist);
}

$this->appConfig->setAppValueString('whitelist', $whitelist);
public function setAppWhitelist(array $whitelist): void {
$this->appConfig->setAppValueString(ConfigLexicon::WHITE_LIST, implode(',', $whitelist));
}

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

public function canCreateGuests(): bool {
Expand Down Expand Up @@ -111,7 +108,7 @@ public function canCreateGuests(): bool {
* @return list<string>
*/
public function getCreateRestrictedToGroup(): array {
$groups = $this->appConfig->getAppValueArray('create_restricted_to_group', []);
$groups = $this->appConfig->getAppValueArray(ConfigLexicon::GROUP_LIMITATION);
// If empty, it means there is no restriction
if (empty($groups)) {
return [];
Expand All @@ -120,7 +117,7 @@ public function getCreateRestrictedToGroup(): array {
// It does not matter at this point if the admin
// group is in the list or not. We are checking it
// anyway in the canCreateGuests method.
return array_values(array_unique($this->appConfig->getAppValueArray('create_restricted_to_group', [])));
return array_values(array_unique($this->appConfig->getAppValueArray(ConfigLexicon::GROUP_LIMITATION)));
}

/**
Expand Down
60 changes: 60 additions & 0 deletions lib/ConfigLexicon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Guests;

use OCP\Config\Lexicon\Entry;
use OCP\Config\Lexicon\ILexicon;
use OCP\Config\Lexicon\Preset;
use OCP\Config\Lexicon\Strictness;
use OCP\Config\ValueType;

/**
* Config Lexicon for guests app.
*
* Please Add & Manage your Config Keys in that file and keep the Lexicon up to date!
*
* {@see ILexicon}
*/
class ConfigLexicon implements ILexicon {
public const HIDE_OTHER_ACCOUNTS = 'hide_users';
public const EXTERNAL_STORAGE_ENABLED = 'allow_external_storage';
public const WHITE_LIST_ENABLED = 'usewhitelist';
public const WHITE_LIST = 'whitelist';
public const GROUP_LIMITATION = 'create_restricted_to_group';
public const GUEST_DISK_QUOTA = 'guest_quota';
public const USER_CREATED_BY = 'created_by';

#[\Override]
public function getStrictness(): Strictness {
return Strictness::NOTICE;
}

#[\Override]
public function getAppConfigs(): array {
return [
new Entry(self::EXTERNAL_STORAGE_ENABLED, ValueType::BOOL, false, 'permit guests to access to external storage'),
new Entry(self::HIDE_OTHER_ACCOUNTS, ValueType::BOOL, true, 'hide other accounts to guests'),
new Entry(self::WHITE_LIST_ENABLED, ValueType::BOOL, true, 'enable a white listing of limited apps made available to guests'),
new Entry(self::WHITE_LIST, ValueType::STRING, AppWhitelist::DEFAULT_WHITELIST, 'list of white listed apps available to guests'),
new Entry(self::GUEST_DISK_QUOTA, ValueType::STRING, defaultRaw: fn (Preset $p): string => match ($p) {
Preset::PRIVATE, Preset::FAMILY => '1 GB',
Preset::SMALL, Preset::MEDIUM, Preset::LARGE => '10 GB',
default => '0 B',
}, definition: 'set default disk quota assigned to guest account at its creation'),
new Entry(self::GROUP_LIMITATION, ValueType::ARRAY, []),
];
}

#[\Override]
public function getUserConfigs(): array {
return [
new Entry(self::USER_CREATED_BY, ValueType::STRING, null, 'user that generated this guest account'),
];
}
}
9 changes: 5 additions & 4 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
use OCA\Guests\AppInfo\Application;
use OCA\Guests\AppWhitelist;
use OCA\Guests\Config;
use OCA\Guests\ConfigLexicon;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IRequest;

/**
Expand All @@ -24,10 +26,10 @@
* @package OCA\Guests\Controller
*/
class SettingsController extends Controller {

public function __construct(
IRequest $request,
private readonly Config $config,
private readonly IAppConfig $appConfig,
private readonly AppWhitelist $appWhitelist,
) {
parent::__construct(Application::APP_ID, $request);
Expand Down Expand Up @@ -98,10 +100,9 @@ public function getWhitelist(): DataResponse {
* @return DataResponse with the reset whitelist
*/
public function resetWhitelist(): DataResponse {
$this->config->setAppWhitelist(AppWhitelist::DEFAULT_WHITELIST);

$this->appConfig->deleteAppValue(ConfigLexicon::WHITE_LIST);
return new DataResponse([
'whitelist' => explode(',', AppWhitelist::DEFAULT_WHITELIST),
'whitelist' => $this->config->getAppWhitelist(),
]);
}
}
13 changes: 10 additions & 3 deletions lib/GuestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace OCA\Guests;

use OCA\Guests\AppInfo\Application;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Config\IUserConfig;
use OCP\Config\ValueType;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
Expand All @@ -25,6 +29,8 @@
class GuestManager {
public function __construct(
private readonly IConfig $config,
private readonly IAppConfig $appConfig,
private readonly IUserConfig $userConfig,
private readonly UserBackend $userBackend,
private readonly ISecureRandom $secureRandom,
private readonly ICrypto $crypto,
Expand Down Expand Up @@ -72,7 +78,7 @@ public function createGuest(?IUser $createdBy, string $userId, string $email, st
$this->userBackend->setInitialEmail($userId, $email);
$user->setSystemEMailAddress($email);
if ($createdBy instanceof IUser) {
$this->config->setUserValue($userId, 'guests', 'created_by', $createdBy->getUID());
$this->userConfig->setValueString($userId, Application::APP_ID, ConfigLexicon::USER_CREATED_BY, $createdBy->getUID());
}

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

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

return $user;
}
Expand All @@ -129,7 +135,8 @@ public function getGuestsInfo(): array {
$guestsInfo = $this->userBackend->getAllGuestAccounts();
$guests = array_keys($guestsInfo);
$shareCounts = $this->getShareCountForUsers($guests);
$createdBy = $this->config->getUserValueForUsers('guests', 'created_by', $guests);
$createdBy = $this->userConfig->getValuesByUsers(Application::APP_ID, ConfigLexicon::USER_CREATED_BY, ValueType::STRING, $guests);

return array_map(function (string $uid) use ($createdBy, $guestsInfo, $shareCounts): array {
$allSharesCount = count(array_merge(
$this->shareManager->getSharedWith($uid, IShare::TYPE_USER, null, -1, 0),
Expand Down
Loading
Loading