RoleMiddleware::handle()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
c 0
b 0
f 0
rs 10
cc 3
nc 4
nop 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