1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\DoctrineAuditBundle; |
4
|
|
|
|
5
|
|
|
use DH\DoctrineAuditBundle\Annotation\AnnotationLoader; |
6
|
|
|
use DH\DoctrineAuditBundle\Helper\DoctrineHelper; |
7
|
|
|
use DH\DoctrineAuditBundle\User\UserProviderInterface; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use ReflectionException; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use Symfony\Bundle\SecurityBundle\Security\FirewallMap; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
14
|
|
|
|
15
|
|
|
class AuditConfiguration |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var UserProviderInterface |
19
|
|
|
*/ |
20
|
|
|
protected $userProvider; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RequestStack |
24
|
|
|
*/ |
25
|
|
|
protected $requestStack; |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $tablePrefix; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $tableSuffix; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $timezone; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $ignoredColumns = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $entities = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
private $enabled = true; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
private $enabledViewer = true; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var FirewallMap |
63
|
|
|
*/ |
64
|
|
|
private $firewallMap; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var EntityManagerInterface |
68
|
|
|
*/ |
69
|
|
|
private $entityManager; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var null|AnnotationLoader |
73
|
|
|
*/ |
74
|
|
|
private $annotationLoader; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var EventDispatcherInterface |
78
|
|
|
*/ |
79
|
|
|
private $dispatcher; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var bool |
83
|
|
|
*/ |
84
|
|
|
private $is_pre43_dispatcher; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
private $annotationLoaded = false; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* AuditConfiguration constructor. |
93
|
|
|
* |
94
|
|
|
* @param array $config |
95
|
|
|
* @param UserProviderInterface $userProvider |
96
|
|
|
* @param RequestStack $requestStack |
97
|
|
|
* @param FirewallMap $firewallMap |
98
|
|
|
* @param EntityManagerInterface $entityManager |
99
|
|
|
* @param null|AnnotationLoader $annotationLoader |
100
|
|
|
* @param EventDispatcherInterface $dispatcher |
101
|
|
|
* |
102
|
|
|
* @throws ReflectionException |
103
|
|
|
*/ |
104
|
|
|
public function __construct( |
105
|
|
|
array $config, |
106
|
|
|
UserProviderInterface $userProvider, |
107
|
|
|
RequestStack $requestStack, |
108
|
|
|
FirewallMap $firewallMap, |
109
|
|
|
EntityManagerInterface $entityManager, |
110
|
|
|
?AnnotationLoader $annotationLoader, |
111
|
|
|
EventDispatcherInterface $dispatcher |
112
|
|
|
) { |
113
|
|
|
$this->userProvider = $userProvider; |
114
|
|
|
$this->requestStack = $requestStack; |
115
|
|
|
$this->firewallMap = $firewallMap; |
116
|
|
|
$this->entityManager = $entityManager; |
117
|
|
|
$this->annotationLoader = $annotationLoader; |
118
|
|
|
$this->dispatcher = $dispatcher; |
119
|
|
|
|
120
|
|
|
$r = new ReflectionMethod($this->dispatcher, 'dispatch'); |
121
|
|
|
$p = $r->getParameters(); |
122
|
|
|
$this->is_pre43_dispatcher = 2 === \count($p) && 'event' !== $p[0]->name; |
123
|
|
|
|
124
|
|
|
$this->enabled = $config['enabled']; |
125
|
|
|
$this->enabledViewer = $config['enabled_viewer']; |
126
|
|
|
$this->tablePrefix = $config['table_prefix']; |
127
|
|
|
$this->tableSuffix = $config['table_suffix']; |
128
|
|
|
$this->timezone = $config['timezone']; |
129
|
|
|
$this->ignoredColumns = $config['ignored_columns']; |
130
|
|
|
|
131
|
|
|
if (isset($config['entities']) && !empty($config['entities'])) { |
132
|
|
|
// use entity names as array keys for easier lookup |
133
|
|
|
foreach ($config['entities'] as $auditedEntity => $entityOptions) { |
134
|
|
|
$this->entities[$auditedEntity] = $entityOptions; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function isPre43Dispatcher(): bool |
140
|
|
|
{ |
141
|
|
|
return $this->is_pre43_dispatcher; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the value of entities. |
146
|
|
|
* |
147
|
|
|
* This method completely overrides entities configuration |
148
|
|
|
* including annotation configuration |
149
|
|
|
* |
150
|
|
|
* @param array<string, array> $entities |
151
|
|
|
*/ |
152
|
|
|
public function setEntities(array $entities): void |
153
|
|
|
{ |
154
|
|
|
$this->annotationLoaded = true; |
155
|
|
|
$this->entities = $entities; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* enabled audit. |
160
|
|
|
* |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function enable(): self |
164
|
|
|
{ |
165
|
|
|
$this->enabled = true; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* disable audit. |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function disable(): self |
176
|
|
|
{ |
177
|
|
|
$this->enabled = false; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get enabled flag. |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function isEnabled(): bool |
188
|
|
|
{ |
189
|
|
|
return $this->enabled; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* enable audit Controller and its routing. |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
|
|
public function enableViewer(): self |
198
|
|
|
{ |
199
|
|
|
$this->enabledViewer = true; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* disable audit Controller and its routing. |
206
|
|
|
* |
207
|
|
|
* @return $this |
208
|
|
|
*/ |
209
|
|
|
public function disableViewer(): self |
210
|
|
|
{ |
211
|
|
|
$this->enabledViewer = false; |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get enabled flag. |
218
|
|
|
* |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
|
|
public function isEnabledViewer(): bool |
222
|
|
|
{ |
223
|
|
|
return $this->enabledViewer; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns true if $entity is auditable. |
228
|
|
|
* |
229
|
|
|
* @param object|string $entity |
230
|
|
|
* |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function isAuditable($entity): bool |
234
|
|
|
{ |
235
|
|
|
$class = DoctrineHelper::getRealClassName($entity); |
236
|
|
|
|
237
|
|
|
// is $entity part of audited entities? |
238
|
|
|
if (!\array_key_exists($class, $this->getEntities())) { |
239
|
|
|
// no => $entity is not audited |
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return true; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns true if $entity is audited. |
248
|
|
|
* |
249
|
|
|
* @param object|string $entity |
250
|
|
|
* |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function isAudited($entity): bool |
254
|
|
|
{ |
255
|
|
|
if (!$this->enabled) { |
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$class = DoctrineHelper::getRealClassName($entity); |
260
|
|
|
|
261
|
|
|
// is $entity part of audited entities? |
262
|
|
|
if (!\array_key_exists($class, $this->getEntities())) { |
263
|
|
|
// no => $entity is not audited |
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$entityOptions = $this->getEntities()[$class]; |
268
|
|
|
|
269
|
|
|
if (null === $entityOptions) { |
270
|
|
|
// no option defined => $entity is audited |
271
|
|
|
return true; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if (isset($entityOptions['enabled'])) { |
275
|
|
|
return (bool) $entityOptions['enabled']; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return true; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Returns true if $field is audited. |
283
|
|
|
* |
284
|
|
|
* @param object|string $entity |
285
|
|
|
* @param string $field |
286
|
|
|
* |
287
|
|
|
* @return bool |
288
|
|
|
*/ |
289
|
|
|
public function isAuditedField($entity, string $field): bool |
290
|
|
|
{ |
291
|
|
|
// is $field is part of globally ignored columns? |
292
|
|
|
if (\in_array($field, $this->ignoredColumns, true)) { |
293
|
|
|
// yes => $field is not audited |
294
|
|
|
return false; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// is $entity audited? |
298
|
|
|
if (!$this->isAudited($entity)) { |
299
|
|
|
// no => $field is not audited |
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$class = DoctrineHelper::getRealClassName($entity); |
304
|
|
|
$entityOptions = $this->getEntities()[$class]; |
305
|
|
|
|
306
|
|
|
if (null === $entityOptions) { |
307
|
|
|
// no option defined => $field is audited |
308
|
|
|
return true; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
// are columns excluded and is field part of them? |
312
|
|
|
if (isset($entityOptions['ignored_columns']) && |
313
|
|
|
\in_array($field, $entityOptions['ignored_columns'], true)) { |
314
|
|
|
// yes => $field is not audited |
315
|
|
|
return false; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return true; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Get the value of tablePrefix. |
323
|
|
|
* |
324
|
|
|
* @return string |
325
|
|
|
*/ |
326
|
|
|
public function getTablePrefix(): string |
327
|
|
|
{ |
328
|
|
|
return $this->tablePrefix; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get the value of tableSuffix. |
333
|
|
|
* |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
public function getTableSuffix(): string |
337
|
|
|
{ |
338
|
|
|
return $this->tableSuffix; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Get the value of timezone. |
343
|
|
|
* |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
public function getTimezone(): string |
347
|
|
|
{ |
348
|
|
|
return $this->timezone; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Get the value of excludedColumns. |
353
|
|
|
* |
354
|
|
|
* @return array<string> |
355
|
|
|
*/ |
356
|
|
|
public function getIgnoredColumns(): array |
357
|
|
|
{ |
358
|
|
|
return $this->ignoredColumns; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Get the value of entities. |
363
|
|
|
* |
364
|
|
|
* @return array |
365
|
|
|
*/ |
366
|
|
|
public function getEntities(): array |
367
|
|
|
{ |
368
|
|
|
if (false === $this->annotationLoaded && null !== $this->annotationLoader) { |
369
|
|
|
// Update config using annotations |
370
|
|
|
$config = $this->annotationLoader->load(); |
371
|
|
|
$this->entities = array_merge($this->entities, $config); |
372
|
|
|
$this->annotationLoaded = true; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
return $this->entities; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Enables auditing for a specific entity. |
380
|
|
|
* |
381
|
|
|
* @param string $entity Entity class name |
382
|
|
|
* |
383
|
|
|
* @return $this |
384
|
|
|
*/ |
385
|
|
|
public function enableAuditFor(string $entity): self |
386
|
|
|
{ |
387
|
|
|
if (isset($this->getEntities()[$entity])) { |
388
|
|
|
$this->entities[$entity]['enabled'] = true; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $this; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Disables auditing for a specific entity. |
396
|
|
|
* |
397
|
|
|
* @param string $entity Entity class name |
398
|
|
|
* |
399
|
|
|
* @return $this |
400
|
|
|
*/ |
401
|
|
|
public function disableAuditFor(string $entity): self |
402
|
|
|
{ |
403
|
|
|
if (isset($this->getEntities()[$entity])) { |
404
|
|
|
$this->entities[$entity]['enabled'] = false; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
return $this; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Get the value of userProvider. |
412
|
|
|
* |
413
|
|
|
* @return UserProviderInterface |
414
|
|
|
*/ |
415
|
|
|
public function getUserProvider(): ?UserProviderInterface |
416
|
|
|
{ |
417
|
|
|
return $this->userProvider; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Get the value of requestStack. |
422
|
|
|
* |
423
|
|
|
* @return RequestStack |
424
|
|
|
*/ |
425
|
|
|
public function getRequestStack(): RequestStack |
426
|
|
|
{ |
427
|
|
|
return $this->requestStack; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Gets the value of firewallMap. |
432
|
|
|
* |
433
|
|
|
* @return FirewallMap |
434
|
|
|
*/ |
435
|
|
|
public function getFirewallMap(): FirewallMap |
436
|
|
|
{ |
437
|
|
|
return $this->firewallMap; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @return EntityManagerInterface |
442
|
|
|
*/ |
443
|
|
|
public function getEntityManager(): EntityManagerInterface |
444
|
|
|
{ |
445
|
|
|
return $this->entityManager; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @return null|AnnotationLoader |
450
|
|
|
*/ |
451
|
|
|
public function getAnnotationLoader(): ?AnnotationLoader |
452
|
|
|
{ |
453
|
|
|
return $this->annotationLoader; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* @return EventDispatcherInterface |
458
|
|
|
*/ |
459
|
|
|
public function getEventDispatcher(): EventDispatcherInterface |
460
|
|
|
{ |
461
|
|
|
return $this->dispatcher; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|