1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace bedezign\yii2\audit\models; |
4
|
|
|
|
5
|
|
|
use bedezign\yii2\audit\components\db\ActiveRecord; |
6
|
|
|
use bedezign\yii2\audit\components\Helper; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\db\ActiveQuery; |
9
|
|
|
use yii\db\Expression; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* AuditEntry |
13
|
|
|
* @package bedezign\yii2\audit\models |
14
|
|
|
* |
15
|
|
|
* @property int $id |
16
|
|
|
* @property string $created |
17
|
|
|
* @property float $duration |
18
|
|
|
* @property int $user_id 0 means anonymous |
19
|
|
|
* @property string $ip |
20
|
|
|
* @property string $route |
21
|
|
|
* @property int $memory_max |
22
|
|
|
* @property string $request_method |
23
|
|
|
* @property string $ajax |
24
|
|
|
* |
25
|
|
|
* @property AuditError[] $linkedErrors |
26
|
|
|
* @property AuditJavascript[] $javascripts |
27
|
|
|
* @property AuditTrail[] $trails |
28
|
|
|
* @property AuditMail[] $mails |
29
|
|
|
* @property AuditData[] $data |
30
|
|
|
*/ |
31
|
|
|
class AuditEntry extends ActiveRecord |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $autoSerialize = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
57 |
|
public static function tableName() |
42
|
|
|
{ |
43
|
57 |
|
return '{{%audit_entry}}'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param bool $initialise |
48
|
|
|
* @return static |
49
|
|
|
*/ |
50
|
113 |
|
public static function create($initialise = true) |
51
|
|
|
{ |
52
|
39 |
|
$entry = new static; |
53
|
74 |
|
if ($initialise) |
54
|
39 |
|
$entry->record(); |
55
|
|
|
|
56
|
113 |
|
return $entry; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns all linked AuditError instances |
61
|
|
|
* (Called `linkedErrors()` to avoid confusion with the `getErrors()` method) |
62
|
|
|
* @return ActiveQuery |
63
|
|
|
*/ |
64
|
3 |
|
public function getLinkedErrors() |
65
|
|
|
{ |
66
|
3 |
|
return static::hasMany(AuditError::className(), ['entry_id' => 'id']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns all linked AuditTrail instances |
71
|
|
|
* @return ActiveQuery |
72
|
|
|
*/ |
73
|
3 |
|
public function getTrails() |
74
|
|
|
{ |
75
|
3 |
|
return static::hasMany(AuditTrail::className(), ['entry_id' => 'id']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns all linked AuditMail instances |
80
|
|
|
* @return ActiveQuery |
81
|
|
|
*/ |
82
|
3 |
|
public function getMails() |
83
|
|
|
{ |
84
|
3 |
|
return static::hasMany(AuditMail::className(), ['entry_id' => 'id']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns all linked AuditJavascript instances |
89
|
|
|
* @return ActiveQuery |
90
|
|
|
*/ |
91
|
3 |
|
public function getJavascripts() |
92
|
|
|
{ |
93
|
3 |
|
return static::hasMany(AuditJavascript::className(), ['entry_id' => 'id']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns all linked data records |
98
|
|
|
* @return ActiveQuery |
99
|
|
|
*/ |
100
|
1 |
|
public function getData() |
101
|
|
|
{ |
102
|
1 |
|
return static::hasMany(AuditData::className(), ['entry_id' => 'id'])->indexBy('type'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Writes a number of associated data records in one go. |
107
|
|
|
* @param $batchData |
108
|
|
|
* @param bool $compact |
109
|
|
|
* @throws \yii\db\Exception |
110
|
|
|
*/ |
111
|
38 |
|
public function addBatchData($batchData, $compact = true) |
112
|
|
|
{ |
113
|
38 |
|
$columns = ['entry_id', 'type', 'created', 'data']; |
114
|
38 |
|
$rows = []; |
115
|
38 |
|
$params = []; |
116
|
14 |
|
$date = date('Y-m-d H:i:s'); |
117
|
|
|
// Some database like postgres depend on the data being escaped correctly. |
118
|
|
|
// PDO can take care of this if you define the field as a LOB (Large OBject), but unfortunately Yii does threat values |
119
|
|
|
// for batch inserts the same way. This code adds a number of literals instead of the actual values |
120
|
|
|
// so that they can be bound right before insert and still get escaped correctly |
121
|
14 |
|
foreach ($batchData as $type => $data) { |
122
|
14 |
|
$param = ':data_' . str_replace('/', '_', $type); |
123
|
14 |
|
$rows[] = [$this->id, $type, $date, new Expression($param)]; |
124
|
14 |
|
$params[$param] = [Helper::serialize($data, $compact), \PDO::PARAM_LOB]; |
125
|
14 |
|
} |
126
|
14 |
|
static::getDb()->createCommand()->batchInsert(AuditData::tableName(), $columns, $rows)->bindValues($params)->execute(); |
127
|
38 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $type |
131
|
|
|
* @param $data |
132
|
|
|
* @param bool|true $compact |
133
|
|
|
* @throws \yii\db\Exception |
134
|
|
|
*/ |
135
|
2 |
|
public function addData($type, $data, $compact = true) |
136
|
|
|
{ |
137
|
|
|
// Make sure to mark data as a large object so it gets escaped |
138
|
|
|
$record = [ |
139
|
2 |
|
'entry_id' => $this->id, |
140
|
2 |
|
'type' => $type, |
141
|
2 |
|
'created' => date('Y-m-d H:i:s'), |
142
|
2 |
|
'data' => [Helper::serialize($data, $compact), \PDO::PARAM_LOB], |
143
|
2 |
|
]; |
144
|
2 |
|
static::getDb()->createCommand()->insert(AuditData::tableName(), $record)->execute(); |
145
|
2 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Records the current application state into the instance. |
149
|
|
|
*/ |
150
|
113 |
|
public function record() |
151
|
|
|
{ |
152
|
113 |
|
$app = Yii::$app; |
153
|
39 |
|
$request = $app->request; |
154
|
|
|
|
155
|
39 |
|
$this->route = $app->requestedAction ? $app->requestedAction->uniqueId : null; |
156
|
113 |
|
if ($request instanceof \yii\web\Request) { |
157
|
39 |
|
$user = $app->user; |
158
|
39 |
|
$this->user_id = $user->isGuest ? 0 : $user->id; |
159
|
39 |
|
$this->ip = $request->userIP; |
160
|
39 |
|
$this->ajax = $request->isAjax; |
161
|
39 |
|
$this->request_method = $request->method; |
162
|
39 |
|
} else if ($request instanceof \yii\console\Request) { |
163
|
|
|
$this->request_method = 'CLI'; |
164
|
74 |
|
} |
165
|
|
|
|
166
|
39 |
|
$this->save(false); |
167
|
113 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
24 |
|
public function finalize() |
173
|
|
|
{ |
174
|
24 |
|
$this->duration = microtime(true) - YII_BEGIN_TIME; |
175
|
24 |
|
$this->memory_max = memory_get_peak_usage(); |
176
|
24 |
|
return $this->save(false, ['duration', 'memory_max']); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
5 |
|
public function attributeLabels() |
183
|
|
|
{ |
184
|
|
|
return [ |
185
|
5 |
|
'id' => Yii::t('audit', 'Entry ID'), |
186
|
5 |
|
'created' => Yii::t('audit', 'Created'), |
187
|
5 |
|
'ip' => Yii::t('audit', 'IP'), |
188
|
5 |
|
'duration' => Yii::t('audit', 'Duration'), |
189
|
5 |
|
'user_id' => Yii::t('audit', 'User'), |
190
|
5 |
|
'memory_max' => Yii::t('audit', 'Memory'), |
191
|
5 |
|
'request_method' => Yii::t('audit', 'Request Method'), |
192
|
5 |
|
]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|