Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

src/Auth/Database/HasPermissions.php (3 issues)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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...
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 $permission
23
     *
24
     * @return bool
25
     */
26
    public function can(string $permission) : bool
27
    {
28
        if ($this->isAdministrator()) {
29
            return true;
30
        }
31
32
        if ($this->permissions->pluck('slug')->contains($permission)) {
33
            return true;
34
        }
35
36
        return $this->roles->pluck('permissions')->flatten()->pluck('slug')->contains($permission);
0 ignored issues
show
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...
37
    }
38
39
    /**
40
     * Check if user has no permission.
41
     *
42
     * @param $permission
43
     *
44
     * @return bool
45
     */
46
    public function cannot(string $permission) : bool
47
    {
48
        return !$this->can($permission);
49
    }
50
51
    /**
52
     * Check if user is administrator.
53
     *
54
     * @return mixed
55
     */
56
    public function isAdministrator() : bool
57
    {
58
        return $this->isRole('administrator');
59
    }
60
61
    /**
62
     * Check if user is $role.
63
     *
64
     * @param string $role
65
     *
66
     * @return mixed
67
     */
68
    public function isRole(string $role) : bool
69
    {
70
        return $this->roles->pluck('slug')->contains($role);
71
    }
72
73
    /**
74
     * Check if user in $roles.
75
     *
76
     * @param array $roles
77
     *
78
     * @return mixed
79
     */
80
    public function inRoles(array $roles = []) : bool
81
    {
82
        return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty();
83
    }
84
85
    /**
86
     * If visible for roles.
87
     *
88
     * @param $roles
89
     *
90
     * @return bool
91
     */
92
    public function visible(array $roles = []) : bool
93
    {
94
        if (empty($roles)) {
95
            return true;
96
        }
97
98
        $roles = array_column($roles, 'slug');
99
100
        return $this->inRoles($roles) || $this->isAdministrator();
101
    }
102
103
    /**
104
     * Detach models from the relationship.
105
     *
106
     * @return void
107
     */
108
    protected static function boot()
109
    {
110
        parent::boot();
111
112
        static::deleting(function ($model) {
113
            $model->roles()->detach();
114
115
            $model->permissions()->detach();
116
        });
117
    }
118
}
119