Completed
Push — master ( 77f56b...bad6ef )
by ARCANEDEV
04:03
created

CheckAdministrators   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 56
ccs 0
cts 18
cp 0
rs 10
wmc 6
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 7 2
A isAllowed() 0 8 3
A failedAuthorization() 0 6 1
1
<?php namespace Arcanesoft\Core\Http\Middleware;
2
3
use Arcanedev\Support\Http\Middleware;
4
use Closure;
5
use Illuminate\Auth\Access\AuthorizationException;
6
use Illuminate\Http\Request;
7
8
/**
9
 * Class     AdminMiddleware
10
 *
11
 * @package  Arcanesoft\Foundation\Http\Middleware
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class CheckAdministrators extends Middleware
15
{
16
    /* -----------------------------------------------------------------
17
     |  Main Methods
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Run the request filter.
23
     *
24
     * @param  \Illuminate\Http\Request  $request
25
     * @param  \Closure                  $next
26
     *
27
     * @return mixed
28
     *
29
     * @throws \Illuminate\Auth\Access\AuthorizationException
30
     */
31
    public function handle(Request $request, Closure $next)
32
    {
33
        if ( ! $this->isAllowed())
34
            $this->failedAuthorization();
35
36
        return $next($request);
37
    }
38
39
    /* -----------------------------------------------------------------
40
     |  Other Methods
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Check if the user is allowed.
46
     *
47
     * @return bool
48
     */
49
    protected function isAllowed()
50
    {
51
        /** @var  \Arcanesoft\Contracts\Auth\Models\User  $user */
52
        if (is_null($user = auth()->user()))
53
            return false;
54
55
        return $user->isAdmin() || $user->isModerator();
56
    }
57
58
    /**
59
     * Handle a failed authorization attempt.
60
     *
61
     * @throws \Illuminate\Auth\Access\AuthorizationException
62
     */
63
    protected function failedAuthorization()
64
    {
65
        throw new AuthorizationException(
66
            '[Unauthorized] You are not allowed to perform this action.', 403
67
        );
68
    }
69
}
70