PermissionMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 5
1
<?php
2
3
namespace Spatie\Permission\Middlewares;
4
5
use Closure;
6
use Spatie\Permission\Exceptions\UnauthorizedException;
7
8
class PermissionMiddleware
9
{
10
    public function handle($request, Closure $next, $permission, $guard = null)
11
    {
12
        if (app('auth')->guard($guard)->guest()) {
13
            throw UnauthorizedException::notLoggedIn();
14
        }
15
16
        $permissions = is_array($permission)
17
            ? $permission
18
            : explode('|', $permission);
19
20
        foreach ($permissions as $permission) {
21
            if (app('auth')->guard($guard)->user()->can($permission)) {
22
                return $next($request);
23
            }
24
        }
25
26
        throw UnauthorizedException::forPermissions($permissions);
27
    }
28
}
29