Completed
Pull Request — master (#4975)
by zi
02:51
created

HasPermissions::bootHasPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Auth\Database;
4
5
use Illuminate\Support\Collection;
6
7
trait HasPermissions
8
{
9
    /**
10
     * Get all permissions of user.
11
     *
12
     * @return mixed
13
     */
14
    public function allPermissions(): Collection
15
    {
16
        return $this->roles()->with('permissions')->get()->pluck('permissions')->flatten()->merge($this->permissions);
0 ignored issues
show
Bug introduced by
The property permissions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like roles() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
17
    }
18
19
    /**
20
     * Check if user has permission.
21
     *
22
     * @param $ability
23
     * @param array $arguments
24
     *
25
     * @return bool
26
     */
27
    public function can($ability, $arguments = []): bool
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        if (empty($ability)) {
30
            return true;
31
        }
32
33
        if ($this->isAdministrator()) {
34
            return true;
35
        }
36
37
        if ($this->permissions->pluck('slug')->contains($ability)) {
38
            return true;
39
        }
40
41
        return $this->roles->pluck('permissions')->flatten()->pluck('slug')->contains($ability);
0 ignored issues
show
Bug introduced by
The property roles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
    }
43
44
    /**
45
     * Check if user has no permission.
46
     *
47
     * @param iterable|string $abilities
48
     * @param array|mixed $arguments
49
     * @return bool
50
     */
51
    public function cant($abilities, $arguments = []): bool
52
    {
53
        return !$this->can($abilities, $arguments);
54
    }
55
56
    /**
57
     * Check if user has no permission.
58
     *
59
     * @param iterable|string $abilities
60
     * @param array|mixed $arguments
61
     * @return bool
62
     */
63
    public function cannot($abilities, $arguments = []): bool
64
    {
65
        return $this->cant($abilities, $arguments);
66
    }
67
68
    /**
69
     * Check if user is administrator.
70
     *
71
     * @return bool
72
     */
73
    public function isAdministrator(): bool
74
    {
75
        return $this->isRole('administrator');
76
    }
77
78
    /**
79
     * Check if user is $role.
80
     *
81
     * @param string $role
82
     *
83
     * @return bool
84
     */
85
    public function isRole(string $role): bool
86
    {
87
        return $this->roles->pluck('slug')->contains($role);
88
    }
89
90
    /**
91
     * Check if user in $roles.
92
     *
93
     * @param array $roles
94
     *
95
     * @return bool
96
     */
97
    public function inRoles(array $roles = []): bool
98
    {
99
        return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty();
100
    }
101
102
    /**
103
     * If visible for roles.
104
     *
105
     * @param $roles
106
     *
107
     * @return bool
108
     */
109
    public function visible(array $roles = []): bool
110
    {
111
        if (empty($roles)) {
112
            return true;
113
        }
114
115
        $roles = array_column($roles, 'slug');
116
117
        return $this->inRoles($roles) || $this->isAdministrator();
118
    }
119
120
    /**
121
     * Detach models from the relationship.
122
     *
123
     * @return void
124
     */
125
    protected static function bootHasPermissions()
126
    {
127
        static::deleting(function ($model) {
128
            $model->roles()->detach();
129
130
            $model->permissions()->detach();
131
        });
132
    }
133
}
134