RbacAuthServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Itstructure\LaRbac;
4
5
use Illuminate\Support\Facades\Gate;
6
use Illuminate\Foundation\Support\Providers\AuthServiceProvider;
7
use Itstructure\LaRbac\Models\Permission;
8
use Itstructure\LaRbac\Interfaces\{RbacUserInterface, RbacModelInterface};
9
use Itstructure\LaRbac\Classes\MemberToRole;
10
11
/**
12
 * Class RbacAuthServiceProvider
13
 *
14
 * @package Itstructure\LaRbac
15
 *
16
 * @author Andrey Girnik <[email protected]>
17
 */
18
class RbacAuthServiceProvider extends AuthServiceProvider
19
{
20
    /**
21
     * Register any authentication / authorization services.
22
     * @return void
23
     */
24
    public function boot()
25
    {
26
        $this->registerPolicies();
27
        $this->registerRbacPolicies();
28
    }
29
30
    /**
31
     * Register Rbac policies.
32
     */
33
    public function registerRbacPolicies()
34
    {
35
        /*
36
        |--------------------------------------------------------------------------
37
        | ADMIN member section.
38
        |--------------------------------------------------------------------------
39
        */
40
        Gate::define(Permission::ADMINISTRATE_PERMISSION, function (RbacUserInterface $user) {
41
            return $user->hasAccess([
42
                Permission::ADMINISTRATE_PERMISSION
43
            ]);
44
        });
45
46
        Gate::define(Permission::ASSIGN_ROLE_FLAG, function (RbacUserInterface $user, MemberToRole $memberToRole) {
47
            return $user->canAssignRole($memberToRole->getMember(), $memberToRole->getRole());
48
        });
49
50
        Gate::define(Permission::DELETE_MEMBER_FLAG, function (RbacUserInterface $user, $memberKey) {
51
            return $user->getMemberKeyAttribute() != $memberKey;
52
        });
53
54
55
        /*
56
        |--------------------------------------------------------------------------
57
        | Record section for application.
58
        |--------------------------------------------------------------------------
59
        */
60
        Gate::define(Permission::VIEW_RECORD_PERMISSION, function (RbacUserInterface $user, RbacModelInterface $model = null) {
61
            return $user->hasAccess([
62
                Permission::VIEW_RECORD_PERMISSION
63
            ]) || !empty($model) ? $user->getMemberKeyAttribute() == $model->getAuthorIdAttribute() : false;
0 ignored issues
show
Bug introduced by
The method getAuthorIdAttribute() does not exist on null. ( Ignorable by Annotation )

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

63
            ]) || !empty($model) ? $user->getMemberKeyAttribute() == $model->/** @scrutinizer ignore-call */ getAuthorIdAttribute() : false;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
        });
65
66
        Gate::define(Permission::CREATE_RECORD_PERMISSION, function (RbacUserInterface $user) {
67
            return $user->hasAccess([
68
                Permission::CREATE_RECORD_PERMISSION
69
            ]);
70
        });
71
72
        Gate::define(Permission::UPDATE_RECORD_PERMISSION, function (RbacUserInterface $user, RbacModelInterface $model = null) {
73
            return $user->hasAccess([
74
                Permission::UPDATE_RECORD_PERMISSION
75
            ]) || !empty($model) ? $user->getMemberKeyAttribute() == $model->getAuthorIdAttribute() : false;
76
        });
77
78
        Gate::define(Permission::DELETE_RECORD_PERMISSION, function (RbacUserInterface $user, RbacModelInterface $model = null) {
79
            return $user->hasAccess([
80
                Permission::DELETE_RECORD_PERMISSION
81
            ]) || !empty($model) ? $user->getMemberKeyAttribute() == $model->getAuthorIdAttribute() : false;
82
        });
83
84
        Gate::define(Permission::PUBLISH_RECORD_PERMISSION, function (RbacUserInterface $user) {
85
            return $user->hasAccess([
86
                Permission::PUBLISH_RECORD_PERMISSION
87
            ]);
88
        });
89
    }
90
}
91