Yeelight /
cloud2
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Yeelight\Http\Middleware; |
||
| 4 | |||
| 5 | use Illuminate\Http\Request; |
||
| 6 | use Illuminate\Support\Facades\Auth; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | use Yeelight\Models\AdminOperationLog as OperationLogModel; |
||
| 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' => Auth::guard(config('yeelight.backend.route.prefix'))->user()->id, |
||
| 25 | 'path' => $request->path(), |
||
| 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('yeelight.backend.operation_log.enable') |
||
| 45 | && !$this->inExceptArray($request) |
||
| 46 | && Auth::guard(config('yeelight.backend.route.prefix'))->user(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Determine if the request has a URI that should pass through CSRF verification. |
||
| 51 | * |
||
| 52 | * @param \Illuminate\Http\Request $request |
||
| 53 | * |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | protected function inExceptArray($request) |
||
| 57 | { |
||
| 58 | foreach (config('yeelight.backend.operation_log.except') as $except) { |
||
| 59 | if ($except !== '/') { |
||
| 60 | $except = trim($except, '/'); |
||
| 61 | } |
||
| 62 | |||
| 63 | $methods = []; |
||
| 64 | |||
| 65 | if (Str::contains($except, ':')) { |
||
| 66 | list($methods, $except) = explode(':', $except); |
||
| 67 | $methods = explode(',', $methods); |
||
| 68 | } |
||
| 69 | |||
| 70 | $methods = array_map('strtoupper', $methods); |
||
| 71 | |||
| 72 | if ($request->is($except) && |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 73 | (empty($methods) || in_array($request->method(), $methods))) { |
||
| 74 | return true; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return false; |
||
| 79 | } |
||
| 80 | } |
||
| 81 |