Completed
Push — master ( 6a8d73...24a186 )
by Song
02:47
created

HasPermissions::visible()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
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 $permission
48
     *
49
     * @return bool
50
     */
51
    public function cannot(string $permission) : bool
52
    {
53
        return !$this->can($permission);
54
    }
55
56
    /**
57
     * Check if user is administrator.
58
     *
59
     * @return mixed
60
     */
61
    public function isAdministrator() : bool
62
    {
63
        return $this->isRole('administrator');
64
    }
65
66
    /**
67
     * Check if user is $role.
68
     *
69
     * @param string $role
70
     *
71
     * @return mixed
72
     */
73
    public function isRole(string $role) : bool
74
    {
75
        return $this->roles->pluck('slug')->contains($role);
76
    }
77
78
    /**
79
     * Check if user in $roles.
80
     *
81
     * @param array $roles
82
     *
83
     * @return mixed
84
     */
85
    public function inRoles(array $roles = []) : bool
86
    {
87
        return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty();
88
    }
89
90
    /**
91
     * If visible for roles.
92
     *
93
     * @param $roles
94
     *
95
     * @return bool
96
     */
97
    public function visible(array $roles = []) : bool
98
    {
99
        if (empty($roles)) {
100
            return true;
101
        }
102
103
        $roles = array_column($roles, 'slug');
104
105
        return $this->inRoles($roles) || $this->isAdministrator();
106
    }
107
108
    /**
109
     * Detach models from the relationship.
110
     *
111
     * @return void
112
     */
113
    protected static function bootHasPermissions()
114
    {
115
        static::deleting(function ($model) {
116
            $model->roles()->detach();
117
118
            $model->permissions()->detach();
119
        });
120
    }
121
}
122