Completed
Pull Request — master (#902)
by
unknown
01:25
created

RoleOrPermissionMiddleware::handle()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 7
nop 3
dl 0
loc 19
rs 9.0111
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
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
9
10
class RoleOrPermissionMiddleware
11
{
12
    public function handle($request, Closure $next, $roleOrPermission)
13
    {
14
        if (Auth::guest()) {
15
            throw UnauthorizedException::notLoggedIn();
16
        }
17
18
        $rolesOrPermissions = is_array($roleOrPermission)
19
            ? $roleOrPermission
20
            : explode('|', $roleOrPermission);
21
22
        try {
23
            if (! Auth::user()->hasAnyRole($rolesOrPermissions) || ! Auth::user()->hasAnyPermission($rolesOrPermissions)) {
0 ignored issues
show
Bug introduced by
The method hasAnyRole() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method hasAnyPermission() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
                throw UnauthorizedException::forRolesOrPermissions($rolesOrPermissions);
25
            }
26
        } catch (PermissionDoesNotExist $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
27
        }
28
29
        return $next($request);
30
    }
31
}
32