1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\Permission\Contracts\Role; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Spatie\Permission\PermissionRegistrar; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
10
|
|
|
|
11
|
|
|
trait HasRoles |
12
|
|
|
{ |
13
|
|
|
use HasPermissions; |
14
|
|
|
|
15
|
|
|
private $roleClass; |
16
|
|
|
|
17
|
|
|
public static function bootHasRoles() |
18
|
|
|
{ |
19
|
|
|
static::deleting(function ($model) { |
20
|
|
|
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$model->roles()->detach(); |
25
|
|
|
}); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getRoleClass() |
29
|
|
|
{ |
30
|
|
|
if (! isset($this->roleClass)) { |
31
|
|
|
$this->roleClass = app(PermissionRegistrar::class)->getRoleClass(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->roleClass; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A model may have multiple roles. |
39
|
|
|
*/ |
40
|
|
|
public function roles(): MorphToMany |
41
|
|
|
{ |
42
|
|
|
return $this->morphToMany( |
|
|
|
|
43
|
|
|
config('permission.models.role'), |
44
|
|
|
'model', |
45
|
|
|
config('permission.table_names.model_has_roles'), |
46
|
|
|
config('permission.column_names.model_morph_key'), |
47
|
|
|
'role_id' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Scope the model query to certain roles only. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
55
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
56
|
|
|
* @param string $guard |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
59
|
|
|
*/ |
60
|
|
|
public function scopeRole(Builder $query, $roles, $guard = null): Builder |
61
|
|
|
{ |
62
|
|
|
if ($roles instanceof Collection) { |
63
|
|
|
$roles = $roles->all(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (! is_array($roles)) { |
67
|
|
|
$roles = [$roles]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$roles = array_map(function ($role) use ($guard) { |
71
|
|
|
if ($role instanceof Role) { |
72
|
|
|
return $role; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$method = is_numeric($role) ? 'findById' : 'findByName'; |
76
|
|
|
$guard = $guard ?: $this->getDefaultGuardName(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return $this->getRoleClass()->{$method}($role, $guard); |
79
|
|
|
}, $roles); |
80
|
|
|
|
81
|
|
|
return $query->whereHas('roles', function ($query) use ($roles) { |
82
|
|
|
$query->where(function ($query) use ($roles) { |
83
|
|
|
foreach ($roles as $role) { |
84
|
|
|
$query->orWhere(config('permission.table_names.roles').'.id', $role->id); |
85
|
|
|
} |
86
|
|
|
}); |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Scope the model query to certain roles only. |
92
|
|
|
* This will not return an exception if the role does not exist. |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
95
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
96
|
|
|
* @param string $guard |
97
|
|
|
* |
98
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
99
|
|
|
*/ |
100
|
|
|
public function scopeWhereRole(Builder $query, $roles, $guard = null): Builder |
101
|
|
|
{ |
102
|
|
|
if ($roles instanceof Collection) { |
103
|
|
|
$roles = $roles->all(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (! is_array($roles)) { |
107
|
|
|
$roles = [$roles]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$roles = collect($roles)->map(function ($role) { |
111
|
|
|
if ($role instanceof Role) { |
112
|
|
|
return $role->id; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $role; |
116
|
|
|
})->unique(); |
117
|
|
|
|
118
|
|
|
$guard = is_null($guard) ? $this->getDefaultGuardName() : $guard; |
119
|
|
|
|
120
|
|
|
return $query->whereHas('roles', function ($query) use ($roles, $guard) { |
121
|
|
|
$query->where(function ($query) use ($roles, $guard) { |
122
|
|
|
foreach ($roles as $role) { |
123
|
|
|
$column = is_numeric($role) ? 'id' : 'name'; |
124
|
|
|
$query->orWhere([ |
125
|
|
|
[config('permission.table_names.roles').".{$column}", '=', $role], |
126
|
|
|
[config('permission.table_names.roles').'.guard_name', '=', $guard], |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
}); |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Assign the given role to the model. |
135
|
|
|
* |
136
|
|
|
* @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function assignRole(...$roles) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$roles = collect($roles) |
143
|
|
|
->flatten() |
144
|
|
|
->map(function ($role) { |
145
|
|
|
if (empty($role)) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this->getStoredRole($role); |
150
|
|
|
}) |
151
|
|
|
->filter(function ($role) { |
152
|
|
|
return $role instanceof Role; |
153
|
|
|
}) |
154
|
|
|
->each(function ($role) { |
155
|
|
|
$this->ensureModelSharesGuard($role); |
156
|
|
|
}) |
157
|
|
|
->map->id |
158
|
|
|
->all(); |
159
|
|
|
|
160
|
|
|
$model = $this->getModel(); |
|
|
|
|
161
|
|
|
|
162
|
|
|
if ($model->exists) { |
163
|
|
|
$this->roles()->sync($roles, false); |
164
|
|
|
$model->load('roles'); |
165
|
|
|
} else { |
166
|
|
|
$class = \get_class($model); |
167
|
|
|
|
168
|
|
|
$class::saved( |
169
|
|
|
function ($object) use ($roles, $model) { |
170
|
|
|
static $modelLastFiredOn; |
171
|
|
|
if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
$object->roles()->sync($roles, false); |
175
|
|
|
$object->load('roles'); |
176
|
|
|
$modelLastFiredOn = $object; |
177
|
|
|
}); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->forgetCachedPermissions(); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Revoke the given role from the model. |
187
|
|
|
* |
188
|
|
|
* @param string|\Spatie\Permission\Contracts\Role $role |
189
|
|
|
*/ |
190
|
|
|
public function removeRole($role) |
191
|
|
|
{ |
192
|
|
|
$this->roles()->detach($this->getStoredRole($role)); |
193
|
|
|
|
194
|
|
|
$this->load('roles'); |
|
|
|
|
195
|
|
|
|
196
|
|
|
$this->forgetCachedPermissions(); |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Remove all current roles and set the given ones. |
203
|
|
|
* |
204
|
|
|
* @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
205
|
|
|
* |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
public function syncRoles(...$roles) |
209
|
|
|
{ |
210
|
|
|
$this->roles()->detach(); |
211
|
|
|
|
212
|
|
|
return $this->assignRole($roles); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Determine if the model has (one of) the given role(s). |
217
|
|
|
* |
218
|
|
|
* @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
219
|
|
|
* @param string|null $guard |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function hasRole($roles, string $guard = null): bool |
223
|
|
|
{ |
224
|
|
View Code Duplication |
if (is_string($roles) && false !== strpos($roles, '|')) { |
|
|
|
|
225
|
|
|
$roles = $this->convertPipeToArray($roles); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
View Code Duplication |
if (is_string($roles)) { |
|
|
|
|
229
|
|
|
return $guard |
230
|
|
|
? $this->roles->where('guard_name', $guard)->contains('name', $roles) |
|
|
|
|
231
|
|
|
: $this->roles->contains('name', $roles); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
if (is_int($roles)) { |
|
|
|
|
235
|
|
|
return $guard |
236
|
|
|
? $this->roles->where('guard_name', $guard)->contains('id', $roles) |
237
|
|
|
: $this->roles->contains('id', $roles); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ($roles instanceof Role) { |
241
|
|
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (is_array($roles)) { |
245
|
|
|
foreach ($roles as $role) { |
246
|
|
|
if ($this->hasRole($role, $guard)) { |
247
|
|
|
return true; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $roles->intersect($guard ? $this->roles->where('guard_name', $guard) : $this->roles)->isNotEmpty(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Determine if the model has any of the given role(s). |
259
|
|
|
* |
260
|
|
|
* Alias to hasRole() but without Guard controls |
261
|
|
|
* |
262
|
|
|
* @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
263
|
|
|
* |
264
|
|
|
* @return bool |
265
|
|
|
*/ |
266
|
|
|
public function hasAnyRole(...$roles): bool |
267
|
|
|
{ |
268
|
|
|
return $this->hasRole($roles); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Determine if the model has all of the given role(s). |
273
|
|
|
* |
274
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
275
|
|
|
* @param string|null $guard |
276
|
|
|
* @return bool |
277
|
|
|
*/ |
278
|
|
|
public function hasAllRoles($roles, string $guard = null): bool |
279
|
|
|
{ |
280
|
|
View Code Duplication |
if (is_string($roles) && false !== strpos($roles, '|')) { |
|
|
|
|
281
|
|
|
$roles = $this->convertPipeToArray($roles); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
View Code Duplication |
if (is_string($roles)) { |
|
|
|
|
285
|
|
|
return $guard |
286
|
|
|
? $this->roles->where('guard_name', $guard)->contains('name', $roles) |
287
|
|
|
: $this->roles->contains('name', $roles); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if ($roles instanceof Role) { |
291
|
|
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$roles = collect()->make($roles)->map(function ($role) { |
295
|
|
|
return $role instanceof Role ? $role->name : $role; |
|
|
|
|
296
|
|
|
}); |
297
|
|
|
|
298
|
|
|
return $roles->intersect( |
299
|
|
|
$guard |
300
|
|
|
? $this->roles->where('guard_name', $guard)->pluck('name') |
301
|
|
|
: $this->getRoleNames()) == $roles; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Return all permissions directly coupled to the model. |
306
|
|
|
*/ |
307
|
|
|
public function getDirectPermissions(): Collection |
308
|
|
|
{ |
309
|
|
|
return $this->permissions; |
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function getRoleNames(): Collection |
313
|
|
|
{ |
314
|
|
|
return $this->roles->pluck('name'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
protected function getStoredRole($role): Role |
318
|
|
|
{ |
319
|
|
|
$roleClass = $this->getRoleClass(); |
320
|
|
|
|
321
|
|
|
if (is_numeric($role)) { |
322
|
|
|
return $roleClass->findById($role, $this->getDefaultGuardName()); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if (is_string($role)) { |
326
|
|
|
return $roleClass->findByName($role, $this->getDefaultGuardName()); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $role; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
protected function convertPipeToArray(string $pipeString) |
333
|
|
|
{ |
334
|
|
|
$pipeString = trim($pipeString); |
335
|
|
|
|
336
|
|
|
if (strlen($pipeString) <= 2) { |
337
|
|
|
return $pipeString; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
$quoteCharacter = substr($pipeString, 0, 1); |
341
|
|
|
$endCharacter = substr($quoteCharacter, -1, 1); |
342
|
|
|
|
343
|
|
|
if ($quoteCharacter !== $endCharacter) { |
344
|
|
|
return explode('|', $pipeString); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
if (! in_array($quoteCharacter, ["'", '"'])) { |
348
|
|
|
return explode('|', $pipeString); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return explode('|', trim($pipeString, $quoteCharacter)); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
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.