Completed
Pull Request — master (#2254)
by
unknown
11:30
created

HasPermissions::roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 8
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Auth\Database;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Storage;
8
9
trait HasPermissions
10
{
11
    /**
12
     * Get avatar attribute.
13
     *
14
     * @param string $avatar
15
     *
16
     * @return string
17
     */
18
    public function getAvatarAttribute($avatar)
19
    {
20
        if ($avatar) {
21
            return Storage::disk(config('admin.upload.disk'))->url($avatar);
22
        }
23
24
        return admin_asset('/vendor/laravel-admin/AdminLTE/dist/img/user2-160x160.jpg');
25
    }
26
27
    /**
28
     * A user has and belongs to many roles.
29
     *
30
     * @return BelongsToMany
31
     */
32 View Code Duplication
    public function roles() : BelongsToMany
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $pivotTable = config('admin.database.role_users_table');
35
36
        $relatedModel = config('admin.database.roles_model');
37
38
        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id');
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() 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...
39
    }
40
41
    /**
42
     * A User has and belongs to many permissions.
43
     *
44
     * @return BelongsToMany
45
     */
46
    public function permissions() : BelongsToMany
47
    {
48
        $pivotTable = config('admin.database.user_permissions_table');
49
50
        $relatedModel = config('admin.database.permissions_model');
51
52
        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'permission_id');
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() 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...
53
    }
54
55
    /**
56
     * Get all permissions of user.
57
     *
58
     * @return mixed
59
     */
60
    public function allPermissions() : Collection
61
    {
62
        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...
63
    }
64
65
    /**
66
     * Check if user has permission.
67
     *
68
     * @param $permission
69
     *
70
     * @return bool
71
     */
72
    public function can(string $permission) : bool
73
    {
74
        if ($this->isAdministrator()) {
75
            return true;
76
        }
77
78
        if ($this->permissions->pluck('slug')->contains($permission)) {
79
            return true;
80
        }
81
82
        return $this->roles->pluck('permissions')->flatten()->pluck('slug')->contains($permission);
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...
83
    }
84
85
    /**
86
     * Check if user has no permission.
87
     *
88
     * @param $permission
89
     *
90
     * @return bool
91
     */
92
    public function cannot(string $permission) : bool
93
    {
94
        return !$this->can($permission);
95
    }
96
97
    /**
98
     * Check if user is administrator.
99
     *
100
     * @return mixed
101
     */
102
    public function isAdministrator() : bool
103
    {
104
        return $this->isRole('administrator');
105
    }
106
107
    /**
108
     * Check if user is $role.
109
     *
110
     * @param string $role
111
     *
112
     * @return mixed
113
     */
114
    public function isRole(string $role) : bool
115
    {
116
        return $this->roles->pluck('slug')->contains($role);
117
    }
118
119
    /**
120
     * Check if user in $roles.
121
     *
122
     * @param array $roles
123
     *
124
     * @return mixed
125
     */
126
    public function inRoles(array $roles = []) : bool
127
    {
128
        return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty();
129
    }
130
131
    /**
132
     * If visible for roles.
133
     *
134
     * @param $roles
135
     *
136
     * @return bool
137
     */
138
    public function visible(array $roles = []) : bool
139
    {
140
        if (empty($roles)) {
141
            return true;
142
        }
143
144
        $roles = array_column($roles, 'slug');
145
146
        return $this->inRoles($roles) || $this->isAdministrator();
147
    }
148
149
    /**
150
     * Detach models from the relationship.
151
     *
152
     * @return void
153
     */
154
    protected static function boot()
155
    {
156
        parent::boot();
157
158
        static::deleting(function ($model) {
159
            $model->roles()->detach();
160
161
            $model->permissions()->detach();
162
        });
163
    }
164
}
165