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