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