Completed
Push — master ( 3a6e78...84eb52 )
by Mads
02:50
created

AclService::mayall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
 * @package Napp\Core\Acl
11
 */
12
class AclService implements AclServiceInterface
13
{
14
    /**
15
     * @param \Napp\Core\Acl\Contract\Role|null $user
16
     * @param array|string $roles
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 4
                    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
     * @return bool
42
     */
43 44
    public function hasPermission(string $permission, ?Role $user = null): bool
44
    {
45
        // no user - no permission
46 44
        if (null === $user) {
47 4
            return false;
48
        }
49
50
        // admins and superuser have full access
51 40
        if ($user->isAdmin() || $user->isSuperUser()) {
52 8
            return true;
53
        }
54
55 32
        $allPermissions = PermissionRegistrar::formatPermissions($user->getMergedPermissions());
56
57
        // check if user has permission
58 32
        if (false === array_key_exists($permission, $allPermissions)) {
59 22
            return false;
60
        }
61
62 18
        $permissionAction = $allPermissions[$permission];
63
        //dd($permission, $permissionAction);
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
     * @return bool
77
     */
78 10
    public function hasAnyPermission(array $permissions, ?Role $user = null): bool
79
    {
80 10
        foreach ($permissions as $permission) {
81 10
            $result = $this->hasPermission($permission, $user);
82 10
            if (true === $result) {
83 10
                return true;
84
            }
85
        }
86
87 6
        return false;
88
    }
89
90
    /**
91
     * @param array $permissions
92
     * @param \Napp\Core\Acl\Contract\Role|null $user
93
     * @return bool
94
     */
95 8
    public function hasAllPermissions(array $permissions, ?Role $user = null): bool
96
    {
97 8
        foreach ($permissions as $permission) {
98 8
            $result = $this->hasPermission($permission, $user);
99 8
            if (false === $result) {
100 8
                return false;
101
            }
102
        }
103
104 4
        return true;
105
    }
106
107
    /**
108
     * @return array
109
     */
110 2
    public function allPermissions(): array
111
    {
112 2
        return PermissionRegistrar::getPermissions();
113
    }
114
115
    /**
116
     * @param \Napp\Core\Acl\Contract\Role|null $user
117
     * @return array
118
     */
119 10
    public function getUserPermissions(?Role $user = null): array
120
    {
121 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...
122 2
            return [];
123
        }
124
125 8
        if (null === $user && true === \class_exists(\Napp\Common\Context\Context::class)) {
126 2
            $user = app(\Napp\Common\Context\Context::class)->getCMSUser();
127
        }
128
129 8
        if (null === $user) {
130 4
            return [];
131
        }
132
133
        // admin and superuser have full access
134 4
        if ($user->isAdmin() || $user->isSuperUser()) {
135 2
            return array_keys($this->allPermissions());
136
        }
137
138 2
        return array_keys(PermissionRegistrar::formatPermissions($user->getMergedPermissions()));
139
    }
140
141
    /**
142
     * @param string|array $permission
143
     * @return bool
144
     */
145 34
    public function may($permission): bool
146
    {
147 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...
148
149 34
        if (\is_array($permission)) {
150 10
            return $this->hasAnyPermission($permission, $user);
151
        }
152
153 24
        return $this->hasPermission($permission, $user);
154
    }
155
156
    /**
157
     * @param string|array $permission
158
     * @return bool
159
     */
160 6
    public function maynot($permission): bool
161
    {
162 6
        return !$this->may($permission);
163
    }
164
165
    /**
166
     * @param array $permissions
167
     * @return bool
168
     */
169 2
    public function mayall(array $permissions): bool
170
    {
171 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...
172
    }
173
}
174