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 |
|
|
|
|
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'); |
|
|
|
|
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'); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.