1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\DoctrineAuditBundle; |
4
|
|
|
|
5
|
|
|
use DH\DoctrineAuditBundle\Helper\DoctrineHelper; |
6
|
|
|
use DH\DoctrineAuditBundle\User\UserProviderInterface; |
7
|
|
|
use Symfony\Bundle\SecurityBundle\Security\FirewallMap; |
8
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
9
|
|
|
|
10
|
|
|
class AuditConfiguration |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $tablePrefix; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $tableSuffix; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $ignoredColumns = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $entities = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
private $enabled = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var UserProviderInterface |
39
|
|
|
*/ |
40
|
|
|
protected $userProvider; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var RequestStack |
44
|
|
|
*/ |
45
|
|
|
protected $requestStack; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var FirewallMap |
49
|
|
|
*/ |
50
|
|
|
private $firewallMap; |
51
|
|
|
|
52
|
|
|
public function __construct(array $config, UserProviderInterface $userProvider, RequestStack $requestStack, FirewallMap $firewallMap) |
53
|
|
|
{ |
54
|
|
|
$this->userProvider = $userProvider; |
55
|
|
|
$this->requestStack = $requestStack; |
56
|
|
|
$this->firewallMap = $firewallMap; |
57
|
|
|
|
58
|
|
|
$this->tablePrefix = $config['table_prefix']; |
59
|
|
|
$this->tableSuffix = $config['table_suffix']; |
60
|
|
|
$this->ignoredColumns = $config['ignored_columns']; |
61
|
|
|
|
62
|
|
|
if (isset($config['entities']) && !empty($config['entities'])) { |
63
|
|
|
// use entity names as array keys for easier lookup |
64
|
|
|
foreach ($config['entities'] as $auditedEntity => $entityOptions) { |
65
|
|
|
$this->entities[$auditedEntity] = $entityOptions; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the value of entities. |
72
|
|
|
* |
73
|
|
|
* @param array $entities |
74
|
|
|
*/ |
75
|
|
|
public function setEntities(array $entities): void |
76
|
|
|
{ |
77
|
|
|
$this->entities = $entities; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* enabled audit. |
82
|
|
|
* |
83
|
|
|
* @param bool $enabled |
84
|
|
|
*/ |
85
|
|
|
public function enabled(): self |
86
|
|
|
{ |
87
|
|
|
$this->enabled = true; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* disable audit. |
94
|
|
|
* |
95
|
|
|
* @param bool $enabled |
96
|
|
|
*/ |
97
|
|
|
public function disable(): self |
98
|
|
|
{ |
99
|
|
|
$this->enabled = false; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get enabled flag. |
106
|
|
|
* |
107
|
|
|
* @param bool $enabled |
108
|
|
|
*/ |
109
|
|
|
public function getEnabled(): bool |
110
|
|
|
{ |
111
|
|
|
return $this->enabled; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns true if $entity is audited. |
116
|
|
|
* |
117
|
|
|
* @param object|string $entity |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isAuditable($entity): bool |
122
|
|
|
{ |
123
|
|
|
$class = DoctrineHelper::getRealClass($entity); |
124
|
|
|
|
125
|
|
|
// is $entity part of audited entities? |
126
|
|
|
if (!array_key_exists($class, $this->entities)) { |
127
|
|
|
// no => $entity is not audited |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns true if $entity is audited. |
136
|
|
|
* |
137
|
|
|
* @param object|string $entity |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function isAudited($entity): bool |
142
|
|
|
{ |
143
|
|
|
if (!$this->enabled) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$class = DoctrineHelper::getRealClass($entity); |
148
|
|
|
|
149
|
|
|
// is $entity part of audited entities? |
150
|
|
|
if (!array_key_exists($class, $this->entities)) { |
151
|
|
|
// no => $entity is not audited |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$entityOptions = $this->entities[$class]; |
156
|
|
|
|
157
|
|
|
if (null === $entityOptions) { |
158
|
|
|
// no option defined => $entity is audited |
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (isset($entityOptions['enabled'])) { |
163
|
|
|
return (bool) $entityOptions['enabled']; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns true if $field is audited. |
171
|
|
|
* |
172
|
|
|
* @param object|string $entity |
173
|
|
|
* @param string $field |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function isAuditedField($entity, string $field): bool |
178
|
|
|
{ |
179
|
|
|
// is $field is part of globally ignored columns? |
180
|
|
|
if (\in_array($field, $this->ignoredColumns, true)) { |
181
|
|
|
// yes => $field is not audited |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// is $entity audited? |
186
|
|
|
if (!$this->isAudited($entity)) { |
187
|
|
|
// no => $field is not audited |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$class = DoctrineHelper::getRealClass($entity); |
192
|
|
|
$entityOptions = $this->entities[$class]; |
193
|
|
|
|
194
|
|
|
if (null === $entityOptions) { |
195
|
|
|
// no option defined => $field is audited |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// are columns excluded and is field part of them? |
200
|
|
|
if (isset($entityOptions['ignored_columns']) && |
201
|
|
|
\in_array($field, $entityOptions['ignored_columns'], true)) { |
202
|
|
|
// yes => $field is not audited |
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the value of tablePrefix. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getTablePrefix(): string |
215
|
|
|
{ |
216
|
|
|
return $this->tablePrefix; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the value of tableSuffix. |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function getTableSuffix(): string |
225
|
|
|
{ |
226
|
|
|
return $this->tableSuffix; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get the value of excludedColumns. |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
public function getIgnoredColumns(): array |
235
|
|
|
{ |
236
|
|
|
return $this->ignoredColumns; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get the value of entities. |
241
|
|
|
* |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
|
|
public function getEntities(): array |
245
|
|
|
{ |
246
|
|
|
return $this->entities; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Enables auditing for a specific entity. |
251
|
|
|
* |
252
|
|
|
* @param string $entity Entity class name |
253
|
|
|
* |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
|
|
public function enableAuditFor(string $entity): self |
257
|
|
|
{ |
258
|
|
|
if (isset($this->entities[$entity])) { |
259
|
|
|
$this->entities[$entity]['enabled'] = true; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Disables auditing for a specific entity. |
267
|
|
|
* |
268
|
|
|
* @param string $entity Entity class name |
269
|
|
|
* |
270
|
|
|
* @return $this |
271
|
|
|
*/ |
272
|
|
|
public function disableAuditFor(string $entity): self |
273
|
|
|
{ |
274
|
|
|
if (isset($this->entities[$entity])) { |
275
|
|
|
$this->entities[$entity]['enabled'] = false; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get the value of userProvider. |
283
|
|
|
* |
284
|
|
|
* @return UserProviderInterface |
285
|
|
|
*/ |
286
|
|
|
public function getUserProvider(): ?UserProviderInterface |
287
|
|
|
{ |
288
|
|
|
return $this->userProvider; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get the value of requestStack. |
293
|
|
|
* |
294
|
|
|
* @return RequestStack |
295
|
|
|
*/ |
296
|
|
|
public function getRequestStack(): RequestStack |
297
|
|
|
{ |
298
|
|
|
return $this->requestStack; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Gets the value of firewallMap. |
303
|
|
|
* |
304
|
|
|
* @return FirewallMap |
305
|
|
|
*/ |
306
|
|
|
public function getFirewallMap(): FirewallMap |
307
|
|
|
{ |
308
|
|
|
return $this->firewallMap; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|