Completed
Pull Request — master (#2523)
by jxlwqq
05:51
created

LogOperation::inAllowedMethods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
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,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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