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