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