Completed
Push — master ( aa55a0...981779 )
by Abdelrahman
03:08
created

Abilities::hasAbilityTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Http\Middleware;
17
18
use Closure;
19
use Illuminate\Http\Request;
20
use Illuminate\Support\Facades\Auth;
21
use Illuminate\Support\Facades\Lang;
22
23
class Abilities
24
{
25
    /**
26
     * Handle an incoming request.
27
     *
28
     * @param \Illuminate\Http\Request $request
29
     * @param \Closure                 $next
30
     * @param string|null              $guard
31
     *
32
     * @return mixed
33
     */
34
    public function handle(Request $request, Closure $next, $guard = null)
35
    {
36
        // Check if the user has ability
37
        if (! ($id = Auth::guard($guard)->id()) || ! $this->hasAbilityTo($id, $request->route()->getName())) {
38
            // Fire the unauthorized event
39
            event('rinvex.fort.auth.unauthorized');
40
41
            return ! $id ? intend([
42
                'intended'   => route('rinvex.fort.frontend.auth.login'),
43
                'withErrors' => ['rinvex.fort.session.expired' => Lang::get('rinvex.fort::message.auth.session.required')],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
44
            ], 401) : intend([
45
                'intended'   => route('home'),
46
                'withErrors' => ['no_ability' => Lang::get('rinvex.fort::message.auth.unauthorized')],
47
            ], 401);
48
        }
49
50
        return $next($request);
51
    }
52
53
    /**
54
     * Determine if user has ability to access this route.
55
     *
56
     * @param int    $id
57
     * @param string $routeName
58
     *
59
     * @return bool
60
     */
61
    protected function hasAbilityTo($id, $routeName)
62
    {
63
        $user = app('rinvex.fort.user')->with(['abilities', 'roles'])->find($id);
64
65
        return app('rinvex.fort.user')->hasAbilityTo($user, ['global.superuser', $routeName]);
66
    }
67
}
68