Passed
Push — develop ( b3638f...964a77 )
by Ngoding
04:49
created

AuthServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Providers;
4
5
use App\Infrastructure\Contracts\Auth\HasRole;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
8
use Illuminate\Support\Facades\Gate;
9
10
class AuthServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * {@inheritDoc}
14
     *
15
     * @var array<class-string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, class-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, class-string>.
Loading history...
16
     */
17
    protected $policies = [
18
        // 'App\Models\Model' => 'App\Policies\ModelPolicy',
19
    ];
20
21
    /**
22
     * Register any authentication / authorization services.
23
     *
24
     * @return void
25
     */
26 99
    public function boot()
27
    {
28 99
        $this->registerPolicies();
29
30 99
        $this->defineGate();
31 99
    }
32
33
    /**
34
     * Set gate policy definition.
35
     *
36
     * @return void
37
     */
38 99
    protected function defineGate()
39
    {
40 99
        Gate::before(function (HasRole $user, $ability, array $models) {
41
            if ($user->isAdmin()) {
42
                if (in_array($ability, ['delete', 'forceDelete'])) {
43
                    $model = head($models);
44
45
                    if ($model instanceof Model && $model->is($user)) {
46
                        return false;
47
                    }
48
                }
49
50
                return true;
51
            }
52 99
        });
53 99
    }
54
}
55