|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OwenIt\Auditing; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Carbon; |
|
8
|
|
|
use Illuminate\Support\Facades\Config; |
|
9
|
|
|
use Illuminate\Support\Facades\Date; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use OwenIt\Auditing\Contracts\AttributeEncoder; |
|
13
|
|
|
|
|
14
|
|
|
trait Audit |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Audit data. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $data = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The Audit attributes that belong to the metadata. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $metadata = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The Auditable attributes that were modified. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $modified = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function auditable() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->morphTo(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function user() |
|
49
|
|
|
{ |
|
50
|
|
|
$morphPrefix = Config::get('audit.user.morph_prefix', 'user'); |
|
51
|
|
|
|
|
52
|
|
|
return $this->morphTo(__FUNCTION__, $morphPrefix . '_type', $morphPrefix . '_id'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
17 |
|
public function getConnectionName() |
|
59
|
|
|
{ |
|
60
|
17 |
|
return Config::get('audit.drivers.database.connection'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
17 |
|
public function getTable(): string |
|
67
|
|
|
{ |
|
68
|
17 |
|
return Config::get('audit.drivers.database.table', parent::getTable()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function resolveData(): array |
|
75
|
|
|
{ |
|
76
|
|
|
$morphPrefix = Config::get('audit.user.morph_prefix', 'user'); |
|
77
|
|
|
|
|
78
|
|
|
// Metadata |
|
79
|
|
|
$this->data = [ |
|
80
|
|
|
'audit_id' => $this->getKey(), |
|
|
|
|
|
|
81
|
|
|
'audit_event' => $this->event, |
|
82
|
|
|
'audit_tags' => $this->tags, |
|
83
|
|
|
'audit_created_at' => $this->serializeDate($this->{$this->getCreatedAtColumn()}), |
|
|
|
|
|
|
84
|
|
|
'audit_updated_at' => $this->serializeDate($this->{$this->getUpdatedAtColumn()}), |
|
|
|
|
|
|
85
|
|
|
'user_id' => $this->getAttribute($morphPrefix . '_id'), |
|
|
|
|
|
|
86
|
|
|
'user_type' => $this->getAttribute($morphPrefix . '_type'), |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
// add resolvers data to metadata |
|
90
|
|
|
$resolverData = []; |
|
91
|
|
|
foreach (array_keys(Config::get('audit.resolvers', [])) as $name) { |
|
92
|
|
|
$resolverData['audit_' . $name] = $this->$name; |
|
93
|
|
|
} |
|
94
|
|
|
$this->data = array_merge($this->data, $resolverData); |
|
95
|
|
|
|
|
96
|
|
|
if ($this->user) { |
|
97
|
|
|
foreach ($this->user->getArrayableAttributes() as $attribute => $value) { |
|
98
|
|
|
$this->data['user_' . $attribute] = $value; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->metadata = array_keys($this->data); |
|
103
|
|
|
|
|
104
|
|
|
// Modified Auditable attributes |
|
105
|
|
|
foreach ($this->new_values ?? [] as $key => $value) { |
|
106
|
|
|
$this->data['new_' . $key] = $value; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
foreach ($this->old_values ?? [] as $key => $value) { |
|
110
|
|
|
$this->data['old_' . $key] = $value; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->modified = array_diff_key(array_keys($this->data), $this->metadata); |
|
114
|
|
|
|
|
115
|
|
|
return $this->data; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the formatted value of an Eloquent model. |
|
120
|
|
|
* |
|
121
|
|
|
* @param Model $model |
|
122
|
|
|
* @param string $key |
|
123
|
|
|
* @param mixed $value |
|
124
|
|
|
* |
|
125
|
|
|
* @return mixed |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getFormattedValue(Model $model, string $key, $value) |
|
128
|
|
|
{ |
|
129
|
|
|
// Apply defined get mutator |
|
130
|
|
|
if ($model->hasGetMutator($key)) { |
|
131
|
|
|
return $model->mutateAttribute($key, $value); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (method_exists($model, 'hasAttributeMutator') && $model->hasAttributeMutator($key)) { |
|
135
|
|
|
return $model->mutateAttributeMarkedAttribute($key, $value); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if (array_key_exists( |
|
139
|
|
|
$key, |
|
140
|
|
|
$model->getCasts() |
|
141
|
|
|
) && $model->getCasts()[$key] == 'Illuminate\Database\Eloquent\Casts\AsArrayObject') { |
|
142
|
|
|
$arrayObject = new \Illuminate\Database\Eloquent\Casts\ArrayObject(json_decode($value, true) ?: []); |
|
143
|
|
|
return $arrayObject; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// Cast to native PHP type |
|
147
|
|
|
if ($model->hasCast($key)) { |
|
148
|
|
|
if ($model->getCastType($key) == 'datetime' ) { |
|
149
|
|
|
$value = $this->castDatetimeUTC($model, $value); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
unset($model->classCastCache[$key]); |
|
153
|
|
|
|
|
154
|
|
|
return $model->castAttribute($key, $value); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Honour DateTime attribute |
|
158
|
|
|
if ($value !== null && in_array($key, $model->getDates(), true)) { |
|
159
|
|
|
return $model->asDateTime($this->castDatetimeUTC($model, $value)); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $value; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function castDatetimeUTC($model, $value) |
|
166
|
|
|
{ |
|
167
|
|
|
if (!is_string($value)) { |
|
168
|
|
|
return $value; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) { |
|
172
|
|
|
$date = Carbon::createFromFormat('Y-m-d', $value, Date::now('UTC')->getTimezone()); |
|
173
|
|
|
|
|
174
|
|
|
if (! $date) { |
|
175
|
|
|
return $value; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return Date::instance($date->startOfDay()); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $value)) { |
|
182
|
|
|
return Date::instance(Carbon::createFromFormat('Y-m-d H:i:s', $value, Date::now('UTC')->getTimezone())); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
try { |
|
186
|
|
|
return Date::createFromFormat($model->getDateFormat(), $value, Date::now('UTC')->getTimezone()); |
|
187
|
|
|
} catch (InvalidArgumentException $e) { |
|
188
|
|
|
return $value; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* {@inheritdoc} |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getDataValue(string $key) |
|
196
|
|
|
{ |
|
197
|
|
|
if (!array_key_exists($key, $this->data)) { |
|
198
|
|
|
return; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$value = $this->data[$key]; |
|
202
|
|
|
|
|
203
|
|
|
// User value |
|
204
|
|
|
if ($this->user && Str::startsWith($key, 'user_')) { |
|
205
|
|
|
return $this->getFormattedValue($this->user, substr($key, 5), $value); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
// Auditable value |
|
209
|
|
|
if ($this->auditable && Str::startsWith($key, ['new_', 'old_'])) { |
|
210
|
|
|
$attribute = substr($key, 4); |
|
211
|
|
|
|
|
212
|
|
|
return $this->getFormattedValue( |
|
213
|
|
|
$this->auditable, |
|
214
|
|
|
$attribute, |
|
215
|
|
|
$this->decodeAttributeValue($this->auditable, $attribute, $value) |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return $value; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Decode attribute value. |
|
224
|
|
|
* |
|
225
|
|
|
* @param Contracts\Auditable $auditable |
|
226
|
|
|
* @param string $attribute |
|
227
|
|
|
* @param mixed $value |
|
228
|
|
|
* |
|
229
|
|
|
* @return mixed |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function decodeAttributeValue(Contracts\Auditable $auditable, string $attribute, $value) |
|
232
|
|
|
{ |
|
233
|
|
|
$attributeModifiers = $auditable->getAttributeModifiers(); |
|
234
|
|
|
|
|
235
|
|
|
if (!array_key_exists($attribute, $attributeModifiers)) { |
|
236
|
|
|
return $value; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$attributeDecoder = $attributeModifiers[$attribute]; |
|
240
|
|
|
|
|
241
|
|
|
if (is_subclass_of($attributeDecoder, AttributeEncoder::class)) { |
|
242
|
|
|
return call_user_func([$attributeDecoder, 'decode'], $value); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $value; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* {@inheritdoc} |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getMetadata(bool $json = false, int $options = 0, int $depth = 512) |
|
252
|
|
|
{ |
|
253
|
|
|
if (empty($this->data)) { |
|
254
|
|
|
$this->resolveData(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$metadata = []; |
|
258
|
|
|
|
|
259
|
|
|
foreach ($this->metadata as $key) { |
|
260
|
|
|
$value = $this->getDataValue($key); |
|
261
|
|
|
$metadata[$key] = $value; |
|
262
|
|
|
|
|
263
|
|
|
if ($value instanceof DateTimeInterface) { |
|
264
|
|
|
$metadata[$key] = !is_null($this->auditable) ? $this->auditable->serializeDate($value) : $this->serializeDate($value); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if (! $json) { |
|
269
|
|
|
return $metadata; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return json_encode($metadata, $options, $depth) ?: '{}'; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* {@inheritdoc} |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getModified(bool $json = false, int $options = 0, int $depth = 512) |
|
279
|
|
|
{ |
|
280
|
|
|
if (empty($this->data)) { |
|
281
|
|
|
$this->resolveData(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$modified = []; |
|
285
|
|
|
|
|
286
|
|
|
foreach ($this->modified as $key) { |
|
287
|
|
|
$attribute = substr($key, 4); |
|
288
|
|
|
$state = substr($key, 0, 3); |
|
289
|
|
|
|
|
290
|
|
|
$value = $this->getDataValue($key); |
|
291
|
|
|
$modified[$attribute][$state] = $value; |
|
292
|
|
|
|
|
293
|
|
|
if ($value instanceof DateTimeInterface) { |
|
294
|
|
|
$modified[$attribute][$state] = !is_null($this->auditable) ? $this->auditable->serializeDate($value) : $this->serializeDate($value); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
|
|
299
|
|
|
if (! $json) { |
|
300
|
|
|
return $modified; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
return json_encode($modified, $options, $depth) ?: '{}'; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Get the Audit tags as an array. |
|
308
|
|
|
* |
|
309
|
|
|
* @return array |
|
310
|
|
|
*/ |
|
311
|
|
|
public function getTags(): array |
|
312
|
|
|
{ |
|
313
|
|
|
return preg_split('/,/', $this->tags, -1, PREG_SPLIT_NO_EMPTY); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|