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 array |
34
|
|
|
*/ |
35
|
|
|
private $entityConfig; |
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->entityConfig = $config['entities']; |
59
|
|
|
$this->tablePrefix = $config['table_prefix']; |
60
|
|
|
$this->tableSuffix = $config['table_suffix']; |
61
|
|
|
$this->ignoredColumns = $config['ignored_columns']; |
62
|
|
|
|
63
|
|
|
$this->setEntityDefault(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function setEntityDefault() { |
67
|
|
|
if (isset($this->entityConfig) && !empty($this->entityConfig)) { |
68
|
|
|
// use entity names as array keys for easier lookup |
69
|
|
|
foreach ($this->entityConfig as $auditedEntity => $entityOptions) { |
70
|
|
|
$this->entities[$auditedEntity] = $entityOptions; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the value of entities. |
77
|
|
|
* |
78
|
|
|
* @param array $entities |
79
|
|
|
*/ |
80
|
|
|
public function setEntities(array $entities): void |
81
|
|
|
{ |
82
|
|
|
$this->entities = $entities; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns true if $entity is audited. |
87
|
|
|
* |
88
|
|
|
* @param object|string $entity |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function isAudited($entity): bool |
93
|
|
|
{ |
94
|
|
|
$class = DoctrineHelper::getRealClass($entity); |
95
|
|
|
|
96
|
|
|
// is $entity part of audited entities? |
97
|
|
|
if (!array_key_exists($class, $this->entities)) { |
98
|
|
|
// no => $entity is not audited |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$entityOptions = $this->entities[$class]; |
103
|
|
|
|
104
|
|
|
if (null === $entityOptions) { |
105
|
|
|
// no option defined => $entity is audited |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (isset($entityOptions['enabled'])) { |
110
|
|
|
return (bool) $entityOptions['enabled']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns true if $field is audited. |
118
|
|
|
* |
119
|
|
|
* @param object|string $entity |
120
|
|
|
* @param string $field |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isAuditedField($entity, string $field): bool |
125
|
|
|
{ |
126
|
|
|
// is $field is part of globally ignored columns? |
127
|
|
|
if (\in_array($field, $this->ignoredColumns, true)) { |
128
|
|
|
// yes => $field is not audited |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// is $entity audited? |
133
|
|
|
if (!$this->isAudited($entity)) { |
134
|
|
|
// no => $field is not audited |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$class = DoctrineHelper::getRealClass($entity); |
139
|
|
|
$entityOptions = $this->entities[$class]; |
140
|
|
|
|
141
|
|
|
if (null === $entityOptions) { |
142
|
|
|
// no option defined => $field is audited |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// are columns excluded and is field part of them? |
147
|
|
|
if (isset($entityOptions['ignored_columns']) && |
148
|
|
|
\in_array($field, $entityOptions['ignored_columns'], true)) { |
149
|
|
|
// yes => $field is not audited |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the value of tablePrefix. |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getTablePrefix(): string |
162
|
|
|
{ |
163
|
|
|
return $this->tablePrefix; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the value of tableSuffix. |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getTableSuffix(): string |
172
|
|
|
{ |
173
|
|
|
return $this->tableSuffix; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the value of excludedColumns. |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public function getIgnoredColumns(): array |
182
|
|
|
{ |
183
|
|
|
return $this->ignoredColumns; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the value of entities. |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getEntities(): array |
192
|
|
|
{ |
193
|
|
|
return $this->entities; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Enables auditing for a specific entity. |
198
|
|
|
* |
199
|
|
|
* @param string $entity Entity class name |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function enableAuditFor(string $entity): self |
204
|
|
|
{ |
205
|
|
|
if (isset($this->entities[$entity])) { |
206
|
|
|
$this->entities[$entity]['enabled'] = true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Disables auditing for a specific entity. |
214
|
|
|
* |
215
|
|
|
* @param string $entity Entity class name |
216
|
|
|
* |
217
|
|
|
* @return $this |
218
|
|
|
*/ |
219
|
|
|
public function disableAuditFor(string $entity): self |
220
|
|
|
{ |
221
|
|
|
if (isset($this->entities[$entity])) { |
222
|
|
|
$this->entities[$entity]['enabled'] = false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Enables/Disables auditing for all entities or restore default configuration. |
230
|
|
|
* |
231
|
|
|
* @param bool|string enabled: true/false/defalt |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function enableAudit($enabled = 'default'): self |
236
|
|
|
{ |
237
|
|
|
// set ALL entities to enabled = true/false |
238
|
|
|
if ($enabled === true || $enabled === false) { |
239
|
|
|
foreach (array_keys($this->entities) as $key) { |
240
|
|
|
$this->entities[$key]['enabled'] = $enabled; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// restore default entities config |
247
|
|
|
$this->setEntityDefault(); |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get the value of userProvider. |
254
|
|
|
* |
255
|
|
|
* @return UserProviderInterface |
256
|
|
|
*/ |
257
|
|
|
public function getUserProvider(): ?UserProviderInterface |
258
|
|
|
{ |
259
|
|
|
return $this->userProvider; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get the value of requestStack. |
264
|
|
|
* |
265
|
|
|
* @return RequestStack |
266
|
|
|
*/ |
267
|
|
|
public function getRequestStack(): RequestStack |
268
|
|
|
{ |
269
|
|
|
return $this->requestStack; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Gets the value of firewallMap. |
274
|
|
|
* |
275
|
|
|
* @return FirewallMap |
276
|
|
|
*/ |
277
|
|
|
public function getFirewallMap(): FirewallMap |
278
|
|
|
{ |
279
|
|
|
return $this->firewallMap; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|