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
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Provisioning_API\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\Config\BeforePreferenceDeletedEvent;
use OCP\Config\BeforePreferenceSetEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
class PreferencesController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private IConfig $config,
private IUserSession $userSession,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request);
}
/**
* @NoSubAdminRequired
*
* Update multiple preference values of an app
*
* @param string $appId ID of the app
* @param array<string, string> $configs Key-value pairs of the preferences
*
* @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
*
* 200: Preferences updated successfully
* 400: Preference invalid
*/
#[NoAdminRequired]
public function setMultiplePreferences(string $appId, array $configs): DataResponse {
$userId = $this->userSession->getUser()->getUID();
foreach ($configs as $configKey => $configValue) {
$event = new BeforePreferenceSetEvent(
$userId,
$appId,
$configKey,
$configValue
);
$this->eventDispatcher->dispatchTyped($event);
if (!$event->isValid()) {
// No listener validated that the preference can be set (to this value)
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
foreach ($configs as $configKey => $configValue) {
$this->config->setUserValue(
$userId,
$appId,
$configKey,
$configValue
);
}
return new DataResponse();
}
/**
* @NoSubAdminRequired
*
* Update a preference value of an app
*
* @param string $appId ID of the app
* @param string $configKey Key of the preference
* @param string $configValue New value
* @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
*
* 200: Preference updated successfully
* 400: Preference invalid
*/
#[NoAdminRequired]
public function setPreference(string $appId, string $configKey, string $configValue): DataResponse {
$userId = $this->userSession->getUser()->getUID();
$event = new BeforePreferenceSetEvent(
$userId,
$appId,
$configKey,
$configValue
);
$this->eventDispatcher->dispatchTyped($event);
if (!$event->isValid()) {
// No listener validated that the preference can be set (to this value)
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
$this->config->setUserValue(
$userId,
$appId,
$configKey,
$configValue
);
return new DataResponse();
}
/**
* @NoSubAdminRequired
*
* Delete multiple preferences for an app
*
* @param string $appId ID of the app
* @param string[] $configKeys Keys to delete
*
* @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
*
* 200: Preferences deleted successfully
* 400: Preference invalid
*/
#[NoAdminRequired]
public function deleteMultiplePreference(string $appId, array $configKeys): DataResponse {
$userId = $this->userSession->getUser()->getUID();
foreach ($configKeys as $configKey) {
$event = new BeforePreferenceDeletedEvent(
$userId,
$appId,
$configKey
);
$this->eventDispatcher->dispatchTyped($event);
if (!$event->isValid()) {
// No listener validated that the preference can be deleted
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
foreach ($configKeys as $configKey) {
$this->config->deleteUserValue(
$userId,
$appId,
$configKey
);
}
return new DataResponse();
}
/**
* @NoSubAdminRequired
*
* Delete a preference for an app
*
* @param string $appId ID of the app
* @param string $configKey Key to delete
* @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
*
* 200: Preference deleted successfully
* 400: Preference invalid
*/
#[NoAdminRequired]
public function deletePreference(string $appId, string $configKey): DataResponse {
$userId = $this->userSession->getUser()->getUID();
$event = new BeforePreferenceDeletedEvent(
$userId,
$appId,
$configKey
);
$this->eventDispatcher->dispatchTyped($event);
if (!$event->isValid()) {
// No listener validated that the preference can be deleted
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
$this->config->deleteUserValue(
$userId,
$appId,
$configKey
);
return new DataResponse();
}
}
|