Passed
Push — feature/application-review-ui ( afbf92 )
by Chris
06:55
created

CheckRole::handle()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 21
rs 8.8333
cc 7
nc 5
nop 3
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Facades\App\Services\WhichPortal;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Log;
9
10
class CheckRole
11
{
12
13
    /**
14
     * Handle an incoming request.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @param  \Closure  $next
18
     * @param  string  $role
19
     * @return mixed
20
     */
21
    public function handle($request, Closure $next, $role)
22
    {
23
        // If user logged in as admin, always pass, regardless of $role.
24
        if (Auth::check() && Auth::user()->isAdmin()) {
25
            Log::info('CheckRole Bypassed as Admin');
26
            return $next($request);
27
        }
28
29
        // Redirect if not logged in, or if not the correct role.
30
        if (Auth::guest() || !Auth::user()->hasRole($role)) {
31
            Log::info('CheckRole Failed');
32
            // TODO: redirect to some sort of error messag
33
            if (WhichPortal::isManagerPortal()) {
34
                return redirect(route('manager.home'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
                return /** @scrutinizer ignore-call */ redirect(route('manager.home'));
Loading history...
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
                return redirect(/** @scrutinizer ignore-call */ route('manager.home'));
Loading history...
35
            } elseif (WhichPortal::isHrPortal()) {
36
                return redirect(route('hr_advisor.home'));
37
            } else {
38
                return redirect(route('home'));
39
            }
40
        }
41
        return $next($request);
42
    }
43
}
44