-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathAuditingBehavior.php
188 lines (159 loc) · 5.5 KB
/
AuditingBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace bedezign\yii2\audit;
use yii\db\ActiveRecord;
use bedezign\yii2\audit\models\AuditTrail;
class AuditingBehavior extends \yii\base\Behavior
{
private $_oldattributes = [];
/**
* Array with fields to save
* You don't need to configure both `allowed` and `ignored`
* @var array
*/
public $allowed = [];
/**
* Array with fields to ignore
* You don't need to configure both `allowed` and `ignored`
* @var array
*/
public $ignored = [];
/**
* Array with classes to ignore
* @var array
*/
public $ignoredClasses = [];
/**
* Skip fields where bouth old and new values are NULL
* @var boolean
*/
public $skipNulls = true;
/**
* Is the behavior is active or not
* @var boolean
*/
public $active = true;
public function events()
{
return [
ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
];
}
public function afterDelete($event)
{
$this->leaveTrail('DELETE');
}
public function afterFind($event)
{
$this->setOldAttributes($this->owner->getAttributes());
}
public function afterInsert($event)
{
$this->audit(true);
}
public function afterUpdate($event)
{
$this->audit(false);
}
public function audit($insert)
{
$allowedFields = $this->allowed;
$ignoredFields = $this->ignored;
$ignoredClasses = $this->ignoredClasses;
$newattributes = $this->owner->getAttributes();
$oldattributes = $this->getOldAttributes();
// Lets check if the whole class should be ignored
if (sizeof($ignoredClasses) > 0) {
if (array_search(get_class($this->owner), $ignoredClasses) !== false) {
return;
}
}
// Lets unset fields which are not allowed
if (sizeof($allowedFields) > 0) {
foreach ($newattributes as $f => $v) {
if (array_search($f, $allowedFields) === false) {
unset($newattributes[$f]);
}
}
foreach ($oldattributes as $f => $v) {
if (array_search($f, $allowedFields) === false) {
unset($oldattributes[$f]);
}
}
}
// Lets unset fields which are ignored
if (sizeof($ignoredFields) > 0) {
foreach ($newattributes as $f => $v) {
if (array_search($f, $ignoredFields) !== false) {
unset($newattributes[$f]);
}
}
foreach ($oldattributes as $f => $v) {
if (array_search($f, $ignoredFields) !== false) {
unset($oldattributes[$f]);
}
}
}
// If no difference then WHY?
// There is some kind of problem here that means "0" and 1 do not diff for array_diff so beware: stackoverflow.com/questions/12004231/php-array-diff-weirdness :S
if (count(array_diff_assoc($newattributes, $oldattributes)) <= 0) {
return;
}
// If this is a new record lets add a CREATE notification
if ($insert) {
$this->leaveTrail('CREATE');
}
// Now lets actually write the attributes
$this->auditAttributes($insert, $newattributes, $oldattributes);
// Reset old attributes to handle the case with the same model instance updated multiple times
$this->setOldAttributes($this->owner->getAttributes());
}
public function auditAttributes($insert, $newattributes, $oldattributes = [])
{
foreach ($newattributes as $name => $value) {
$old = isset($oldattributes[$name]) ? $oldattributes[$name] : '';
// If we are skipping nulls then lets see if both sides are null
if ($this->skipNulls && empty($old) && empty($value)) {
continue;
}
// If they are not the same lets write an audit log
if ($value != $old) {
$trail = $this->leaveTrail($insert ? 'SET' : 'CHANGE', $name, $value, $old);
}
}
}
public function leaveTrail($action, $name = null, $value = null, $old_value = null)
{
if ($this->active) {
$entry = Auditing::current() ? Auditing::current()->getEntry() : null;
$user = \Yii::$app->has('user') ? \Yii::$app->user : null;
$log = new AuditTrail;
$log->audit_id = $entry ? $entry->id : null;
$log->user_id = $user ? $user->id : null;
$log->old_value = $old_value;
$log->new_value = $value;
$log->action = $action;
$log->model = $this->owner->className(); // Gets a plain text version of the model name
$log->model_id = (string)$this->getNormalizedPk();
$log->field = $name;
$log->stamp = new \yii\db\Expression('NOW()');
return $log->save();
}
return true;
}
public function getOldAttributes()
{
return $this->_oldattributes;
}
public function setOldAttributes($value)
{
$this->_oldattributes = $value;
}
protected function getNormalizedPk()
{
$pk = $this->owner->getPrimaryKey();
return is_array($pk) ? json_encode($pk) : $pk;
}
}