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