AclService   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 5
dl 0
loc 169
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A userHasRole() 0 18 5
B hasPermission() 0 28 6
A hasAnyPermission() 0 11 3
A hasAllPermissions() 0 11 3
A allPermissions() 0 4 1
B getUserPermissions() 0 21 8
A may() 0 10 2
A maynot() 0 4 1
A mayall() 0 4 1
1
<?php
2
3
namespace Napp\Core\Acl;
4
5
use Illuminate\Container\Container;
6
use Napp\Core\Acl\Contract\Role;
7
8
/**
9
 * Class AclService.
10
 */
11
class AclService implements AclServiceInterface
12
{
13
    /**
14
     * @param \Napp\Core\Acl\Contract\Role|null $user
15
     * @param array|string                      $roles
16
     *
17
     * @return bool
18
     */
19 14
    public function userHasRole(?Role $user, $roles): bool
20
    {
21 14
        if (null === $user) {
22 2
            return false;
23
        }
24
25 12
        if (\is_array($roles)) {
26 4
            foreach ($roles as $role) {
27 4
                if (true === $this->userHasRole($user, $role)) {
28 2
                    return true;
29
                }
30
            }
31
        } else {
32 12
            return \in_array($roles, $user->getRoles()->pluck('slug')->all());
33
        }
34
35 2
        return false;
36
    }
37
38
    /**
39
     * @param string                            $permission
40
     * @param \Napp\Core\Acl\Contract\Role|null $user
41
     *
42
     * @return bool
43
     */
44 44
    public function hasPermission(string $permission, ?Role $user = null): bool
45
    {
46
        // no user - no permission
47 44
        if (null === $user) {
48 4
            return false;
49
        }
50
51
        // admins and superuser have full access
52 40
        if ($user->isAdmin() || $user->isSuperUser()) {
53 8
            return true;
54
        }
55
56 32
        $allPermissions = PermissionRegistrar::formatPermissions($user->getMergedPermissions());
57
58
        // check if user has permission
59 32
        if (false === array_key_exists($permission, $allPermissions)) {
60 22
            return false;
61
        }
62
63 18
        $permissionAction = $allPermissions[$permission];
64
        // simple permission check - no closure
65 18
        if ($permissionAction === $permission) {
66 12
            return true;
67
        }
68
69
        // Calling Instance Methods: ClassName@methodName
70 6
        return Container::getInstance()->call($permissionAction);
71
    }
72
73
    /**
74
     * @param array                             $permissions
75
     * @param \Napp\Core\Acl\Contract\Role|null $user
76
     *
77
     * @return bool
78
     */
79 10
    public function hasAnyPermission(array $permissions, ?Role $user = null): bool
80
    {
81 10
        foreach ($permissions as $permission) {
82 10
            $result = $this->hasPermission($permission, $user);
83 10
            if (true === $result) {
84 4
                return true;
85
            }
86
        }
87
88 6
        return false;
89
    }
90
91
    /**
92
     * @param array                             $permissions
93
     * @param \Napp\Core\Acl\Contract\Role|null $user
94
     *
95
     * @return bool
96
     */
97 8
    public function hasAllPermissions(array $permissions, ?Role $user = null): bool
98
    {
99 8
        foreach ($permissions as $permission) {
100 8
            $result = $this->hasPermission($permission, $user);
101 8
            if (false === $result) {
102 6
                return false;
103
            }
104
        }
105
106 4
        return true;
107
    }
108
109
    /**
110
     * @return array
111
     */
112 2
    public function allPermissions(): array
113
    {
114 2
        return PermissionRegistrar::getPermissions();
115
    }
116
117
    /**
118
     * @param \Napp\Core\Acl\Contract\Role|null $user
119
     *
120
     * @return array
121
     */
122 10
    public function getUserPermissions(?Role $user = null): array
123
    {
124 10
        if (null === $user && false === auth()->check()) {
0 ignored issues
show
Bug introduced by
The method check does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
125 2
            return [];
126
        }
127
128 8
        if (null === $user && true === \class_exists(\Napp\Common\Context\Context::class)) {
129 2
            $user = app(\Napp\Common\Context\Context::class)->getCMSUser();
130
        }
131
132 8
        if (null === $user) {
133 4
            return [];
134
        }
135
136
        // admin and superuser have full access
137 4
        if ($user->isAdmin() || $user->isSuperUser()) {
138 2
            return array_keys($this->allPermissions());
139
        }
140
141 2
        return array_keys(PermissionRegistrar::formatPermissions($user->getMergedPermissions()));
142
    }
143
144
    /**
145
     * @param string|array $permission
146
     *
147
     * @return bool
148
     */
149 34
    public function may($permission): bool
150
    {
151 34
        $user = auth(config('acl.guard'))->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
152
153 34
        if (\is_array($permission)) {
154 10
            return $this->hasAnyPermission($permission, $user);
155
        }
156
157 24
        return $this->hasPermission($permission, $user);
158
    }
159
160
    /**
161
     * @param string|array $permission
162
     *
163
     * @return bool
164
     */
165 6
    public function maynot($permission): bool
166
    {
167 6
        return ! $this->may($permission);
168
    }
169
170
    /**
171
     * @param array $permissions
172
     *
173
     * @return bool
174
     */
175 2
    public function mayall(array $permissions): bool
176
    {
177 2
        return $this->hasAllPermissions($permissions, auth(config('acl.guard'))->user());
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
178
    }
179
}
180