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\Contracts\Permission; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
10
|
|
|
|
11
|
|
|
trait HasRoles |
12
|
|
|
{ |
13
|
|
|
use HasPermissions; |
14
|
|
|
|
15
|
|
|
public static function bootHasRoles() |
16
|
|
|
{ |
17
|
|
|
static::deleting(function ($model) { |
18
|
|
|
$model->roles()->detach(); |
19
|
|
|
$model->permissions()->detach(); |
20
|
|
|
}); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A model may have multiple roles. |
25
|
|
|
*/ |
26
|
|
|
public function roles(): MorphToMany |
27
|
|
|
{ |
28
|
|
|
return $this->morphToMany( |
|
|
|
|
29
|
|
|
config('permission.models.role'), |
30
|
|
|
'model', |
31
|
|
|
config('permission.table_names.model_has_roles'), |
32
|
|
|
'model_id', |
33
|
|
|
'role_id' |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A model may have multiple direct permissions. |
39
|
|
|
*/ |
40
|
|
|
public function permissions(): MorphToMany |
41
|
|
|
{ |
42
|
|
|
return $this->morphToMany( |
|
|
|
|
43
|
|
|
config('permission.models.permission'), |
44
|
|
|
'model', |
45
|
|
|
config('permission.table_names.model_has_permissions'), |
46
|
|
|
'model_id', |
47
|
|
|
'permission_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
|
|
|
* |
57
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
58
|
|
|
*/ |
59
|
|
|
public function scopeRole(Builder $query, $roles): Builder |
60
|
|
|
{ |
61
|
|
|
if ($roles instanceof Collection) { |
62
|
|
|
$roles = $roles->toArray(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (! is_array($roles)) { |
66
|
|
|
$roles = [$roles]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$roles = array_map(function ($role) { |
70
|
|
|
if ($role instanceof Role) { |
71
|
|
|
return $role; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return app(Role::class)->findByName($role, $this->getDefaultGuardName()); |
75
|
|
|
}, $roles); |
76
|
|
|
|
77
|
|
|
return $query->whereHas('roles', function ($query) use ($roles) { |
78
|
|
|
$query->where(function ($query) use ($roles) { |
79
|
|
|
foreach ($roles as $role) { |
80
|
|
|
$query->orWhere(config('permission.table_names.roles').'.id', $role->id); |
81
|
|
|
} |
82
|
|
|
}); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function convertToPermissionModels($permissions): array |
92
|
|
|
{ |
93
|
|
|
if ($permissions instanceof Collection) { |
94
|
|
|
$permissions = $permissions->toArray(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$permissions = array_wrap($permissions); |
98
|
|
|
|
99
|
|
|
return array_map(function ($permission) { |
100
|
|
|
if ($permission instanceof Permission) { |
101
|
|
|
return $permission; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return app(Permission::class)->findByName($permission, $this->getDefaultGuardName()); |
105
|
|
|
}, $permissions); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Scope the model query to certain permissions only. |
110
|
|
|
* |
111
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
112
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
113
|
|
|
* |
114
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
115
|
|
|
*/ |
116
|
|
|
public function scopePermission(Builder $query, $permissions): Builder |
117
|
|
|
{ |
118
|
|
|
$permissions = $this->convertToPermissionModels($permissions); |
119
|
|
|
|
120
|
|
|
$rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) { |
121
|
|
|
return array_merge($result, $permission->roles->all()); |
122
|
|
|
}, [])); |
123
|
|
|
|
124
|
|
|
return $query-> |
125
|
|
|
where(function ($query) use ($permissions, $rolesWithPermissions) { |
126
|
|
|
$query->whereHas('permissions', function ($query) use ($permissions) { |
127
|
|
|
$query->where(function ($query) use ($permissions) { |
128
|
|
|
foreach ($permissions as $permission) { |
129
|
|
|
$query->orWhere(config('permission.table_names.permissions').'.id', $permission->id); |
130
|
|
|
} |
131
|
|
|
}); |
132
|
|
|
}); |
133
|
|
|
if (count($rolesWithPermissions) > 0) { |
134
|
|
|
$query->orWhereHas('roles', function ($query) use ($rolesWithPermissions) { |
135
|
|
|
$query->where(function ($query) use ($rolesWithPermissions) { |
136
|
|
|
foreach ($rolesWithPermissions as $role) { |
137
|
|
|
$query->orWhere(config('permission.table_names.roles').'.id', $role->id); |
138
|
|
|
} |
139
|
|
|
}); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Assign the given role to the model. |
147
|
|
|
* |
148
|
|
|
* @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function assignRole(...$roles) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$roles = collect($roles) |
155
|
|
|
->flatten() |
156
|
|
|
->map(function ($role) { |
157
|
|
|
return $this->getStoredRole($role); |
158
|
|
|
}) |
159
|
|
|
->each(function ($role) { |
160
|
|
|
$this->ensureModelSharesGuard($role); |
161
|
|
|
}) |
162
|
|
|
->all(); |
163
|
|
|
|
164
|
|
|
$this->roles()->saveMany($roles); |
165
|
|
|
|
166
|
|
|
$this->forgetCachedPermissions(); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Revoke the given role from the model. |
173
|
|
|
* |
174
|
|
|
* @param string|\Spatie\Permission\Contracts\Role $role |
175
|
|
|
*/ |
176
|
|
|
public function removeRole($role) |
177
|
|
|
{ |
178
|
|
|
$this->roles()->detach($this->getStoredRole($role)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Remove all current roles and set the given ones. |
183
|
|
|
* |
184
|
|
|
* @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
185
|
|
|
* |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function syncRoles(...$roles) |
189
|
|
|
{ |
190
|
|
|
$this->roles()->detach(); |
191
|
|
|
|
192
|
|
|
return $this->assignRole($roles); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Determine if the model has (one of) the given role(s). |
197
|
|
|
* |
198
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
199
|
|
|
* |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
public function hasRole($roles): bool |
203
|
|
|
{ |
204
|
|
View Code Duplication |
if (is_string($roles) && false !== strpos($roles, '|')) { |
|
|
|
|
205
|
|
|
$roles = $this->convertPipeToArray($roles); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (is_string($roles)) { |
209
|
|
|
return $this->roles->contains('name', $roles); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if ($roles instanceof Role) { |
213
|
|
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if (is_array($roles)) { |
217
|
|
|
foreach ($roles as $role) { |
218
|
|
|
if ($this->hasRole($role)) { |
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $roles->intersect($this->roles)->isNotEmpty(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Determine if the model has any of the given role(s). |
231
|
|
|
* |
232
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
233
|
|
|
* |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
|
|
public function hasAnyRole($roles): bool |
237
|
|
|
{ |
238
|
|
|
return $this->hasRole($roles); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Determine if the model has all of the given role(s). |
243
|
|
|
* |
244
|
|
|
* @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
245
|
|
|
* |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
|
|
public function hasAllRoles($roles): bool |
249
|
|
|
{ |
250
|
|
View Code Duplication |
if (is_string($roles) && false !== strpos($roles, '|')) { |
|
|
|
|
251
|
|
|
$roles = $this->convertPipeToArray($roles); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (is_string($roles)) { |
255
|
|
|
return $this->roles->contains('name', $roles); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
if ($roles instanceof Role) { |
259
|
|
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$roles = collect()->make($roles)->map(function ($role) { |
263
|
|
|
return $role instanceof Role ? $role->name : $role; |
|
|
|
|
264
|
|
|
}); |
265
|
|
|
|
266
|
|
|
return $roles->intersect($this->roles->pluck('name')) == $roles; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Determine if the model may perform the given permission. |
271
|
|
|
* |
272
|
|
|
* @param string|\Spatie\Permission\Contracts\Permission $permission |
273
|
|
|
* @param string|null $guardName |
274
|
|
|
* |
275
|
|
|
* @return bool |
276
|
|
|
*/ |
277
|
|
|
public function hasPermissionTo($permission, $guardName = null): bool |
278
|
|
|
{ |
279
|
|
View Code Duplication |
if (is_string($permission)) { |
|
|
|
|
280
|
|
|
$permission = app(Permission::class)->findByName( |
281
|
|
|
$permission, |
282
|
|
|
$guardName ?? $this->getDefaultGuardName() |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Determine if the model has any of the given permissions. |
291
|
|
|
* |
292
|
|
|
* @param array ...$permissions |
293
|
|
|
* |
294
|
|
|
* @return bool |
295
|
|
|
*/ |
296
|
|
|
public function hasAnyPermission(...$permissions): bool |
297
|
|
|
{ |
298
|
|
|
if (is_array($permissions[0])) { |
299
|
|
|
$permissions = $permissions[0]; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
foreach ($permissions as $permission) { |
303
|
|
|
if ($this->hasPermissionTo($permission)) { |
304
|
|
|
return true; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return false; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Determine if the model has, via roles, the given permission. |
313
|
|
|
* |
314
|
|
|
* @param \Spatie\Permission\Contracts\Permission $permission |
315
|
|
|
* |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
|
|
protected function hasPermissionViaRole(Permission $permission): bool |
319
|
|
|
{ |
320
|
|
|
return $this->hasRole($permission->roles); |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Determine if the model has the given permission. |
325
|
|
|
* |
326
|
|
|
* @param string|\Spatie\Permission\Contracts\Permission $permission |
327
|
|
|
* |
328
|
|
|
* @return bool |
329
|
|
|
*/ |
330
|
|
|
public function hasDirectPermission($permission): bool |
331
|
|
|
{ |
332
|
|
View Code Duplication |
if (is_string($permission)) { |
|
|
|
|
333
|
|
|
$permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName()); |
334
|
|
|
|
335
|
|
|
if (! $permission) { |
336
|
|
|
return false; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $this->permissions->contains('id', $permission->id); |
|
|
|
|
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Return all permissions the directory coupled to the model. |
345
|
|
|
*/ |
346
|
|
|
public function getDirectPermissions(): Collection |
347
|
|
|
{ |
348
|
|
|
return $this->permissions; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Return all the permissions the model has via roles. |
353
|
|
|
*/ |
354
|
|
|
public function getPermissionsViaRoles(): Collection |
355
|
|
|
{ |
356
|
|
|
return $this->load('roles', 'roles.permissions') |
|
|
|
|
357
|
|
|
->roles->flatMap(function ($role) { |
358
|
|
|
return $role->permissions; |
359
|
|
|
})->sort()->values(); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Return all the permissions the model has, both directly and via roles. |
364
|
|
|
*/ |
365
|
|
|
public function getAllPermissions(): Collection |
366
|
|
|
{ |
367
|
|
|
return $this->permissions |
368
|
|
|
->merge($this->getPermissionsViaRoles()) |
369
|
|
|
->sort() |
370
|
|
|
->values(); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function getRoleNames(): Collection |
374
|
|
|
{ |
375
|
|
|
return $this->roles->pluck('name'); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
protected function getStoredRole($role): Role |
379
|
|
|
{ |
380
|
|
|
if (is_string($role)) { |
381
|
|
|
return app(Role::class)->findByName($role, $this->getDefaultGuardName()); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $role; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
protected function convertPipeToArray(string $pipeString) |
388
|
|
|
{ |
389
|
|
|
$pipeString = trim($pipeString); |
390
|
|
|
|
391
|
|
|
if (strlen($pipeString) <= 2) { |
392
|
|
|
return $pipeString; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
$quoteCharacter = substr($pipeString, 0, 1); |
396
|
|
|
$endCharacter = substr($quoteCharacter, -1, 1); |
397
|
|
|
|
398
|
|
|
if ($quoteCharacter !== $endCharacter) { |
399
|
|
|
return explode('|', $pipeString); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
if (! in_array($quoteCharacter, ["'", '"'])) { |
403
|
|
|
return explode('|', $pipeString); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
return explode('|', trim($pipeString, $quoteCharacter)); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
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.