RoleMiddleware::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class RoleMiddleware
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  Request  $request
14
     * @return mixed
15
     */
16
    public function handle($request, Closure $next, $role)
17
    {
18
        if (backpack_auth()->guest()) {
19
            return redirect('/login');
20
        }
21
22
        if (! backpack_auth()->user()->hasRole($role)) {
0 ignored issues
show
Bug introduced by
The method hasRole() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

22
        if (! backpack_auth()->user()->/** @scrutinizer ignore-call */ hasRole($role)) {
Loading history...
23
            abort(403);
24
        }
25
26
        return $next($request);
27
    }
28
}
29