Passed
Push — master ( 80605f...a8b319 )
by Hergen
05:20
created

RoleMiddleware   A

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 (! $same) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $same of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
20
            throw AuthException::auth('401', 'User does not have right roles');
21
        }
22
23
        return $next($request);
24
    }
25
}
26