BackendAuthorize::handle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 4
eloc 6
c 1
b 1
f 1
nc 3
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Http\Requests\Request;
6
use Illuminate\Auth\AuthenticationException;
7
8
class BackendAuthorize
9
{
10
    /**
11
     * @param Request $request
12
     * @param         $next
13
     *
14
     * @throws AuthenticationException
15
     *
16
     * @return \Illuminate\Http\RedirectResponse
17
     */
18
    public function handle($request, $next)
19
    {
20
        $user = $request->user();
21
        if ($user && $user->hasRole('admin')) {
22
            return $next($request);
23
        }
24
25
        if ($request->wantsJson()) {
26
            throw new AuthenticationException();
27
        }
28
29
        return redirect(route('home'))->withErrors('You do not have privilege access this page');
30
    }
31
}
32