Completed
Pull Request — master (#1038)
by Helmut
01:31
created

PermissionMiddleware::handle()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
nc 14
nop 4
dl 22
loc 22
rs 8.9457
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 View Code Duplication
class PermissionMiddleware
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    public function handle($request, Closure $next, $permission, $guard = null)
12
    {
13
        if (is_null($guard)) {
14
            $guard = (require config_path('auth.php'))['defaults']['guard'];
15
        }
16
17
        if (Auth::guard($guard)->guest()) {
18
            throw UnauthorizedException::notLoggedIn();
19
        }
20
21
        $permissions = is_array($permission)
22
            ? $permission
23
            : explode('|', $permission);
24
25
        foreach ($permissions as $permission) {
26
            if (Auth::guard($guard)->user()->can($permission)) {
27
                return $next($request);
28
            }
29
        }
30
31
        throw UnauthorizedException::forPermissions($permissions);
32
    }
33
}
34