Completed
Push — master ( b739db...92d607 )
by Abdelrahman
02:42
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\Gate;
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
        if ($user = Auth::guard($guard)->user()) {
37
            $user->all_abilities->map(function ($ability) {
38
                // Bypass authorization if user is super admin already
39
                Gate::before(function ($user) {
40
                    return $user->isSuperadmin() ? true : null;
41
                });
42
43
                // Define abilities and policies
44
                Gate::define($ability->action, $ability->policy ?: function () { return true; });
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
45
            });
46
        }
47
48
        return $next($request);
49
    }
50
}
51