CheckAdministrators   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

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 2

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()))
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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