1 | <?php |
||
2 | |||
3 | namespace App\Http\Middleware; |
||
4 | |||
5 | use App\Repository\Admin\LogRepository; |
||
6 | use Carbon\Carbon; |
||
7 | use Closure; |
||
8 | use GuzzleHttp\Psr7\Query; |
||
9 | use Illuminate\Support\Facades\Auth; |
||
10 | use App\Jobs\WriteSystemLog; |
||
11 | |||
12 | class Log |
||
13 | { |
||
14 | /** |
||
15 | * Handle an incoming request. |
||
16 | * |
||
17 | * @param \Illuminate\Http\Request $request |
||
18 | * @param \Closure $next |
||
19 | * @return mixed |
||
20 | */ |
||
21 | public function handle($request, Closure $next, $guard = '') |
||
22 | { |
||
23 | $data = []; |
||
24 | if ($guard !== '') { |
||
25 | $user = Auth::guard($guard)->user(); |
||
26 | if ($user) { |
||
27 | $data['user_id'] = $user->id; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
28 | $data['user_name'] = $user->name; |
||
0 ignored issues
–
show
|
|||
29 | } |
||
30 | } |
||
31 | $data['url'] = $request->url(); |
||
32 | $data['ua'] = $request->userAgent(); |
||
33 | $data['ip'] = (string) $request->getClientIp(); |
||
34 | $input = $request->all(); |
||
35 | if (isset($input['password'])) { |
||
36 | $input['password'] = '******'; |
||
37 | } |
||
38 | $data['data'] = Query::build($input, false); |
||
39 | |||
40 | if (config('light.log_async_write')) { |
||
41 | $data['updated_at'] = $data['created_at'] = Carbon::now(); |
||
42 | dispatch(new WriteSystemLog($data)); |
||
43 | } else { |
||
44 | LogRepository::add($data); |
||
45 | } |
||
46 | return $next($request); |
||
47 | } |
||
48 | } |
||
49 |