|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CRUDlex package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CRUDlex; |
|
13
|
|
|
|
|
14
|
|
|
use League\Flysystem\FilesystemInterface; |
|
15
|
|
|
use League\Flysystem\Util\MimeType; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
21
|
|
|
use Symfony\Component\Translation\Translator; |
|
22
|
|
|
use Twig_Environment; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This is the Controller offering all CRUD pages. |
|
27
|
|
|
* |
|
28
|
|
|
* It offers functions for this routes: |
|
29
|
|
|
* |
|
30
|
|
|
* "/resource/static" serving static resources |
|
31
|
|
|
* |
|
32
|
|
|
* "/{entity}/create" creation page of the entity |
|
33
|
|
|
* |
|
34
|
|
|
* "/{entity}" list page of the entity |
|
35
|
|
|
* |
|
36
|
|
|
* "/{entity}/{id}" details page of a single entity instance |
|
37
|
|
|
* |
|
38
|
|
|
* "/{entity}/{id}/edit" edit page of a single entity instance |
|
39
|
|
|
* |
|
40
|
|
|
* "/{entity}/{id}/delete" POST only deletion route for an entity instance |
|
41
|
|
|
* |
|
42
|
|
|
* "/{entity}/{id}/{field}/file" renders a file field of an entity instance |
|
43
|
|
|
* |
|
44
|
|
|
* "/{entity}/{id}/{field}/delete" POST only deletion of a file field of an entity instance |
|
45
|
|
|
*/ |
|
46
|
|
|
class Controller { |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Holds the filesystme. |
|
50
|
|
|
* @var FilesystemInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $filesystem; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Holds the session. |
|
56
|
|
|
* @var Session |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $session; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Holds the translator. |
|
62
|
|
|
* @var Translator |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $translator; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Holds the service. |
|
68
|
|
|
* @var Service |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $service; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Holds the Twig instance. |
|
74
|
|
|
* @var Twig_Environment |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $twig; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Postprocesses the entity after modification by handling the uploaded |
|
80
|
|
|
* files and setting the flash. |
|
81
|
|
|
* |
|
82
|
|
|
* @param Request $request |
|
83
|
|
|
* the current request |
|
84
|
|
|
* @param AbstractData $crudData |
|
85
|
|
|
* the data instance of the entity |
|
86
|
|
|
* @param Entity $instance |
|
87
|
|
|
* the entity |
|
88
|
|
|
* @param string $entity |
|
89
|
|
|
* the name of the entity |
|
90
|
|
|
* @param string $mode |
|
91
|
|
|
* whether to 'edit' or to 'create' the entity |
|
92
|
|
|
* |
|
93
|
|
|
* @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
|
94
|
|
|
* the HTTP response of this modification |
|
95
|
|
|
*/ |
|
96
|
4 |
|
protected function modifyFilesAndSetFlashBag(Request $request, AbstractData $crudData, Entity $instance, $entity, $mode) |
|
97
|
|
|
{ |
|
98
|
4 |
|
$id = $instance->get('id'); |
|
99
|
4 |
|
$fileHandler = new FileHandler($this->filesystem, $crudData->getDefinition()); |
|
100
|
4 |
|
$result = $mode == 'edit' ? $fileHandler->updateFiles($crudData, $request, $instance, $entity) : $fileHandler->createFiles($crudData, $request, $instance, $entity); |
|
101
|
4 |
|
if (!$result) { |
|
102
|
2 |
|
return null; |
|
103
|
|
|
} |
|
104
|
4 |
|
$this->session->getFlashBag()->add('success', $this->translator->trans('crudlex.'.$mode.'.success', [ |
|
105
|
4 |
|
'%label%' => $crudData->getDefinition()->getLabel(), |
|
106
|
4 |
|
'%id%' => $id |
|
107
|
|
|
])); |
|
108
|
4 |
|
return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Sets the flashes of a failed entity modification. |
|
113
|
|
|
* |
|
114
|
|
|
* @param boolean $optimisticLocking |
|
115
|
|
|
* whether the optimistic locking failed |
|
116
|
|
|
* @param string $mode |
|
117
|
|
|
* the modification mode, either 'create' or 'edit' |
|
118
|
|
|
*/ |
|
119
|
2 |
|
protected function setValidationFailedFlashes($optimisticLocking, $mode) |
|
120
|
|
|
{ |
|
121
|
2 |
|
$this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.'.$mode.'.error')); |
|
122
|
2 |
|
if ($optimisticLocking) { |
|
123
|
1 |
|
$this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.edit.locked')); |
|
124
|
|
|
} |
|
125
|
2 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Validates and saves the new or updated entity and returns the appropriate HTTP |
|
129
|
|
|
* response. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Request $request |
|
132
|
|
|
* the current request |
|
133
|
|
|
* @param AbstractData $crudData |
|
134
|
|
|
* the data instance of the entity |
|
135
|
|
|
* @param Entity $instance |
|
136
|
|
|
* the entity |
|
137
|
|
|
* @param string $entity |
|
138
|
|
|
* the name of the entity |
|
139
|
|
|
* @param boolean $edit |
|
140
|
|
|
* whether to edit (true) or to create (false) the entity |
|
141
|
|
|
* |
|
142
|
|
|
* @return Response |
|
|
|
|
|
|
143
|
|
|
* the HTTP response of this modification |
|
144
|
|
|
*/ |
|
145
|
5 |
|
protected function modifyEntity(Request $request, AbstractData $crudData, Entity $instance, $entity, $edit) |
|
146
|
|
|
{ |
|
147
|
5 |
|
$fieldErrors = []; |
|
148
|
5 |
|
$mode = $edit ? 'edit' : 'create'; |
|
149
|
5 |
|
if ($request->getMethod() == 'POST') { |
|
150
|
4 |
|
$instance->populateViaRequest($request); |
|
151
|
4 |
|
$validator = new EntityValidator($instance); |
|
152
|
4 |
|
$validation = $validator->validate($crudData, intval($request->get('version'))); |
|
153
|
|
|
|
|
154
|
4 |
|
$fieldErrors = $validation['errors']; |
|
155
|
4 |
|
if (!$validation['valid']) { |
|
156
|
2 |
|
$optimisticLocking = isset($fieldErrors['version']); |
|
157
|
2 |
|
$this->setValidationFailedFlashes($optimisticLocking, $mode); |
|
158
|
|
|
} else { |
|
159
|
4 |
|
$modified = $edit ? $crudData->update($instance) : $crudData->create($instance); |
|
160
|
4 |
|
$response = $modified ? $this->modifyFilesAndSetFlashBag($request, $crudData, $instance, $entity, $mode) : false; |
|
161
|
4 |
|
if ($response) { |
|
162
|
4 |
|
return $response; |
|
163
|
|
|
} |
|
164
|
2 |
|
$this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.'.$mode.'.failed')); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
3 |
|
return $this->twig->render($this->service->getTemplate('template', 'form', $entity), [ |
|
169
|
3 |
|
'crud' => $this->service, |
|
170
|
3 |
|
'crudEntity' => $entity, |
|
171
|
3 |
|
'crudData' => $crudData, |
|
172
|
3 |
|
'entity' => $instance, |
|
173
|
3 |
|
'mode' => $mode, |
|
174
|
3 |
|
'fieldErrors' => $fieldErrors, |
|
175
|
3 |
|
'layout' => $this->service->getTemplate('layout', $mode, $entity) |
|
176
|
|
|
]); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Gets the parameters for the redirection after deleting an entity. |
|
181
|
|
|
* |
|
182
|
|
|
* @param Request $request |
|
183
|
|
|
* the current request |
|
184
|
|
|
* @param string $entity |
|
185
|
|
|
* the entity name |
|
186
|
|
|
* @param string $redirectPage |
|
187
|
|
|
* reference, where the page to redirect to will be stored |
|
188
|
|
|
* |
|
189
|
|
|
* @return array<string,string> |
|
190
|
|
|
* the parameters of the redirection, entity and id |
|
191
|
|
|
*/ |
|
192
|
1 |
|
protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) |
|
193
|
|
|
{ |
|
194
|
1 |
|
$redirectPage = 'crudList'; |
|
195
|
1 |
|
$redirectParameters = ['entity' => $entity]; |
|
196
|
1 |
|
$redirectEntity = $request->get('redirectEntity'); |
|
197
|
1 |
|
$redirectId = $request->get('redirectId'); |
|
198
|
1 |
|
if ($redirectEntity && $redirectId) { |
|
199
|
1 |
|
$redirectPage = 'crudShow'; |
|
200
|
|
|
$redirectParameters = [ |
|
201
|
1 |
|
'entity' => $redirectEntity, |
|
202
|
1 |
|
'id' => $redirectId |
|
203
|
|
|
]; |
|
204
|
|
|
} |
|
205
|
1 |
|
return $redirectParameters; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Builds up the parameters of the list page filters. |
|
210
|
|
|
* |
|
211
|
|
|
* @param Request $request |
|
212
|
|
|
* the current request |
|
213
|
|
|
* @param EntityDefinition $definition |
|
214
|
|
|
* the current entity definition |
|
215
|
|
|
* @param array &$filter |
|
216
|
|
|
* will hold a map of fields to request parameters for the filters |
|
217
|
|
|
* @param boolean $filterActive |
|
218
|
|
|
* reference, will be true if at least one filter is active |
|
219
|
|
|
* @param array $filterToUse |
|
220
|
|
|
* reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
|
221
|
|
|
* @param array $filterOperators |
|
222
|
|
|
* reference, will hold a map of fields to operators for AbstractData::listEntries() |
|
223
|
|
|
*/ |
|
224
|
4 |
|
protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) |
|
225
|
|
|
{ |
|
226
|
4 |
|
foreach ($definition->getFilter() as $filterField) { |
|
227
|
4 |
|
$type = $definition->getType($filterField); |
|
228
|
4 |
|
$filter[$filterField] = $request->get('crudFilter'.$filterField); |
|
229
|
4 |
|
if ($filter[$filterField]) { |
|
230
|
1 |
|
$filterActive = true; |
|
231
|
1 |
|
$filterToUse[$filterField] = $filter[$filterField]; |
|
232
|
1 |
|
$filterOperators[$filterField] = '='; |
|
233
|
1 |
|
if ($type === 'boolean') { |
|
234
|
1 |
|
$filterToUse[$filterField] = $filter[$filterField] == 'true' ? 1 : 0; |
|
235
|
1 |
|
} else if ($type === 'reference') { |
|
236
|
1 |
|
$filter[$filterField] = ['id' => $filter[$filterField]]; |
|
237
|
1 |
|
} else if ($type === 'many') { |
|
238
|
1 |
|
$filter[$filterField] = array_map(function($value) { |
|
239
|
1 |
|
return ['id' => $value]; |
|
240
|
1 |
|
}, $filter[$filterField]); |
|
241
|
1 |
|
$filterToUse[$filterField] = $filter[$filterField]; |
|
242
|
1 |
|
} else if (in_array($type, ['text', 'multiline', 'fixed'])) { |
|
243
|
1 |
|
$filterToUse[$filterField] = '%'.$filter[$filterField].'%'; |
|
244
|
4 |
|
$filterOperators[$filterField] = 'LIKE'; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
4 |
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Controller constructor. |
|
252
|
|
|
* |
|
253
|
|
|
* @param Service $service |
|
254
|
|
|
* the CRUDlex service |
|
255
|
|
|
* @param FilesystemInterface $filesystem |
|
256
|
|
|
* the used filesystem |
|
257
|
|
|
* @param Twig_Environment $twig |
|
258
|
|
|
* the Twig environment |
|
259
|
|
|
* @param Session $session |
|
260
|
|
|
* the session service |
|
261
|
|
|
* @param Translator $translator |
|
262
|
|
|
* the translation service |
|
263
|
|
|
*/ |
|
264
|
10 |
|
public function __construct(Service $service, FilesystemInterface $filesystem, Twig_Environment $twig, Session $session, Translator $translator) |
|
265
|
|
|
{ |
|
266
|
10 |
|
$this->service = $service; |
|
267
|
10 |
|
$this->filesystem = $filesystem; |
|
268
|
10 |
|
$this->twig = $twig; |
|
269
|
10 |
|
$this->session = $session; |
|
270
|
10 |
|
$this->translator = $translator; |
|
271
|
10 |
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Generates the not found page. |
|
275
|
|
|
* |
|
276
|
|
|
* @param string $error |
|
277
|
|
|
* the cause of the not found error |
|
278
|
|
|
* |
|
279
|
|
|
* @return Response |
|
280
|
|
|
* the rendered not found page with the status code 404 |
|
281
|
|
|
*/ |
|
282
|
9 |
|
public function getNotFoundPage($error) |
|
283
|
|
|
{ |
|
284
|
9 |
|
return new Response($this->twig->render('@crud/notFound.twig', [ |
|
285
|
9 |
|
'crud' => $this->service, |
|
286
|
9 |
|
'error' => $error, |
|
287
|
9 |
|
'crudEntity' => '', |
|
288
|
9 |
|
'layout' => $this->service->getTemplate('layout', '', '') |
|
289
|
9 |
|
]), 404); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Transfers the locale from the translator to CRUDlex and |
|
294
|
|
|
* |
|
295
|
|
|
* @param Request $request |
|
296
|
|
|
* the current request |
|
297
|
|
|
* @return Response|null |
|
298
|
|
|
* null if everything is ok, a 404 response else |
|
299
|
|
|
*/ |
|
300
|
9 |
|
public function setLocaleAndCheckEntity(Request $request) |
|
301
|
|
|
{ |
|
302
|
9 |
|
$locale = $this->translator->getLocale(); |
|
303
|
9 |
|
$this->service->setLocale($locale); |
|
304
|
9 |
|
if (!$this->service->getData($request->get('entity'))) { |
|
305
|
7 |
|
return $this->getNotFoundPage($this->translator->trans('crudlex.entityNotFound')); |
|
306
|
|
|
} |
|
307
|
9 |
|
return null; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* The controller for the "create" action. |
|
312
|
|
|
* |
|
313
|
|
|
* @param Request $request |
|
314
|
|
|
* the current request |
|
315
|
|
|
* @param string $entity |
|
316
|
|
|
* the current entity |
|
317
|
|
|
* |
|
318
|
|
|
* @return Response |
|
|
|
|
|
|
319
|
|
|
* the HTTP response of this action |
|
320
|
|
|
*/ |
|
321
|
4 |
|
public function create(Request $request, $entity) |
|
322
|
|
|
{ |
|
323
|
4 |
|
$crudData = $this->service->getData($entity); |
|
324
|
4 |
|
$instance = $crudData->createEmpty(); |
|
325
|
4 |
|
$instance->populateViaRequest($request); |
|
326
|
4 |
|
return $this->modifyEntity($request, $crudData, $instance, $entity, false); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* The controller for the "show list" action. |
|
331
|
|
|
* |
|
332
|
|
|
* @param Request $request |
|
333
|
|
|
* the current request |
|
334
|
|
|
* @param string $entity |
|
335
|
|
|
* the current entity |
|
336
|
|
|
* |
|
337
|
|
|
* @return Response |
|
|
|
|
|
|
338
|
|
|
* the HTTP response of this action or 404 on invalid input |
|
339
|
|
|
*/ |
|
340
|
4 |
|
public function showList(Request $request, $entity) |
|
341
|
|
|
{ |
|
342
|
4 |
|
$crudData = $this->service->getData($entity); |
|
343
|
4 |
|
$definition = $crudData->getDefinition(); |
|
344
|
|
|
|
|
345
|
4 |
|
$filter = []; |
|
346
|
4 |
|
$filterActive = false; |
|
347
|
4 |
|
$filterToUse = []; |
|
348
|
4 |
|
$filterOperators = []; |
|
349
|
4 |
|
$this->buildUpListFilter($request, $definition, $filter, $filterActive, $filterToUse, $filterOperators); |
|
350
|
|
|
|
|
351
|
4 |
|
$pageSize = $definition->getPageSize(); |
|
352
|
4 |
|
$total = $crudData->countBy($definition->getTable(), $filterToUse, $filterOperators, true); |
|
353
|
4 |
|
$page = abs(intval($request->get('crudPage', 0))); |
|
354
|
4 |
|
$maxPage = intval($total / $pageSize); |
|
355
|
4 |
|
if ($total % $pageSize == 0) { |
|
356
|
4 |
|
$maxPage--; |
|
357
|
|
|
} |
|
358
|
4 |
|
if ($page > $maxPage) { |
|
359
|
4 |
|
$page = $maxPage; |
|
360
|
|
|
} |
|
361
|
4 |
|
$skip = $page * $pageSize; |
|
362
|
|
|
|
|
363
|
4 |
|
$sortField = $request->get('crudSortField', $definition->getInitialSortField()); |
|
364
|
4 |
|
$sortAscendingRequest = $request->get('crudSortAscending'); |
|
365
|
4 |
|
$sortAscending = $sortAscendingRequest !== null ? $sortAscendingRequest === 'true' : $definition->isInitialSortAscending(); |
|
366
|
|
|
|
|
367
|
4 |
|
$entities = $crudData->listEntries($filterToUse, $filterOperators, $skip, $pageSize, $sortField, $sortAscending); |
|
368
|
|
|
|
|
369
|
4 |
|
return $this->twig->render($this->service->getTemplate('template', 'list', $entity), [ |
|
370
|
4 |
|
'crud' => $this->service, |
|
371
|
4 |
|
'crudEntity' => $entity, |
|
372
|
4 |
|
'crudData' => $crudData, |
|
373
|
4 |
|
'definition' => $definition, |
|
374
|
4 |
|
'entities' => $entities, |
|
375
|
4 |
|
'pageSize' => $pageSize, |
|
376
|
4 |
|
'maxPage' => $maxPage, |
|
377
|
4 |
|
'page' => $page, |
|
378
|
4 |
|
'total' => $total, |
|
379
|
4 |
|
'filter' => $filter, |
|
380
|
4 |
|
'filterActive' => $filterActive, |
|
381
|
4 |
|
'sortField' => $sortField, |
|
382
|
4 |
|
'sortAscending' => $sortAscending, |
|
383
|
4 |
|
'layout' => $this->service->getTemplate('layout', 'list', $entity) |
|
384
|
|
|
]); |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* The controller for the "show" action. |
|
389
|
|
|
* |
|
390
|
|
|
* @param string $entity |
|
391
|
|
|
* the current entity |
|
392
|
|
|
* @param string $id |
|
393
|
|
|
* the instance id to show |
|
394
|
|
|
* |
|
395
|
|
|
* @return Response |
|
396
|
|
|
* the HTTP response of this action or 404 on invalid input |
|
397
|
|
|
*/ |
|
398
|
6 |
|
public function show($entity, $id) |
|
399
|
|
|
{ |
|
400
|
6 |
|
$crudData = $this->service->getData($entity); |
|
401
|
6 |
|
$instance = $crudData->get($id); |
|
402
|
6 |
|
if (!$instance) { |
|
403
|
1 |
|
return $this->getNotFoundPage($this->translator->trans('crudlex.instanceNotFound')); |
|
404
|
|
|
} |
|
405
|
6 |
|
$definition = $crudData->getDefinition(); |
|
406
|
|
|
|
|
407
|
6 |
|
$childrenLabelFields = $definition->getChildrenLabelFields(); |
|
408
|
6 |
|
$children = []; |
|
409
|
6 |
|
if (count($childrenLabelFields) > 0) { |
|
410
|
3 |
|
foreach ($definition->getChildren() as $child) { |
|
411
|
3 |
|
$childField = $child[1]; |
|
412
|
3 |
|
$childEntity = $child[2]; |
|
413
|
3 |
|
$childLabelField = array_key_exists($childEntity, $childrenLabelFields) ? $childrenLabelFields[$childEntity] : 'id'; |
|
414
|
3 |
|
$childCrud = $this->service->getData($childEntity); |
|
415
|
3 |
|
$children[] = [ |
|
416
|
3 |
|
$childCrud->getDefinition()->getLabel(), |
|
417
|
3 |
|
$childEntity, |
|
418
|
3 |
|
$childLabelField, |
|
419
|
3 |
|
$childCrud->listEntries([$childField => $instance->get('id')]), |
|
420
|
3 |
|
$childField |
|
421
|
|
|
]; |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
6 |
|
return $this->twig->render($this->service->getTemplate('template', 'show', $entity), [ |
|
426
|
6 |
|
'crud' => $this->service, |
|
427
|
6 |
|
'crudEntity' => $entity, |
|
428
|
6 |
|
'entity' => $instance, |
|
429
|
6 |
|
'children' => $children, |
|
430
|
6 |
|
'layout' => $this->service->getTemplate('layout', 'show', $entity) |
|
431
|
|
|
]); |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* The controller for the "edit" action. |
|
436
|
|
|
* |
|
437
|
|
|
* @param Request $request |
|
438
|
|
|
* the current request |
|
439
|
|
|
* @param string $entity |
|
440
|
|
|
* the current entity |
|
441
|
|
|
* @param string $id |
|
442
|
|
|
* the instance id to edit |
|
443
|
|
|
* |
|
444
|
|
|
* @return Response |
|
445
|
|
|
* the HTTP response of this action or 404 on invalid input |
|
446
|
|
|
*/ |
|
447
|
1 |
|
public function edit(Request $request, $entity, $id) |
|
448
|
|
|
{ |
|
449
|
1 |
|
$crudData = $this->service->getData($entity); |
|
450
|
1 |
|
$instance = $crudData->get($id); |
|
451
|
1 |
|
if (!$instance) { |
|
452
|
1 |
|
return $this->getNotFoundPage($this->translator->trans('crudlex.instanceNotFound')); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
1 |
|
return $this->modifyEntity($request, $crudData, $instance, $entity, true); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* The controller for the "delete" action. |
|
460
|
|
|
* |
|
461
|
|
|
* @param Request $request |
|
462
|
|
|
* the current request |
|
463
|
|
|
* @param string $entity |
|
464
|
|
|
* the current entity |
|
465
|
|
|
* @param string $id |
|
466
|
|
|
* the instance id to delete |
|
467
|
|
|
* |
|
468
|
|
|
* @return Response |
|
469
|
|
|
* redirects to the entity list page or 404 on invalid input |
|
470
|
|
|
*/ |
|
471
|
1 |
|
public function delete(Request $request, $entity, $id) |
|
472
|
|
|
{ |
|
473
|
1 |
|
$crudData = $this->service->getData($entity); |
|
474
|
1 |
|
$instance = $crudData->get($id); |
|
475
|
1 |
|
if (!$instance) { |
|
476
|
1 |
|
return $this->getNotFoundPage($this->translator->trans('crudlex.instanceNotFound')); |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
1 |
|
$fileHandler = new FileHandler($this->filesystem, $crudData->getDefinition()); |
|
480
|
1 |
|
$filesDeleted = $fileHandler->deleteFiles($crudData, $instance, $entity); |
|
481
|
1 |
|
$deleted = $filesDeleted ? $crudData->delete($instance) : AbstractData::DELETION_FAILED_EVENT; |
|
482
|
|
|
|
|
483
|
1 |
|
if ($deleted === AbstractData::DELETION_FAILED_EVENT) { |
|
484
|
1 |
|
$this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.delete.failed')); |
|
485
|
1 |
|
return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
|
|
|
|
|
|
486
|
1 |
|
} elseif ($deleted === AbstractData::DELETION_FAILED_STILL_REFERENCED) { |
|
487
|
1 |
|
$this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.delete.error', [ |
|
488
|
1 |
|
'%label%' => $crudData->getDefinition()->getLabel() |
|
489
|
|
|
])); |
|
490
|
1 |
|
return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
|
|
|
|
|
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
1 |
|
$redirectPage = 'crudList'; |
|
494
|
1 |
|
$redirectParameters = $this->getAfterDeleteRedirectParameters($request, $entity, $redirectPage); |
|
495
|
|
|
|
|
496
|
1 |
|
$this->session->getFlashBag()->add('success', $this->translator->trans('crudlex.delete.success', [ |
|
497
|
1 |
|
'%label%' => $crudData->getDefinition()->getLabel() |
|
498
|
|
|
])); |
|
499
|
1 |
|
return new RedirectResponse($this->service->generateURL($redirectPage, $redirectParameters)); |
|
|
|
|
|
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* The controller for the "render file" action. |
|
504
|
|
|
* |
|
505
|
|
|
* @param string $entity |
|
506
|
|
|
* the current entity |
|
507
|
|
|
* @param string $id |
|
508
|
|
|
* the instance id |
|
509
|
|
|
* @param string $field |
|
510
|
|
|
* the field of the file to render of the instance |
|
511
|
|
|
* |
|
512
|
|
|
* @return Response |
|
513
|
|
|
* the rendered file |
|
514
|
|
|
*/ |
|
515
|
1 |
|
public function renderFile($entity, $id, $field) |
|
516
|
|
|
{ |
|
517
|
1 |
|
$crudData = $this->service->getData($entity); |
|
518
|
1 |
|
$instance = $crudData->get($id); |
|
519
|
1 |
|
$definition = $crudData->getDefinition(); |
|
520
|
1 |
|
if (!$instance || $definition->getType($field) != 'file' || !$instance->get($field)) { |
|
521
|
1 |
|
return $this->getNotFoundPage($this->translator->trans('crudlex.instanceNotFound')); |
|
522
|
|
|
} |
|
523
|
1 |
|
$fileHandler = new FileHandler($this->filesystem, $definition); |
|
524
|
1 |
|
return $fileHandler->renderFile($instance, $entity, $field); |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
/** |
|
528
|
|
|
* The controller for the "delete file" action. |
|
529
|
|
|
* |
|
530
|
|
|
* @param string $entity |
|
531
|
|
|
* the current entity |
|
532
|
|
|
* @param string $id |
|
533
|
|
|
* the instance id |
|
534
|
|
|
* @param string $field |
|
535
|
|
|
* the field of the file to delete of the instance |
|
536
|
|
|
* |
|
537
|
|
|
* @return Response |
|
538
|
|
|
* redirects to the instance details page or 404 on invalid input |
|
539
|
|
|
*/ |
|
540
|
1 |
|
public function deleteFile($entity, $id, $field) |
|
541
|
|
|
{ |
|
542
|
1 |
|
$crudData = $this->service->getData($entity); |
|
543
|
1 |
|
$instance = $crudData->get($id); |
|
544
|
1 |
|
if (!$instance) { |
|
545
|
1 |
|
return $this->getNotFoundPage($this->translator->trans('crudlex.instanceNotFound')); |
|
546
|
|
|
} |
|
547
|
1 |
|
$fileHandler = new FileHandler($this->filesystem, $crudData->getDefinition()); |
|
548
|
1 |
|
if (!$crudData->getDefinition()->getField($field, 'required', false) && $fileHandler->deleteFile($crudData, $instance, $entity, $field)) { |
|
549
|
1 |
|
$instance->set($field, ''); |
|
550
|
1 |
|
$crudData->update($instance); |
|
551
|
1 |
|
$this->session->getFlashBag()->add('success', $this->translator->trans('crudlex.file.deleted')); |
|
552
|
|
|
} else { |
|
553
|
1 |
|
$this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.file.notDeleted')); |
|
554
|
|
|
} |
|
555
|
1 |
|
return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
|
|
|
|
|
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
/** |
|
559
|
|
|
* The controller for serving static files. |
|
560
|
|
|
* |
|
561
|
|
|
* @param Request $request |
|
562
|
|
|
* the current request |
|
563
|
|
|
* |
|
564
|
|
|
* @return Response |
|
565
|
|
|
* redirects to the instance details page or 404 on invalid input |
|
566
|
|
|
*/ |
|
567
|
1 |
|
public function staticFile(Request $request) |
|
568
|
|
|
{ |
|
569
|
1 |
|
$fileParam = str_replace('..', '', $request->get('file')); |
|
570
|
1 |
|
$file = __DIR__.'/../static/'.$fileParam; |
|
571
|
1 |
|
if (!$fileParam || !file_exists($file)) { |
|
572
|
1 |
|
return $this->getNotFoundPage($this->translator->trans('crudlex.resourceNotFound')); |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
1 |
|
$mimeType = MimeType::detectByFilename($file); |
|
576
|
1 |
|
$size = filesize($file); |
|
577
|
|
|
|
|
578
|
1 |
|
$streamedFileResponse = new StreamedFileResponse(); |
|
579
|
1 |
|
$response = new StreamedResponse($streamedFileResponse->getStreamedFileFunction($file), 200, [ |
|
580
|
1 |
|
'Content-Type' => $mimeType, |
|
581
|
1 |
|
'Content-Disposition' => 'attachment; filename="'.basename($file).'"', |
|
582
|
1 |
|
'Content-length' => $size |
|
583
|
|
|
]); |
|
584
|
|
|
|
|
585
|
1 |
|
$response->setETag(filemtime($file))->setPublic()->isNotModified($request); |
|
586
|
1 |
|
$response->send(); |
|
587
|
|
|
|
|
588
|
1 |
|
return $response; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* The controller for setting the locale. |
|
593
|
|
|
* |
|
594
|
|
|
* @param Request $request |
|
595
|
|
|
* the current request |
|
596
|
|
|
* @param string $locale |
|
597
|
|
|
* the new locale |
|
598
|
|
|
* |
|
599
|
|
|
* @return Response |
|
600
|
|
|
* redirects to the instance details page or 404 on invalid input |
|
601
|
|
|
*/ |
|
602
|
1 |
|
public function setLocale(Request $request, $locale) |
|
603
|
|
|
{ |
|
604
|
|
|
|
|
605
|
1 |
|
if (!in_array($locale, $this->service->getLocales())) { |
|
606
|
1 |
|
return $this->getNotFoundPage('Locale '.$locale.' not found.'); |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
1 |
|
$manageI18n = $this->service->isManageI18n(); |
|
610
|
1 |
|
if ($manageI18n) { |
|
611
|
1 |
|
$this->session->set('locale', $locale); |
|
612
|
|
|
} |
|
613
|
1 |
|
$redirect = $request->get('redirect'); |
|
614
|
1 |
|
return new RedirectResponse($redirect); |
|
|
|
|
|
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
|
$this->service->generate... $entity, 'id' => $id))can contain request data and is used in output context(s) leading to a potential security vulnerability.8 paths for user data to reach this point
$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assigned in ServerBag.php on line 43$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 43
$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$valuesis assignedin vendor/HeaderBag.php on line 29
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headersis assignedin vendor/HeaderBag.php on line 113
$hostis assignedin vendor/Request.php on line 1273
$hostis passed through trim(), andtrim($host)is passed through preg_replace(), andpreg_replace('/:\\d+$/', '', trim($host))is passed through strtolower(), and$hostis assignedin vendor/Request.php on line 1281
$request->getHost()is passed to RequestContext::setHost()in vendor/RequestContext.php on line 68
$hostis passed through strtolower(), and RequestContext::$host is assignedin vendor/RequestContext.php on line 172
in vendor/RequestContext.php on line 160
$hostis assignedin vendor/Generator/UrlGenerator.php on line 184
$schemeAuthorityis assignedin vendor/Generator/UrlGenerator.php on line 233
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assigned in ServerBag.php on line 44$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$valuesis assignedin vendor/HeaderBag.php on line 29
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headersis assignedin vendor/HeaderBag.php on line 113
$hostis assignedin vendor/Request.php on line 1273
$hostis passed through trim(), andtrim($host)is passed through preg_replace(), andpreg_replace('/:\\d+$/', '', trim($host))is passed through strtolower(), and$hostis assignedin vendor/Request.php on line 1281
$request->getHost()is passed to RequestContext::setHost()in vendor/RequestContext.php on line 68
$hostis passed through strtolower(), and RequestContext::$host is assignedin vendor/RequestContext.php on line 172
in vendor/RequestContext.php on line 160
$hostis assignedin vendor/Generator/UrlGenerator.php on line 184
$schemeAuthorityis assignedin vendor/Generator/UrlGenerator.php on line 233
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$this->parameters['HTTP_AUTHORIZATION']seems to return tainted data, and$authorizationHeaderis assigned in ServerBag.php on line 62$this->parameters['HTTP_AUTHORIZATION']seems to return tainted data, and$authorizationHeaderis assignedin vendor/ServerBag.php on line 62
in vendor/ServerBag.php on line 77
in vendor/ParameterBag.php on line 84
$baseUrlis assignedin vendor/Request.php on line 1877
$baseUrlis passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$_POST,and$_POSTis passed to Request::createRequestFromFactory() in Request.php on line 314$_POST,and$_POSTis passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$requestis passed to Request::__construct()in vendor/Request.php on line 2068
$requestis passed to Request::initialize()in vendor/Request.php on line 255
$requestis passed to ParameterBag::__construct()in vendor/Request.php on line 273
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrlis assignedin vendor/Request.php on line 1877
$baseUrlis passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$_SERVER,and$serveris assigned in Request.php on line 304$_SERVER,and$serveris assignedin vendor/Request.php on line 304
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrlis assignedin vendor/Request.php on line 1877
$baseUrlis passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assigned in Request.php on line 307HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 307
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrlis assignedin vendor/Request.php on line 1877
$baseUrlis passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assigned in Request.php on line 310HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 310
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrlis assignedin vendor/Request.php on line 1877
$baseUrlis passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$server['HTTP_HOST']seems to return tainted data, and$serveris assigned in Request.php on line 380$server['HTTP_HOST']seems to return tainted data, and$serveris assignedin vendor/Request.php on line 380
$serveris assignedin vendor/Request.php on line 428
$serveris assignedin vendor/Request.php on line 429
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 431
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrlis assignedin vendor/Request.php on line 1877
$baseUrlis passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$urlis assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
Used in output context
in vendor/RedirectResponse.php on line 39
in vendor/RedirectResponse.php on line 92
in vendor/Response.php on line 391
in vendor/Response.php on line 350
Preventing Cross-Site-Scripting Attacks
Cross-Site-Scripting allows an attacker to inject malicious code into your website - in particular Javascript code, and have that code executed with the privileges of a visiting user. This can be used to obtain data, or perform actions on behalf of that visiting user.
In order to prevent this, make sure to escape all user-provided data:
General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: