-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathConfigService.php
More file actions
115 lines (101 loc) · 3.44 KB
/
Copy pathConfigService.php
File metadata and controls
115 lines (101 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Forms\Service;
use OCA\Forms\Constants;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
class ConfigService {
private ?IUser $currentUser;
public function __construct(
protected string $appName,
private IConfig $config,
private IGroupManager $groupManager,
IUserSession $userSession,
) {
$this->currentUser = $userSession->getUser();
}
/**
* Load the single values, decode, have default values
*/
public function getAllowPermitAll(): bool {
return json_decode($this->config->getAppValue($this->appName, Constants::CONFIG_KEY_ALLOWPERMITALL, 'true'));
}
public function getAllowPublicLink(): bool {
return json_decode($this->config->getAppValue($this->appName, Constants::CONFIG_KEY_ALLOWPUBLICLINK, 'true'));
}
public function getAllowCustomPublicToken(): bool {
return json_decode($this->config->getAppValue($this->appName, Constants::CONFIG_KEY_ALLOWCUSTOMPUBLICTOKEN, 'false'));
}
public function getAllowShowToAll(): bool {
return json_decode($this->config->getAppValue($this->appName, Constants::CONFIG_KEY_ALLOWSHOWTOALL, 'true'));
}
private function getUnformattedCreationAllowedGroups(): array {
return json_decode($this->config->getAppValue($this->appName, Constants::CONFIG_KEY_CREATIONALLOWEDGROUPS, '[]'));
}
public function getCreationAllowedGroups(): array {
return $this->formatGroupsForMultiselect($this->getUnformattedCreationAllowedGroups());
}
public function getRestrictCreation(): bool {
return json_decode($this->config->getAppValue($this->appName, Constants::CONFIG_KEY_RESTRICTCREATION, 'false'));
}
/**
* Provide the full AppConfig
*/
public function getAppConfig(): array {
return [
Constants::CONFIG_KEY_ALLOWPERMITALL => $this->getAllowPermitAll(),
Constants::CONFIG_KEY_ALLOWPUBLICLINK => $this->getAllowPublicLink(),
Constants::CONFIG_KEY_ALLOWCUSTOMPUBLICTOKEN => $this->getAllowCustomPublicToken(),
Constants::CONFIG_KEY_ALLOWSHOWTOALL => $this->getAllowShowToAll(),
Constants::CONFIG_KEY_CREATIONALLOWEDGROUPS => $this->getCreationAllowedGroups(),
Constants::CONFIG_KEY_RESTRICTCREATION => $this->getRestrictCreation(),
// Additional, calculated information out of Config
'canCreateForms' => $this->canCreateForms()
];
}
/**
* Format the stored groups
*
* @param String[] $groups String Array of the groupIds
* @return Array[] Array of GroupObjects
*/
private function formatGroupsForMultiselect(array $groups): array {
$formattedGroups = [];
foreach ($groups as $groupId) {
$group = $this->groupManager->get($groupId);
if ($group instanceof IGroup) {
$formattedGroups[] = [
'groupId' => $groupId,
'displayName' => $group->getDisplayName()
];
}
}
return $formattedGroups;
}
/**
* Check if currentUser is allowed to create Forms
* @return bool
*/
public function canCreateForms(): bool {
if ($this->currentUser === null) {
return false;
}
// Restriction active or not
if (!$this->getRestrictCreation()) {
return true;
}
$userGroups = $this->groupManager->getUserGroupIds($this->currentUser);
// If array intersection is not empty, user is member of any allowed group.
if (sizeof(array_intersect($userGroups, $this->getUnformattedCreationAllowedGroups()))) {
return true;
}
return false;
}
}