Completed
Push — master ( a77c04...464b27 )
by Andrey
16:47
created

RbacAuthServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 View Code Duplication
        Gate::define(Permission::VIEW_RECORD_PERMISSION, function (RbacUserInterface $user, RbacModelInterface $model = null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            return $user->hasAccess([
62
                Permission::VIEW_RECORD_PERMISSION
63
            ]) || !empty($model) ? $user->getMemberKeyAttribute() == $model->getAuthorIdAttribute() : false;
0 ignored issues
show
Bug introduced by
It seems like $model is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
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 View Code Duplication
        Gate::define(Permission::UPDATE_RECORD_PERMISSION, function (RbacUserInterface $user, RbacModelInterface $model = null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            return $user->hasAccess([
74
                Permission::UPDATE_RECORD_PERMISSION
75
            ]) || !empty($model) ? $user->getMemberKeyAttribute() == $model->getAuthorIdAttribute() : false;
0 ignored issues
show
Bug introduced by
It seems like $model is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
76
        });
77
78 View Code Duplication
        Gate::define(Permission::DELETE_RECORD_PERMISSION, function (RbacUserInterface $user, RbacModelInterface $model = null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            return $user->hasAccess([
80
                Permission::DELETE_RECORD_PERMISSION
81
            ]) || !empty($model) ? $user->getMemberKeyAttribute() == $model->getAuthorIdAttribute() : false;
0 ignored issues
show
Bug introduced by
It seems like $model is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
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