1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audit\Observers; |
4
|
|
|
|
5
|
|
|
// Deps |
6
|
|
|
|
7
|
|
|
use Audit\Models\Change; |
8
|
|
|
use Event; |
9
|
|
|
use Route; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Create a log of all model changing events |
13
|
|
|
*/ |
14
|
|
|
class Changes |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Only log the following events |
18
|
|
|
* |
19
|
|
|
* @param array |
20
|
|
|
*/ |
21
|
|
|
protected $supported = ['created', 'updated', 'deleted']; |
22
|
|
|
|
23
|
|
|
protected $action = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Handle all Eloquent model events |
27
|
|
|
* |
28
|
|
|
* @param string $event |
29
|
|
|
* @param array $payload Contains: |
30
|
|
|
* - |
31
|
|
|
* Audit\Models\Base |
32
|
|
|
* $model |
33
|
|
|
*/ |
34
|
|
|
public function handle($event, $payload) |
35
|
|
|
{ |
36
|
|
|
list($model) = $payload; |
37
|
|
|
if ($this->isToIgnore($model, $event)) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
// If `log_changes` was configed as a callable, see if this model event |
43
|
|
|
// should not be logged |
44
|
|
|
if ($check = \Illuminate\Support\Facades\Config::get('sitec.site.log_changes')) { |
45
|
|
|
if (is_bool($check) && !$check) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
// Get the admin acting on the record |
49
|
|
|
$admin = $this->getUser(); |
50
|
|
|
if (is_callable($check)) { |
51
|
|
|
if (!call_user_func($check, $model, $this->action, $admin)) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Check with the model itself to see if it should be logged |
60
|
|
|
if (method_exists($model, 'shouldLogChange')) { |
61
|
|
|
if (!$model->shouldLogChange($this->action)) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
// Default to not logging changes if there is no shouldLogChange() |
65
|
|
|
} else { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
\Log::debug('[Audit] Log Changes: '.get_class($model)); |
69
|
|
|
// \Log::debug('[Audit] Log Changes: '.print_r($event, true).print_r($payload, true)); |
70
|
|
|
|
71
|
|
|
// Log the event |
72
|
|
|
Change::log($model, $this->action, $admin); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getUser() |
76
|
|
|
{ |
77
|
|
|
return app('facilitador.user'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function isToIgnore($model, $event) |
81
|
|
|
{ |
82
|
|
|
// Don't log changes to pivot models. Even though a user may have initiated |
83
|
|
|
// this, it's kind of meaningless to them. These events can happen when a |
84
|
|
|
// user messes with drag and drop positioning. |
85
|
|
|
if (!empty($this->getDontLog())) { |
86
|
|
|
foreach ($this->getDontLog() as $logClass) { |
87
|
|
|
if (is_a($model, $logClass)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
if (!empty($this->getDontLogAlias())) { |
93
|
|
|
foreach ($this->getDontLogAlias() as $logClassAlias) { |
94
|
|
|
if (strpos($event, $logClassAlias) !== false) { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Get the action of the event |
101
|
|
|
preg_match('#eloquent\.(\w+)#', $event, $matches); |
102
|
|
|
$this->action = $matches[1]; |
|
|
|
|
103
|
|
|
if (!in_array($this->action, $this->supported)) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function getDontLog() |
111
|
|
|
{ |
112
|
|
|
return \Illuminate\Support\Facades\Config::get( |
113
|
|
|
'sitec.audit.dontLog', |
114
|
|
|
[ |
115
|
|
|
\Aschmelyun\Larametrics\Models\LarametricsLog::class, |
116
|
|
|
\Illuminate\Database\Eloquent\Relations\Pivot::class, |
117
|
|
|
] |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getDontLogAlias() |
122
|
|
|
{ |
123
|
|
|
return \Illuminate\Support\Facades\Config::get( |
124
|
|
|
'sitec.audit.dontLogAlias', |
125
|
|
|
[ |
126
|
|
|
'Tracking\Models', |
127
|
|
|
'Analytics', |
128
|
|
|
'Spatie\Analytics', |
129
|
|
|
'Wnx\LaravelStats', |
130
|
|
|
'Aschmelyun\Larametrics\Models', |
131
|
|
|
'Laravel\Horizon', |
132
|
|
|
'Support\Models\Application', |
133
|
|
|
'Support\Models\Ardent', |
134
|
|
|
'Support\Models\Code', |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: