RoleMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 9
dl 0
loc 15
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 3
1
<?php
2
3
namespace Werk365\JwtAuthRoles\Middlewares;
4
5
use Closure;
6
use Illuminate\Support\Facades\Auth;
7
use Werk365\JwtAuthRoles\Exceptions\AuthException;
8
9
class RoleMiddleware
10
{
11
    public function handle($request, Closure $next, $role)
12
    {
13
        $roles = is_array($role)
14
            ? $role
15
            : explode('|', $role);
16
        $user_roles = array_map('strtolower', Auth::user()->roles);
0 ignored issues
show
Bug introduced by
Accessing roles on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
17
        $same = (array_intersect($roles, $user_roles));
18
19
        if (empty($same)) {
20
            throw AuthException::auth('401', 'User does not have right roles');
21
        }
22
23
        return $next($request);
24
    }
25
}
26