GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Authorize::hasRequiredAbility()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 3
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Authorize\Middleware;
4
5
use Closure;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpKernel\Exception\HttpException;
8
9
class Authorize
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param \Illuminate\Http\Request $request
15
     * @param \Closure                 $next
16
     * @param string|null              $ability
17
     * @param string|null              $boundModelName
18
     *
19
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
20
     *
21
     * @return mixed
22
     */
23
    public function handle($request, Closure $next, $ability = null, $boundModelName = null)
24
    {
25
        $model = $this->getModelFromRequest($request, $boundModelName);
26
27
        if (!$this->hasRequiredAbility($request->user(), $ability, $model)) {
28
            return $this->handleUnauthorizedRequest($request, $ability, $model);
29
        }
30
31
        return $next($request);
32
    }
33
34
    /**
35
     * Get the model from the request using given boundModelName.
36
     *
37
     * @param mixed  $request
38
     * @param string $boundModelName
39
     *
40
     * @return \Illuminate\Database\Eloquent\Model|null
41
     */
42
    protected function getModelFromRequest($request, $boundModelName)
43
    {
44
        if (is_null($boundModelName)) {
45
            return;
46
        }
47
48
        return $request->route($boundModelName);
49
    }
50
51
    /**
52
     * Determine if the currently logged in use has the given ability.
53
     *
54
     * @param $user
55
     * @param string|null                              $ability
56
     * @param \Illuminate\Database\Eloquent\Model|null $model
57
     *
58
     * @return bool
59
     */
60
    protected function hasRequiredAbility($user, $ability = null, $model = null)
61
    {
62
        if (!$user) {
63
            return false;
64
        }
65
66
        if (is_null($ability)) {
67
            return true;
68
        }
69
70
        /*
71
         * Some gates may check on number of arguments given. If model
72
         * is null, don't pass it as an argument.
73
         */
74
        if (is_null($model)) {
75
            return $user->can($ability);
76
        }
77
78
        return $user->can($ability, $model);
79
    }
80
81
    /**
82
     * Handle the unauthorized request.
83
     *
84
     * @param $request
85
     * @param string|null                              $ability
86
     * @param \Illuminate\Database\Eloquent\Model|null $model
87
     *
88
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
89
     *
90
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\RedirectResponse|Response
91
     */
92
    protected function handleUnauthorizedRequest($request, $ability = null, $model = null)
0 ignored issues
show
Unused Code introduced by
The parameter $ability is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        if ($request->ajax()) {
95
            return response('Forbidden.', Response::HTTP_FORBIDDEN);
96
        }
97
98
        if (!$request->user()) {
99
            return redirect()->guest(config('laravel-authorize.login_url'));
100
        }
101
102
        throw new HttpException(Response::HTTP_UNAUTHORIZED, 'This action is unauthorized.');
103
    }
104
}
105