-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConfigController.php
More file actions
346 lines (326 loc) · 13 KB
/
Copy pathConfigController.php
File metadata and controls
346 lines (326 loc) · 13 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
declare(strict_types=1);
namespace PhpList\RestBundle\Configuration\Controller;
use Doctrine\ORM\EntityManagerInterface;
use OpenApi\Attributes as OA;
use PhpList\Core\Domain\Configuration\Exception\ConfigNotEditableException;
use PhpList\Core\Domain\Configuration\Model\Config;
use PhpList\Core\Domain\Configuration\Service\Manager\ConfigManager;
use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
use PhpList\Core\Domain\Identity\Service\Authentication;
use PhpList\RestBundle\Common\Controller\BaseController;
use PhpList\RestBundle\Common\Validator\RequestValidator;
use PhpList\RestBundle\Configuration\Request\CreateConfigRequest;
use PhpList\RestBundle\Configuration\Request\UpdateConfigRequest;
use PhpList\RestBundle\Configuration\Serializer\ConfigNormalizer;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/configs', name: 'config_')]
class ConfigController extends BaseController
{
public function __construct(
Authentication $authentication,
RequestValidator $validator,
private readonly ConfigManager $manager,
private readonly ConfigNormalizer $normalizer,
private readonly EntityManagerInterface $entityManager,
) {
parent::__construct($authentication, $validator);
}
#[Route('', name: 'get_list', methods: ['GET'])]
#[OA\Get(
path: '/api/v2/configs',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
'Returns all configuration items.',
summary: 'Gets all configuration items.',
tags: ['configs'],
parameters: [
new OA\Parameter(
name: 'php-auth-pw',
description: 'Session key obtained from login',
in: 'header',
required: true,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
properties: [
new OA\Property(
property: 'items',
type: 'array',
items: new OA\Items(ref: '#/components/schemas/Config')
),
new OA\Property(property: 'pagination', ref: '#/components/schemas/CursorPagination')
],
type: 'object'
)
),
new OA\Response(
response: 403,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
),
]
)]
public function list(Request $request): JsonResponse
{
$this->denyUnlessSettingsAdmin($request, 'You are not allowed to view configuration.');
$items = $this->manager->getAllEditable();
usort(
$items,
fn (Config $aConf, Config $bConf): int => strcmp(
strtolower($aConf->getKey()),
strtolower($bConf->getKey())
)
);
$count = count($items);
return $this->json(
data: [
'items' => array_map(fn($config) => $this->normalizer->normalize($config), $items),
'pagination' => [
'total' => $count,
'limit' => $count + 1,
'has_more' => false,
'next_cursor' => null
]
],
status: Response::HTTP_OK
);
}
#[Route('/{key}', name: 'get_one', requirements: ['key' => '[A-Za-z0-9_.:-]+'], methods: ['GET'])]
#[OA\Get(
path: '/api/v2/configs/{key}',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
'Returns one configuration item by key.',
summary: 'Gets a configuration item.',
tags: ['configs'],
parameters: [
new OA\Parameter(
name: 'php-auth-pw',
description: 'Session key obtained from login',
in: 'header',
required: true,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'key',
description: 'Configuration key',
in: 'path',
required: true,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(ref: '#/components/schemas/Config')
),
new OA\Response(
response: 403,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
),
new OA\Response(
response: 404,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
),
]
)]
public function getOne(
Request $request,
#[MapEntity(mapping: ['key' => 'key'])] ?Config $config,
): JsonResponse {
$this->denyUnlessSettingsAdmin($request, 'You are not allowed to view configuration.');
if ($config === null) {
throw $this->createNotFoundException('Configuration item not found.');
}
return $this->json($this->normalizer->normalize($config), Response::HTTP_OK);
}
#[Route('', name: 'create', methods: ['POST'])]
#[OA\Post(
path: '/api/v2/configs',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
'Creates a configuration item.',
summary: 'Creates a configuration item.',
requestBody: new OA\RequestBody(
description: 'Configuration item data',
required: true,
content: new OA\JsonContent(ref: '#/components/schemas/ConfigRequest')
),
tags: ['configs'],
parameters: [
new OA\Parameter(
name: 'php-auth-pw',
description: 'Session key obtained from login',
in: 'header',
required: true,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(
response: 201,
description: 'Success',
content: new OA\JsonContent(ref: '#/components/schemas/Config')
),
new OA\Response(
response: 403,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
),
new OA\Response(
response: 409,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/AlreadyExistsResponse')
),
new OA\Response(
response: 422,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse')
),
]
)]
public function create(Request $request): JsonResponse
{
$this->denyUnlessSettingsAdmin($request, 'You are not allowed to create configuration.');
/* @var CreateConfigRequest $configRequest */
$configRequest = $this->validator->validate($request, CreateConfigRequest::class);
$config = $this->manager->create($configRequest->getDto());
$this->entityManager->flush();
return $this->json($this->normalizer->normalize($config), Response::HTTP_CREATED);
}
#[Route('/{key}', name: 'update', requirements: ['key' => '[A-Za-z0-9_.:-]+'], methods: ['PUT'])]
#[OA\Put(
path: '/api/v2/configs/{key}',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
'Updates a configuration item value.',
summary: 'Updates a configuration item.',
requestBody: new OA\RequestBody(
description: 'Configuration item data',
required: true,
content: new OA\JsonContent(ref: '#/components/schemas/ConfigUpdateRequest')
),
tags: ['configs'],
parameters: [
new OA\Parameter(
name: 'php-auth-pw',
description: 'Session key obtained from login',
in: 'header',
required: true,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'key',
description: 'Configuration key',
in: 'path',
required: true,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(ref: '#/components/schemas/Config')
),
new OA\Response(
response: 403,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
),
new OA\Response(
response: 404,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
),
new OA\Response(
response: 422,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse')
),
]
)]
public function update(
Request $request,
#[MapEntity(mapping: ['key' => 'key'])] ?Config $config = null
): JsonResponse {
$this->denyUnlessSettingsAdmin($request, 'You are not allowed to update configuration.');
if ($config === null) {
throw $this->createNotFoundException('Configuration item not found.');
}
/* @var UpdateConfigRequest $dto */
$dto = $this->validator->validate($request, UpdateConfigRequest::class);
try {
$this->manager->update($config, $dto->getDto()['value']);
} catch (ConfigNotEditableException $exception) {
throw $this->createAccessDeniedException($exception->getMessage());
}
$this->entityManager->flush();
return $this->json($this->normalizer->normalize($config), Response::HTTP_OK);
}
#[Route('/{key}', name: 'delete', requirements: ['key' => '[A-Za-z0-9_.:-]+'], methods: ['DELETE'])]
#[OA\Delete(
path: '/api/v2/configs/{key}',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
'Deletes a configuration item.',
summary: 'Deletes a configuration item.',
tags: ['configs'],
parameters: [
new OA\Parameter(
name: 'php-auth-pw',
description: 'Session key obtained from login',
in: 'header',
required: true,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'key',
description: 'Configuration key',
in: 'path',
required: true,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(response: Response::HTTP_NO_CONTENT, description: 'Success'),
new OA\Response(
response: 403,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
),
new OA\Response(
response: 404,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
),
]
)]
public function delete(
Request $request,
#[MapEntity(mapping: ['key' => 'key'])] ?Config $config = null
): JsonResponse {
$this->denyUnlessSettingsAdmin($request, 'You are not allowed to delete configuration.');
if ($config === null) {
throw $this->createNotFoundException('Configuration item not found.');
}
$this->manager->delete($config);
$this->entityManager->flush();
return $this->json(null, Response::HTTP_NO_CONTENT);
}
private function denyUnlessSettingsAdmin(Request $request, string $message): void
{
$admin = $this->requireAuthentication($request);
if (!$admin->getPrivileges()->has(PrivilegeFlag::Settings)) {
throw $this->createAccessDeniedException($message);
}
}
}