PermissionMiddleware::handle()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 4
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
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