| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace Dcat\Admin\Middleware;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Models\OperationLog as OperationLogModel;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Http\Request;
- class LogOperation
- {
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- *
- * @return mixed
- */
- public function handle(Request $request, \Closure $next)
- {
- if ($this->shouldLogOperation($request)) {
- $user = Admin::user();
- $log = [
- 'user_id' => $user ? $user->id : 0,
- 'path' => substr($request->path(), 0, 255),
- 'method' => $request->method(),
- 'ip' => $request->getClientIp(),
- 'input' => json_encode($request->input()),
- ];
- try {
- OperationLogModel::create($log);
- } catch (\Exception $exception) {
- // pass
- }
- }
- return $next($request);
- }
- /**
- * @param Request $request
- *
- * @return bool
- */
- protected function shouldLogOperation(Request $request)
- {
- return config('admin.operation_log.enable')
- && !$this->inExceptArray($request)
- && $this->inAllowedMethods($request->method());
- }
- /**
- * Whether requests using this method are allowed to be logged.
- *
- * @param string $method
- *
- * @return bool
- */
- protected function inAllowedMethods($method)
- {
- $allowedMethods = collect(config('admin.operation_log.allowed_methods'))->filter();
- if ($allowedMethods->isEmpty()) {
- return true;
- }
- return $allowedMethods->map(function ($method) {
- return strtoupper($method);
- })->contains($method);
- }
- /**
- * Determine if the request has a URI that should pass through CSRF verification.
- *
- * @param \Illuminate\Http\Request $request
- *
- * @return bool
- */
- protected function inExceptArray($request)
- {
- foreach (config('admin.operation_log.except') as $except) {
- $except = admin_base_path($except);
- if ($except !== '/') {
- $except = trim($except, '/');
- }
- if (Helper::matchRequestPath($except)) {
- return true;
- }
- }
- return false;
- }
- }
|