1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Traits; |
4
|
|
|
|
5
|
|
|
use Spatie\Permission\Contracts\Permission; |
6
|
|
|
use Spatie\Permission\Contracts\Role; |
7
|
|
|
|
8
|
|
|
trait HasRoles |
9
|
|
|
{ |
10
|
|
|
use HasPermissions; |
11
|
|
|
use RefreshesPermissionCache; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A user may have multiple roles. |
15
|
|
|
* |
16
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
17
|
|
|
*/ |
18
|
|
|
public function roles() |
19
|
|
|
{ |
20
|
|
|
return $this->belongsToMany( |
|
|
|
|
21
|
|
|
config('laravel-permission.models.role'), |
22
|
|
|
config('laravel-permission.table_names.user_has_roles') |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A user may have multiple direct permissions. |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
30
|
|
|
*/ |
31
|
|
|
public function permissions() |
32
|
|
|
{ |
33
|
|
|
return $this->belongsToMany( |
|
|
|
|
34
|
|
|
config('laravel-permission.models.permission'), |
35
|
|
|
config('laravel-permission.table_names.user_has_permissions') |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Scope the user query to certain roles only |
41
|
|
|
* |
42
|
|
|
* @param string|array|Role|\Illuminate\Support\Collection $roles |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function scopeRole($query, $roles) |
47
|
|
|
{ |
48
|
|
|
if (is_string($roles)) { |
49
|
|
|
return $query->whereHas('roles', function ($query) use ($roles) { |
50
|
|
|
$query->where('name', $roles); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($roles instanceof Role) { |
55
|
|
|
return $query->whereHas('roles', function ($query) use ($roles) { |
56
|
|
|
$query->where('id', $roles->id); |
|
|
|
|
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (is_array($roles)) { |
61
|
|
|
return $query->whereHas('roles', function ($query) use ($roles) { |
62
|
|
|
$query->where(function($query) use ($roles) { |
63
|
|
|
foreach ($roles as $role) { |
64
|
|
|
if (is_string($role)) { |
65
|
|
|
$query->orWhere('name', $role); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($role instanceof Role) { |
69
|
|
|
$query->orWhere('id', $role->id); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
}); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $query; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Assign the given role to the user. |
81
|
|
|
* |
82
|
|
|
* @param array|string|\Spatie\Permission\Models\Role ...$roles |
83
|
|
|
* |
84
|
|
|
* @return \Spatie\Permission\Contracts\Role |
85
|
|
|
*/ |
86
|
|
|
public function assignRole(...$roles) |
87
|
|
|
{ |
88
|
|
|
$roles = collect($roles) |
89
|
|
|
->flatten() |
90
|
|
|
->map(function ($role) { |
91
|
|
|
return $this->getStoredRole($role); |
92
|
|
|
}) |
93
|
|
|
->all(); |
94
|
|
|
|
95
|
|
|
$this->roles()->saveMany($roles); |
96
|
|
|
|
97
|
|
|
$this->forgetCachedPermissions(); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Revoke the given role from the user. |
104
|
|
|
* |
105
|
|
|
* @param string|Role $role |
106
|
|
|
*/ |
107
|
|
|
public function removeRole($role) |
108
|
|
|
{ |
109
|
|
|
$this->roles()->detach($this->getStoredRole($role)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Remove all current roles and set the given ones. |
114
|
|
|
* |
115
|
|
|
* @param array ...$roles |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function syncRoles(...$roles) |
120
|
|
|
{ |
121
|
|
|
$this->roles()->detach(); |
122
|
|
|
|
123
|
|
|
return $this->assignRole($roles); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Determine if the user has (one of) the given role(s). |
128
|
|
|
* |
129
|
|
|
* @param string|array|Role|\Illuminate\Support\Collection $roles |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function hasRole($roles) |
134
|
|
|
{ |
135
|
|
|
if (is_string($roles)) { |
136
|
|
|
return $this->roles->contains('name', $roles); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($roles instanceof Role) { |
140
|
|
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (is_array($roles)) { |
144
|
|
|
foreach ($roles as $role) { |
145
|
|
|
if ($this->hasRole($role)) { |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return (bool) $roles->intersect($this->roles)->count(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Determine if the user has any of the given role(s). |
158
|
|
|
* |
159
|
|
|
* @param string|array|Role|\Illuminate\Support\Collection $roles |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function hasAnyRole($roles) |
164
|
|
|
{ |
165
|
|
|
return $this->hasRole($roles); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Determine if the user has all of the given role(s). |
170
|
|
|
* |
171
|
|
|
* @param string|Role|\Illuminate\Support\Collection $roles |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function hasAllRoles($roles) |
176
|
|
|
{ |
177
|
|
|
if (is_string($roles)) { |
178
|
|
|
return $this->roles->contains('name', $roles); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($roles instanceof Role) { |
182
|
|
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$roles = collect()->make($roles)->map(function ($role) { |
186
|
|
|
return $role instanceof Role ? $role->name : $role; |
|
|
|
|
187
|
|
|
}); |
188
|
|
|
|
189
|
|
|
return $roles->intersect($this->roles->pluck('name')) == $roles; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Determine if the user may perform the given permission. |
194
|
|
|
* |
195
|
|
|
* @param string|Permission $permission |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function hasPermissionTo($permission) |
200
|
|
|
{ |
201
|
|
|
if (is_string($permission)) { |
202
|
|
|
$permission = app(Permission::class)->findByName($permission); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @deprecated deprecated since version 1.0.1, use hasPermissionTo instead |
210
|
|
|
* |
211
|
|
|
* Determine if the user may perform the given permission. |
212
|
|
|
* |
213
|
|
|
* @param Permission $permission |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function hasPermission($permission) |
218
|
|
|
{ |
219
|
|
|
return $this->hasPermissionTo($permission); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Determine if the user has, via roles, the given permission. |
224
|
|
|
* |
225
|
|
|
* @param Permission $permission |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
protected function hasPermissionViaRole(Permission $permission) |
230
|
|
|
{ |
231
|
|
|
return $this->hasRole($permission->roles); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Determine if the user has the given permission. |
236
|
|
|
* |
237
|
|
|
* @param string|Permission $permission |
238
|
|
|
* |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
protected function hasDirectPermission($permission) |
242
|
|
|
{ |
243
|
|
|
if (is_string($permission)) { |
244
|
|
|
$permission = app(Permission::class)->findByName($permission); |
245
|
|
|
|
246
|
|
|
if (!$permission) { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this->permissions->contains('id', $permission->id); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param $role |
256
|
|
|
* |
257
|
|
|
* @return Role |
258
|
|
|
*/ |
259
|
|
|
protected function getStoredRole($role) |
260
|
|
|
{ |
261
|
|
|
if (is_string($role)) { |
262
|
|
|
return app(Role::class)->findByName($role); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return $role; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.