Passed
Push — master ( 41c09c...5f5681 )
by Iman
06:15
created

CBBackend::guardDelete()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\CBCoreModule\middlewares;
4
5
use Closure;
6
use crocodicstudio\crudbooster\helpers\CRUDBooster;
7
8
class CBBackend
9
{
10
    private $module;
11
12
    private $url;
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)
22
    {
23
        $adminPath = cbConfig('ADMIN_PATH', 'admin');
24
25
        if (auth('cbAdmin')->guest()) {
26
            return redirect(url($adminPath.'/login'))->with('message', cbTrans('not_logged_in'));
27
        }
28
29
        $moduleName = $request->segment(2);
30
        $this->module = CRUDBooster::getCurrentModule();
31
32
        foreach (['notifications', 'users/profile', 'users/edit-save'] as $e) {
33
            if ($request->is($adminPath.'/'.$e.'*')) {
0 ignored issues
show
Bug introduced by
$adminPath . '/' . $e . '*' of type string is incompatible with the type Illuminate\Http\dynamic expected by parameter $patterns of Illuminate\Http\Request::is(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            if ($request->is(/** @scrutinizer ignore-type */ $adminPath.'/'.$e.'*')) {
Loading history...
34
                return $next($request);
35
            }
36
        }
37
38
        if ($request->is($adminPath)) {
39
            return $next($request);
40
        }
41
42
        $this->url = $adminPath.'/'.$moduleName;
43
44
        $this->guardView($request);
45
        $this->guardCreate($request);
46
        $this->guardUpdate($request);
47
        $this->guardDelete($request);
48
        $this->guardRead($request);
49
50
        return $next($request);
51
    }
52
53
    /**
54
     * @param $request
55
     */
56
    private function guardView($request)
57
    {
58
        if ($request->is($this->url.'*') && ! CRUDBooster::canView()) {
59
            $this->stopIllegalAction('view');
60
        }
61
    }
62
63
    /**
64
     * @param $request
65
     */
66
    private function guardCreate($request)
67
    {
68
        if ($request->is($this->url.'/add*') && ! CRUDBooster::canCreate()) {
69
            $this->stopIllegalAction('add');
70
        }
71
    }
72
73
    /**
74
     * @param $request
75
     */
76
    private function guardUpdate($request)
77
    {
78
        if ($request->is($this->url.'/edit*') && ! CRUDBooster::canUpdate()) {
79
            $this->stopIllegalAction('edit');
80
        }
81
    }
82
83
    /**
84
     * @param $request
85
     */
86
    private function guardDelete($request)
87
    {
88
        if ($request->is($this->url.'/delete*') && ! CRUDBooster::canDelete()) {
89
            $this->stopIllegalAction('delete');
90
        }
91
    }
92
93
    /**
94
     * @param $request
95
     */
96
    private function guardRead($request)
97
    {
98
        if ($request->is($this->url.'/detail*') && ! CRUDBooster::canRead()) {
99
            $this->stopIllegalAction('view');
100
        }
101
    }
102
103
    /**
104
     * @param $action
105
     */
106
    private function stopIllegalAction($action)
107
    {
108
        CRUDBooster::insertTryLog($action, '');
109
        CRUDBooster::denyAccess();
110
    }
111
}
112