1
|
|
|
<?php |
2
|
|
|
namespace bedezign\yii2\audit; |
3
|
|
|
|
4
|
|
|
use Yii; |
5
|
|
|
use yii\base\Behavior; |
6
|
|
|
use yii\db\ActiveRecord; |
7
|
|
|
use bedezign\yii2\audit\models\AuditTrail; |
8
|
|
|
use yii\db\Query; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AuditTrailBehavior |
12
|
|
|
* @package bedezign\yii2\audit |
13
|
|
|
* |
14
|
|
|
* @property \yii\db\ActiveRecord $owner |
15
|
|
|
*/ |
16
|
|
|
class AuditTrailBehavior extends Behavior |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Array with fields to save |
21
|
|
|
* You don't need to configure both `allowed` and `ignored` |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
public $allowed = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array with fields to ignore |
28
|
|
|
* You don't need to configure both `allowed` and `ignored` |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $ignored = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Array with classes to ignore |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $ignoredClasses = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Is the behavior is active or not |
41
|
|
|
* @var boolean |
42
|
|
|
*/ |
43
|
|
|
public $active = true; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Date format to use in stamp - set to "Y-m-d H:i:s" for datetime or "U" for timestamp |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $dateFormat = 'Y-m-d H:i:s'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $_oldAttributes = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Array with fields you want to override before saving the row into audit_trail table |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
304 |
|
public $override = []; |
61
|
|
|
|
62
|
|
|
|
63
|
304 |
|
/** |
64
|
304 |
|
* @inheritdoc |
65
|
304 |
|
*/ |
66
|
304 |
|
public function events() |
67
|
114 |
|
{ |
68
|
|
|
return [ |
69
|
|
|
ActiveRecord::EVENT_AFTER_FIND => 'afterFind', |
70
|
|
|
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', |
71
|
|
|
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', |
72
|
|
|
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
73
|
37 |
|
]; |
74
|
|
|
} |
75
|
15 |
|
|
76
|
37 |
|
/** |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
public function afterFind() |
80
|
|
|
{ |
81
|
24 |
|
$this->setOldAttributes($this->owner->getAttributes()); |
82
|
|
|
} |
83
|
8 |
|
|
84
|
8 |
|
/** |
85
|
24 |
|
* |
86
|
|
|
*/ |
87
|
|
|
public function afterInsert() |
88
|
|
|
{ |
89
|
|
|
$this->audit('CREATE'); |
90
|
23 |
|
$this->setOldAttributes($this->owner->getAttributes()); |
91
|
|
|
} |
92
|
9 |
|
|
93
|
9 |
|
/** |
94
|
23 |
|
* |
95
|
|
|
*/ |
96
|
|
|
public function afterUpdate() |
97
|
|
|
{ |
98
|
|
|
$this->audit('UPDATE'); |
99
|
12 |
|
$this->setOldAttributes($this->owner->getAttributes()); |
100
|
|
|
} |
101
|
4 |
|
|
102
|
4 |
|
/** |
103
|
12 |
|
* |
104
|
|
|
*/ |
105
|
|
|
public function afterDelete() |
106
|
|
|
{ |
107
|
|
|
$this->audit('DELETE'); |
108
|
|
|
$this->setOldAttributes([]); |
109
|
53 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
53 |
|
* @param $action |
113
|
9 |
|
* @throws \yii\db\Exception |
114
|
|
|
*/ |
115
|
|
|
public function audit($action) |
116
|
16 |
|
{ |
117
|
9 |
|
// Not active? get out of here |
118
|
|
|
if (!$this->active) { |
119
|
|
|
return; |
120
|
35 |
|
} |
121
|
2 |
|
// Lets check if the whole class should be ignored |
122
|
6 |
|
if (sizeof($this->ignoredClasses) > 0 && array_search(get_class($this->owner), $this->ignoredClasses) !== false) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
11 |
|
// If this is a delete then just write one row and get out of here |
126
|
29 |
|
if ($action == 'DELETE') { |
127
|
|
|
$this->saveAuditTrailDelete(); |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
// Now lets actually write the attributes |
131
|
|
|
$this->auditAttributes($action); |
132
|
|
|
} |
133
|
|
|
|
134
|
11 |
|
/** |
135
|
|
|
* Clean attributes of fields that are not allowed or ignored. |
136
|
11 |
|
* |
137
|
11 |
|
* @param $attributes |
138
|
11 |
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
protected function cleanAttributes($attributes) |
141
|
|
|
{ |
142
|
|
|
$attributes = $this->cleanAttributesAllowed($attributes); |
143
|
|
|
$attributes = $this->cleanAttributesIgnored($attributes); |
144
|
|
|
$attributes = $this->cleanAttributesOverride($attributes); |
145
|
|
|
return $attributes; |
146
|
|
|
} |
147
|
11 |
|
|
148
|
|
|
/** |
149
|
11 |
|
* Unset attributes which are not allowed |
150
|
2 |
|
* |
151
|
2 |
|
* @param $attributes |
152
|
2 |
|
* @return mixed |
153
|
2 |
|
*/ |
154
|
2 |
|
protected function cleanAttributesAllowed($attributes) |
155
|
2 |
|
{ |
156
|
11 |
|
if (sizeof($this->allowed) > 0) { |
157
|
|
|
foreach ($attributes as $f => $v) { |
158
|
|
|
if (array_search($f, $this->allowed) === false) { |
159
|
|
|
unset($attributes[$f]); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return $attributes; |
164
|
|
|
} |
165
|
11 |
|
|
166
|
|
|
/** |
167
|
11 |
|
* Unset attributes which are ignored |
168
|
2 |
|
* |
169
|
2 |
|
* @param $attributes |
170
|
2 |
|
* @return mixed |
171
|
2 |
|
*/ |
172
|
2 |
|
protected function cleanAttributesIgnored($attributes) |
173
|
2 |
|
{ |
174
|
11 |
|
if (sizeof($this->ignored) > 0) { |
175
|
|
|
foreach ($attributes as $f => $v) { |
176
|
|
|
if (array_search($f, $this->ignored) !== false) { |
177
|
|
|
unset($attributes[$f]); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
27 |
|
return $attributes; |
182
|
|
|
} |
183
|
|
|
|
184
|
11 |
|
/** |
185
|
11 |
|
* attributes which need to get override with a new value |
186
|
|
|
* |
187
|
11 |
|
* @param $attributes |
188
|
3 |
|
* @return mixed |
189
|
|
|
*/ |
190
|
|
|
protected function cleanAttributesOverride($attributes) |
191
|
10 |
|
{ |
192
|
10 |
|
if (sizeof($this->override) > 0 && sizeof($attributes) >0) { |
193
|
10 |
|
foreach ($this->override as $field => $queryParams) { |
194
|
10 |
|
$newOverrideValues = $this->getNewOverrideValues($attributes[$field], $queryParams); |
195
|
10 |
|
$saveField = \yii\helpers\ArrayHelper::getValue($queryParams, 'saveField', $field); |
196
|
|
|
|
197
|
10 |
|
if (count($newOverrideValues) >1) { |
198
|
26 |
|
$attributes[$saveField] = implode(', ', |
199
|
|
|
\yii\helpers\ArrayHelper::map($newOverrideValues, $queryParams['returnField'], $queryParams['returnField']) |
200
|
|
|
); |
201
|
|
|
} elseif (count($newOverrideValues) == 1) { |
202
|
|
|
$attributes[$saveField] = $newOverrideValues[0][$queryParams['returnField']]; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
return $attributes; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $searchFieldValue |
211
|
|
|
* @param string $queryParams |
212
|
|
|
* @return mixed |
213
|
26 |
|
*/ |
214
|
|
|
private function getNewOverrideValues($searchFieldValue, $queryParams) |
215
|
|
|
{ |
216
|
26 |
|
$query = new Query; |
217
|
10 |
|
|
218
|
14 |
|
$query->select($queryParams['returnField']) |
219
|
|
|
->from($queryParams['tableName']) |
220
|
14 |
|
->where([$queryParams['searchField'] => $searchFieldValue]); |
221
|
16 |
|
|
222
|
10 |
|
$rows = $query->all(); |
223
|
10 |
|
|
224
|
|
|
return $rows; |
225
|
26 |
|
} |
226
|
26 |
|
|
227
|
10 |
|
|
228
|
10 |
|
/** |
229
|
10 |
|
* @param string $action |
230
|
26 |
|
* @throws \yii\db\Exception |
231
|
|
|
*/ |
232
|
|
|
protected function auditAttributes($action) |
233
|
|
|
{ |
234
|
|
|
// Get the new and old attributes |
235
|
6 |
|
$newAttributes = $this->cleanAttributes($this->owner->getAttributes()); |
236
|
|
|
$oldAttributes = $this->cleanAttributes($this->getOldAttributes()); |
237
|
2 |
|
|
238
|
2 |
|
// ensure to handle serialized attributes properly |
239
|
6 |
|
foreach($newAttributes as $key => $value) |
240
|
6 |
|
if(is_array($newAttributes[$key])) |
241
|
6 |
|
$newAttributes[$key] = implode(',', $newAttributes[$key]); |
242
|
6 |
|
|
243
|
6 |
|
foreach($oldAttributes as $key => $value) |
244
|
6 |
|
if(is_array($oldAttributes[$key])) |
245
|
2 |
|
$oldAttributes[$key] = implode(',', $oldAttributes[$key]); |
246
|
6 |
|
|
247
|
|
|
// If no difference then get out of here |
248
|
|
|
if (count(array_diff_assoc($newAttributes, $oldAttributes)) <= 0) { |
249
|
|
|
return; |
250
|
|
|
} |
251
|
29 |
|
// Get the trail data |
252
|
|
|
$entry_id = $this->getAuditEntryId(); |
253
|
29 |
|
$user_id = $this->getUserId(); |
254
|
|
|
$model = $this->owner->className(); |
255
|
|
|
$model_id = $this->getNormalizedPk(); |
256
|
|
|
$created = date($this->dateFormat); |
257
|
|
|
|
258
|
|
|
$this->saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created); |
259
|
27 |
|
} |
260
|
|
|
|
261
|
27 |
|
/** |
262
|
27 |
|
* Save the audit trails for a create or update action |
263
|
|
|
* |
264
|
|
|
* @param $action |
265
|
|
|
* @param $newAttributes |
266
|
|
|
* @param $oldAttributes |
267
|
12 |
|
* @param $entry_id |
268
|
|
|
* @param $user_id |
269
|
12 |
|
* @param $model |
270
|
12 |
|
* @param $model_id |
271
|
|
|
* @param $created |
272
|
|
|
* @throws \yii\db\Exception |
273
|
|
|
*/ |
274
|
|
|
protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
275
|
|
|
{ |
276
|
12 |
|
// Build a list of fields to log |
277
|
|
|
$rows = array(); |
278
|
12 |
|
foreach ($newAttributes as $field => $new) { |
279
|
|
|
$old = isset($oldAttributes[$field]) ? $oldAttributes[$field] : ''; |
280
|
|
|
// If they are not the same lets write an audit log |
281
|
|
|
if ($new != $old) { |
282
|
|
|
$rows[] = [$entry_id, $user_id, $old, $new, $action, $model, $model_id, $field, $created]; |
283
|
|
|
} |
284
|
|
|
} |
285
|
32 |
|
// Record the field changes with a batch insert |
286
|
|
|
if (!empty($rows)) { |
287
|
12 |
|
$columns = ['entry_id', 'user_id', 'old_value', 'new_value', 'action', 'model', 'model_id', 'field', 'created']; |
288
|
32 |
|
$audit = Audit::getInstance(); |
289
|
|
|
$audit->getDb()->createCommand()->batchInsert(AuditTrail::tableName(), $columns, $rows)->execute(); |
290
|
|
|
} |
291
|
32 |
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
12 |
|
* Save the audit trails for a delete action |
295
|
|
|
*/ |
296
|
|
|
protected function saveAuditTrailDelete() |
297
|
|
|
{ |
298
|
|
|
$audit = Audit::getInstance(); |
299
|
|
|
$audit->getDb()->createCommand()->insert(AuditTrail::tableName(), [ |
300
|
|
|
'action' => 'DELETE', |
301
|
|
|
'entry_id' => $this->getAuditEntryId(), |
302
|
|
|
'user_id' => $this->getUserId(), |
303
|
|
|
'model' => $this->owner->className(), |
304
|
|
|
'model_id' => $this->getNormalizedPk(), |
305
|
|
|
'created' => date($this->dateFormat), |
306
|
|
|
])->execute(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return array |
311
|
|
|
*/ |
312
|
|
|
public function getOldAttributes() |
313
|
|
|
{ |
314
|
|
|
return $this->_oldAttributes; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param $value |
319
|
|
|
*/ |
320
|
|
|
public function setOldAttributes($value) |
321
|
|
|
{ |
322
|
|
|
$this->_oldAttributes = $value; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
|
|
protected function getNormalizedPk() |
329
|
|
|
{ |
330
|
|
|
$pk = $this->owner->getPrimaryKey(); |
331
|
|
|
return is_array($pk) ? json_encode($pk) : $pk; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return int|null|string |
336
|
|
|
*/ |
337
|
|
|
protected function getUserId() |
338
|
|
|
{ |
339
|
|
|
return Audit::getInstance()->getUserId(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @return models\AuditEntry|null|static |
344
|
|
|
* @throws \Exception |
345
|
|
|
*/ |
346
|
|
|
protected function getAuditEntryId() |
347
|
|
|
{ |
348
|
|
|
$module = Audit::getInstance(); |
349
|
|
|
if (!$module) { |
350
|
|
|
$module = \Yii::$app->getModule(Audit::findModuleIdentifier()); |
351
|
|
|
} |
352
|
|
|
if (!$module) { |
353
|
|
|
throw new \Exception('Audit module cannot be loaded'); |
354
|
|
|
} |
355
|
|
|
return Audit::getInstance()->getEntry(true)->id; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
} |
359
|
|
|
|