-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathConfigService.php
More file actions
255 lines (224 loc) · 8.22 KB
/
Copy pathConfigService.php
File metadata and controls
255 lines (224 loc) · 8.22 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
namespace OCA\Deck\Service;
use OCA\Deck\AppInfo\Application;
use OCA\Deck\BadRequestException;
use OCA\Deck\Exceptions\FederationDisabledException;
use OCA\Deck\NoPermissionException;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserSession;
class ConfigService {
public const SETTING_BOARD_NOTIFICATION_DUE_OFF = 'off';
public const SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED = 'assigned';
public const SETTING_BOARD_NOTIFICATION_DUE_ALL = 'all';
public const SETTING_BOARD_NOTIFICATION_DUE_DEFAULT = self::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED;
private ?string $userId = null;
public function __construct(
private readonly IConfig $config,
private readonly IGroupManager $groupManager,
) {
}
public function getUserId(): ?string {
if (!$this->userId) {
// We cannot use DI for the userId or UserSession as the ConfigService
// is initiated too early before the session is actually loaded
$user = \OCP\Server::get(IUserSession::class)->getUser();
$this->userId = $user ? $user->getUID() : null;
}
return $this->userId;
}
public function getAll(): array {
$userId = $this->getUserId();
if ($userId === null) {
return [];
}
$data = [
'calendar' => $this->isCalendarEnabled(),
'cardDetailsInModal' => $this->isCardDetailsInModal(),
'cardIdBadge' => $this->isCardIdBadgeEnabled()
];
if ($this->groupManager->isAdmin($userId)) {
$data['groupLimit'] = $this->get('groupLimit');
$data['federationEnabled'] = $this->get('federationEnabled');
}
return $data;
}
/**
* @return bool|array{id: string, displayname: string}[]
* @throws NoPermissionException
*/
public function get(string $key) {
[$scope] = explode(':', $key, 2);
switch ($scope) {
case 'groupLimit':
if ($this->getUserId() === null || !$this->groupManager->isAdmin($this->getUserId())) {
throw new NoPermissionException('You must be admin to get the group limit');
}
return $this->getGroupLimit();
case 'federationEnabled':
return $this->config->getAppValue(Application::APP_ID, 'federationEnabled', 'no') === 'yes';
case 'calendar':
if ($this->getUserId() === null) {
return false;
}
return (bool)$this->config->getUserValue($this->getUserId(), Application::APP_ID, 'calendar', true);
case 'cardDetailsInModal':
if ($this->getUserId() === null) {
return false;
}
return (bool)$this->config->getUserValue($this->getUserId(), Application::APP_ID, 'cardDetailsInModal', true);
case 'cardIdBadge':
if ($this->getUserId() === null) {
return false;
}
return (bool)$this->config->getUserValue($this->getUserId(), Application::APP_ID, 'cardIdBadge', false);
}
return false;
}
public function isCalendarEnabled(?int $boardId = null): bool {
$userId = $this->getUserId();
if ($userId === null) {
return false;
}
$appConfigState = $this->config->getAppValue(Application::APP_ID, 'calendar', 'yes') === 'yes';
$defaultState = (bool)$this->config->getUserValue($userId, Application::APP_ID, 'calendar', $appConfigState);
if ($boardId === null) {
return $defaultState;
}
return (bool)$this->config->getUserValue($userId, Application::APP_ID, 'board:' . $boardId . ':calendar', $defaultState);
}
public function isCardDetailsInModal(?int $boardId = null): bool {
$userId = $this->getUserId();
if ($userId === null) {
return false;
}
$defaultState = (bool)$this->config->getUserValue($userId, Application::APP_ID, 'cardDetailsInModal', true);
if ($boardId === null) {
return $defaultState;
}
return (bool)$this->config->getUserValue($userId, Application::APP_ID, 'board:' . $boardId . ':cardDetailsInModal', $defaultState);
}
public function isCardIdBadgeEnabled(): bool {
$userId = $this->getUserId();
if ($userId === null) {
return false;
}
$appConfigState = $this->config->getAppValue(Application::APP_ID, 'cardIdBadge', 'yes') === 'no';
$defaultState = (bool)$this->config->getUserValue($userId, Application::APP_ID, 'cardIdBadge', $appConfigState);
return (bool)$this->config->getUserValue($userId, Application::APP_ID, 'cardIdBadge', $defaultState);
}
public function ensureFederationEnabled() {
if (!$this->get('federationEnabled')) {
throw new FederationDisabledException();
}
// @TODO fine tune these config values to respect incoming and outgoing federation separately
if ($this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'no') !== 'yes') {
throw new FederationDisabledException();
}
if ($this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'no') !== 'yes') {
throw new FederationDisabledException();
}
}
public function set($key, $value) {
$userId = $this->getUserId();
if ($userId === null) {
throw new NoPermissionException('Must be logged in to set user config');
}
$result = null;
[$scope] = explode(':', $key, 2);
switch ($scope) {
case 'groupLimit':
if (!$this->groupManager->isAdmin($userId)) {
throw new NoPermissionException('You must be admin to set the group limit');
}
$result = $this->setGroupLimit($value);
break;
case 'federationEnabled':
if (!$this->groupManager->isAdmin($userId)) {
throw new NoPermissionException('You must be admin to set the federation enabled setting');
}
$this->config->setAppValue(Application::APP_ID, 'federationEnabled', (string)$value);
$result = $value;
break;
case 'calendar':
$this->config->setUserValue($userId, Application::APP_ID, 'calendar', (string)$value);
$result = $value;
break;
case 'cardDetailsInModal':
$this->config->setUserValue($userId, Application::APP_ID, 'cardDetailsInModal', (string)$value);
$result = $value;
break;
case 'cardIdBadge':
$this->config->setUserValue($userId, Application::APP_ID, 'cardIdBadge', (string)$value);
$result = $value;
break;
case 'board':
// extra check that user only send one of the allowed board settings and not something random
$parts = explode(':', $key, 3);
if (count($parts) < 3) {
break;
}
$boardConfigKey = $parts[2];
if ($boardConfigKey === 'notify-due' && !in_array($value, [self::SETTING_BOARD_NOTIFICATION_DUE_ALL, self::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED, self::SETTING_BOARD_NOTIFICATION_DUE_OFF], true)) {
throw new BadRequestException('Board notification option must be one of: off, assigned, all');
}
$this->config->setUserValue($userId, Application::APP_ID, $key, (string)$value);
$result = $value;
}
return $result;
}
/**
* @return string[]
*/
private function setGroupLimit(array $value): array {
$groups = [];
foreach ($value as $group) {
$groups[] = $group['id'];
}
$data = implode(',', $groups);
$this->config->setAppValue(Application::APP_ID, 'groupLimit', $data);
return $groups;
}
private function getGroupLimitList(): array {
$value = $this->config->getAppValue(Application::APP_ID, 'groupLimit', '');
$groups = explode(',', $value);
if ($value === '') {
return [];
}
return $groups;
}
/** @return array{id: string, displayname: string}[] */
private function getGroupLimit() {
$groups = $this->getGroupLimitList();
$groups = array_map(function (string $groupId): ?array {
/** @var IGroup $groups */
$group = $this->groupManager->get($groupId);
if ($group === null) {
return null;
}
return [
'id' => $group->getGID(),
'displayname' => $group->getDisplayName(),
];
}, $groups);
return array_filter($groups);
}
public function getAttachmentFolder(?string $userId = null): string {
if ($userId === null && $this->getUserId() === null) {
throw new NoPermissionException('Must be logged in get the attachment folder');
}
return $this->config->getUserValue($userId ?? $this->getUserId(), 'deck', 'attachment_folder', '/Deck');
}
public function setAttachmentFolder(?string $userId, string $path): void {
if ($userId === null && $this->getUserId() === null) {
throw new NoPermissionException('Must be logged in get the attachment folder');
}
$this->config->setUserValue($userId ?? $this->getUserId(), 'deck', 'attachment_folder', $path);
}
}