Completed
Push — master ( a9d673...d0cde4 )
by Arjay
06:58
created

PermissionMiddleware::handle()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 18
ccs 0
cts 16
cp 0
rs 9.2
cc 4
eloc 11
nc 3
nop 3
crap 20
1
<?php
2
3
namespace Yajra\Acl\Middleware;
4
5
use Closure;
6
7 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...
8
{
9
    /**
10
     * Handle an incoming request.
11
     *
12
     * @param  \Illuminate\Http\Request $request
13
     * @param  \Closure $next
14
     * @param  string $permission
15
     * @return mixed
16
     */
17
    public function handle($request, Closure $next, $permission)
18
    {
19
        if (! $request->user() || ! $request->user()->can($permission)) {
20
            if ($request->ajax()) {
21
                return response()->json([
22
                    'error' => [
23
                        'status_code' => 401,
24
                        'code'        => 'INSUFFICIENT_PERMISSIONS',
25
                        'description' => 'You are not authorized to access this resource.',
26
                    ],
27
                ], 401);
28
            }
29
30
            return abort(401, 'You are not authorized to access this resource.');
31
        }
32
33
        return $next($request);
34
    }
35
}
36