-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathConfigService.php
More file actions
128 lines (111 loc) Β· 3.79 KB
/
ConfigService.php
File metadata and controls
128 lines (111 loc) Β· 3.79 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
116
117
118
119
120
121
122
123
124
125
126
127
128
<?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\AppFramework\Services\IAppConfig;
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 IAppConfig $appConfig,
private IGroupManager $groupManager,
IUserSession $userSession,
) {
$this->currentUser = $userSession->getUser();
}
/**
* Load the single values, decode, have default values
*/
public function getAllowPermitAll(): bool {
return $this->appConfig->getAppValueBool(Constants::CONFIG_KEY_ALLOWPERMITALL, true);
}
public function getAllowPublicLink(): bool {
return $this->appConfig->getAppValueBool(Constants::CONFIG_KEY_ALLOWPUBLICLINK, true);
}
public function getAllowShowToAll() : bool {
return $this->appConfig->getAppValueBool(Constants::CONFIG_KEY_ALLOWSHOWTOALL, true);
}
private function getUnformattedCreationAllowedGroups(): array {
return $this->appConfig->getAppValueArray(Constants::CONFIG_KEY_CREATIONALLOWEDGROUPS, []);
}
public function getCreationAllowedGroups(): array {
return $this->formatGroupsForMultiselect($this->getUnformattedCreationAllowedGroups());
}
public function getRestrictCreation(): bool {
return $this->appConfig->getAppValueBool(Constants::CONFIG_KEY_RESTRICTCREATION, false);
}
public function getAllowConfirmationEmail(): bool {
return $this->appConfig->getAppValueBool(Constants::CONFIG_KEY_ALLOWCONFIRMATIONEMAIL, false);
}
public function isMailConfigured(): bool {
return $this->config->getSystemValue('mail_from_address', '') !== '';
}
public function getConfirmationEmailRateLimit(): int {
return $this->appConfig->getAppValueInt(Constants::CONFIG_KEY_CONFIRMATIONEMAILRATELIMIT, 3);
}
/**
* Provide the full AppConfig
*/
public function getAppConfig(): array {
return [
Constants::CONFIG_KEY_ALLOWPERMITALL => $this->getAllowPermitAll(),
Constants::CONFIG_KEY_ALLOWPUBLICLINK => $this->getAllowPublicLink(),
Constants::CONFIG_KEY_ALLOWSHOWTOALL => $this->getAllowShowToAll(),
Constants::CONFIG_KEY_CREATIONALLOWEDGROUPS => $this->getCreationAllowedGroups(),
Constants::CONFIG_KEY_RESTRICTCREATION => $this->getRestrictCreation(),
Constants::CONFIG_KEY_ALLOWCONFIRMATIONEMAIL => $this->getAllowConfirmationEmail(),
Constants::CONFIG_KEY_CONFIRMATIONEMAILRATELIMIT => $this->getConfirmationEmailRateLimit(),
'isMailConfigured' => $this->isMailConfigured(),
// 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;
}
}