|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Auth\Database\OperationLog as OperationLogModel; |
|
6
|
|
|
use Encore\Admin\Facades\Admin; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
|
|
10
|
|
|
class LogOperation |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Handle an incoming request. |
|
14
|
|
|
* |
|
15
|
|
|
* @param \Illuminate\Http\Request $request |
|
16
|
|
|
* @param \Closure $next |
|
17
|
|
|
* |
|
18
|
|
|
* @return mixed |
|
19
|
|
|
*/ |
|
20
|
|
|
public function handle(Request $request, \Closure $next) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($this->shouldLogOperation($request)) { |
|
23
|
|
|
$log = [ |
|
24
|
|
|
'user_id' => Admin::user()->id, |
|
|
|
|
|
|
25
|
|
|
'path' => substr($request->path(), 0, 255), |
|
26
|
|
|
'method' => $request->method(), |
|
27
|
|
|
'ip' => $request->getClientIp(), |
|
28
|
|
|
'input' => json_encode($request->input()), |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
OperationLogModel::create($log); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $next($request); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function shouldLogOperation(Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
return config('admin.operation_log.enable') |
|
45
|
|
|
&& !$this->inExceptArray($request) && $this->inAllowedMethods($request->method()) |
|
46
|
|
|
&& Admin::user(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $method |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function inAllowedMethods($method) { |
|
56
|
|
|
$allowed_methods = array_map('strtoupper', (array)config('admin.operation_log.allowed_methods')); |
|
57
|
|
|
if (empty($allowed_methods)) { |
|
58
|
|
|
return true; |
|
59
|
|
|
} else { |
|
60
|
|
|
if (in_array($method, $allowed_methods)) { |
|
61
|
|
|
return true; |
|
62
|
|
|
} else { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determine if the request has a URI that should pass through CSRF verification. |
|
70
|
|
|
* |
|
71
|
|
|
* @param \Illuminate\Http\Request $request |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function inExceptArray($request) |
|
76
|
|
|
{ |
|
77
|
|
|
foreach (config('admin.operation_log.except') as $except) { |
|
78
|
|
|
if ($except !== '/') { |
|
79
|
|
|
$except = trim($except, '/'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$methods = []; |
|
83
|
|
|
|
|
84
|
|
|
if (Str::contains($except, ':')) { |
|
85
|
|
|
list($methods, $except) = explode(':', $except); |
|
86
|
|
|
$methods = explode(',', $methods); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$methods = array_map('strtoupper', $methods); |
|
90
|
|
|
|
|
91
|
|
|
if ($request->is($except) && |
|
92
|
|
|
(empty($methods) || in_array($request->method(), $methods))) { |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: