RoleOrPermissionMiddleware::handle()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 4
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Middlewares;
4
5
use Closure;
6
use Illuminate\Support\Facades\Auth;
7
use Spatie\Permission\Exceptions\UnauthorizedException;
8
9
class RoleOrPermissionMiddleware
10
{
11
    public function handle($request, Closure $next, $roleOrPermission, $guard = null)
12
    {
13
        if (Auth::guard($guard)->guest()) {
14
            throw UnauthorizedException::notLoggedIn();
15
        }
16
17
        $rolesOrPermissions = is_array($roleOrPermission)
18
            ? $roleOrPermission
19
            : explode('|', $roleOrPermission);
20
21
        if (! Auth::guard($guard)->user()->hasAnyRole($rolesOrPermissions) && ! Auth::guard($guard)->user()->hasAnyPermission($rolesOrPermissions)) {
22
            throw UnauthorizedException::forRolesOrPermissions($rolesOrPermissions);
23
        }
24
25
        return $next($request);
26
    }
27
}
28