1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Traits; |
4
|
|
|
|
5
|
|
|
use Spatie\Permission\Exceptions\WildcardPermissionInvalidArgument; |
6
|
|
|
use Spatie\Permission\Exceptions\WildcardPermissionNotProperlyFormatted; |
7
|
|
|
use Spatie\Permission\Guard; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Spatie\Permission\WildcardPermission; |
11
|
|
|
use Spatie\Permission\PermissionRegistrar; |
12
|
|
|
use Spatie\Permission\Contracts\Permission; |
13
|
|
|
use Spatie\Permission\Exceptions\GuardDoesNotMatch; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
15
|
|
|
use Spatie\Permission\Exceptions\PermissionDoesNotExist; |
16
|
|
|
|
17
|
|
|
trait HasPermissions |
18
|
|
|
{ |
19
|
|
|
private $permissionClass; |
20
|
|
|
|
21
|
|
|
public static function bootHasPermissions() |
22
|
|
|
{ |
23
|
|
|
static::deleting(function ($model) { |
24
|
|
|
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$model->permissions()->detach(); |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getPermissionClass() |
33
|
|
|
{ |
34
|
|
|
if (! isset($this->permissionClass)) { |
35
|
|
|
$this->permissionClass = app(PermissionRegistrar::class)->getPermissionClass(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->permissionClass; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* A model may have multiple direct permissions. |
43
|
|
|
*/ |
44
|
|
|
public function permissions(): MorphToMany |
45
|
|
|
{ |
46
|
|
|
return $this->morphToMany( |
|
|
|
|
47
|
|
|
config('permission.models.permission'), |
48
|
|
|
'model', |
49
|
|
|
config('permission.table_names.model_has_permissions'), |
50
|
|
|
config('permission.column_names.model_morph_key'), |
51
|
|
|
'permission_id' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Scope the model query to certain permissions only. |
57
|
|
|
* |
58
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
59
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
62
|
|
|
*/ |
63
|
|
|
public function scopePermission(Builder $query, $permissions): Builder |
64
|
|
|
{ |
65
|
|
|
$permissions = $this->convertToPermissionModels($permissions); |
66
|
|
|
|
67
|
|
|
$rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) { |
68
|
|
|
return array_merge($result, $permission->roles->all()); |
69
|
|
|
}, [])); |
70
|
|
|
|
71
|
|
|
return $query->where(function (Builder $query) use ($permissions, $rolesWithPermissions) { |
72
|
|
|
$query->whereHas('permissions', function (Builder $subQuery) use ($permissions) { |
73
|
|
|
$subQuery->whereIn(config('permission.table_names.permissions').'.id', \array_column($permissions, 'id')); |
74
|
|
|
}); |
75
|
|
|
if (count($rolesWithPermissions) > 0) { |
76
|
|
|
$query->orWhereHas('roles', function (Builder $subQuery) use ($rolesWithPermissions) { |
77
|
|
|
$subQuery->whereIn(config('permission.table_names.roles').'.id', \array_column($rolesWithPermissions, 'id')); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function convertToPermissionModels($permissions): array |
89
|
|
|
{ |
90
|
|
|
if ($permissions instanceof Collection) { |
91
|
|
|
$permissions = $permissions->all(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$permissions = is_array($permissions) ? $permissions : [$permissions]; |
95
|
|
|
|
96
|
|
|
return array_map(function ($permission) { |
97
|
|
|
if ($permission instanceof Permission) { |
98
|
|
|
return $permission; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName()); |
102
|
|
|
}, $permissions); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Determine if the model may perform the given permission. |
107
|
|
|
* |
108
|
|
|
* @param string|int|\Spatie\Permission\Contracts\Permission $permission |
109
|
|
|
* @param string|null $guardName |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
* @throws PermissionDoesNotExist |
113
|
|
|
*/ |
114
|
|
|
public function hasPermissionTo($permission, $guardName = null): bool |
115
|
|
|
{ |
116
|
|
|
if (config('permission.enable_wildcard_permission', false)) { |
117
|
|
|
return $this->hasWildcardPermission($permission, $guardName); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$permissionClass = $this->getPermissionClass(); |
121
|
|
|
|
122
|
|
|
if (is_string($permission)) { |
123
|
|
|
$permission = $permissionClass->findByName( |
124
|
|
|
$permission, |
125
|
|
|
$guardName ?? $this->getDefaultGuardName() |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (is_int($permission)) { |
130
|
|
|
$permission = $permissionClass->findById( |
131
|
|
|
$permission, |
132
|
|
|
$guardName ?? $this->getDefaultGuardName() |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!$permission instanceof Permission) { |
137
|
|
|
throw new PermissionDoesNotExist; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Validates a wildcard permission against all permissions of a user. |
145
|
|
|
* |
146
|
|
|
* @param string|int|\Spatie\Permission\Contracts\Permission $permission |
147
|
|
|
* @param string|null $guardName |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
protected function hasWildcardPermission($permission, $guardName = null): bool |
152
|
|
|
{ |
153
|
|
|
$guardName = $guardName ?? $this->getDefaultGuardName(); |
154
|
|
|
|
155
|
|
|
if (is_int($permission)) { |
156
|
|
|
$permission = $this->getPermissionClass()->findById($permission, $guardName); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($permission instanceof Permission) { |
160
|
|
|
$permission = $permission->name; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (! is_string($permission)) { |
164
|
|
|
throw WildcardPermissionInvalidArgument::create(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
foreach ($this->getAllPermissions() as $userPermission) { |
168
|
|
|
if ($guardName !== $userPermission->guard_name) { |
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$userPermission = new WildcardPermission($userPermission->name); |
173
|
|
|
|
174
|
|
|
if ($userPermission->implies($permission)) { |
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @deprecated since 2.35.0 |
184
|
|
|
* @alias of hasPermissionTo() |
185
|
|
|
*/ |
186
|
|
|
public function hasUncachedPermissionTo($permission, $guardName = null): bool |
187
|
|
|
{ |
188
|
|
|
return $this->hasPermissionTo($permission, $guardName); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* An alias to hasPermissionTo(), but avoids throwing an exception. |
193
|
|
|
* |
194
|
|
|
* @param string|int|\Spatie\Permission\Contracts\Permission $permission |
195
|
|
|
* @param string|null $guardName |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function checkPermissionTo($permission, $guardName = null): bool |
200
|
|
|
{ |
201
|
|
|
try { |
202
|
|
|
return $this->hasPermissionTo($permission, $guardName); |
203
|
|
|
} catch (PermissionDoesNotExist $e) { |
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Determine if the model has any of the given permissions. |
210
|
|
|
* |
211
|
|
|
* @param array ...$permissions |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
* @throws \Exception |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
public function hasAnyPermission(...$permissions): bool |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$permissions = collect($permissions)->flatten(); |
219
|
|
|
|
220
|
|
|
foreach ($permissions as $permission) { |
221
|
|
|
if ($this->checkPermissionTo($permission)) { |
222
|
|
|
return true; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Determine if the model has all of the given permissions. |
231
|
|
|
* |
232
|
|
|
* @param array ...$permissions |
233
|
|
|
* |
234
|
|
|
* @return bool |
235
|
|
|
* @throws \Exception |
236
|
|
|
*/ |
237
|
|
View Code Duplication |
public function hasAllPermissions(...$permissions): bool |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$permissions = collect($permissions)->flatten(); |
240
|
|
|
|
241
|
|
|
foreach ($permissions as $permission) { |
242
|
|
|
if (! $this->hasPermissionTo($permission)) { |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return true; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Determine if the model has, via roles, the given permission. |
252
|
|
|
* |
253
|
|
|
* @param \Spatie\Permission\Contracts\Permission $permission |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
protected function hasPermissionViaRole(Permission $permission): bool |
258
|
|
|
{ |
259
|
|
|
return $this->hasRole($permission->roles); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Determine if the model has the given permission. |
264
|
|
|
* |
265
|
|
|
* @param string|int|\Spatie\Permission\Contracts\Permission $permission |
266
|
|
|
* |
267
|
|
|
* @return bool |
268
|
|
|
* @throws PermissionDoesNotExist |
269
|
|
|
*/ |
270
|
|
|
public function hasDirectPermission($permission): bool |
271
|
|
|
{ |
272
|
|
|
$permissionClass = $this->getPermissionClass(); |
273
|
|
|
|
274
|
|
|
if (is_string($permission)) { |
275
|
|
|
$permission = $permissionClass->findByName($permission, $this->getDefaultGuardName()); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
if (is_int($permission)) { |
279
|
|
|
$permission = $permissionClass->findById($permission, $this->getDefaultGuardName()); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (! $permission instanceof Permission) { |
283
|
|
|
throw new PermissionDoesNotExist; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $this->permissions->contains('id', $permission->id); |
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Return all the permissions the model has via roles. |
291
|
|
|
*/ |
292
|
|
|
public function getPermissionsViaRoles(): Collection |
293
|
|
|
{ |
294
|
|
|
return $this->loadMissing('roles', 'roles.permissions') |
|
|
|
|
295
|
|
|
->roles->flatMap(function ($role) { |
296
|
|
|
return $role->permissions; |
297
|
|
|
})->sort()->values(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Return all the permissions the model has, both directly and via roles. |
302
|
|
|
*/ |
303
|
|
|
public function getAllPermissions(): Collection |
304
|
|
|
{ |
305
|
|
|
/** @var Collection $permissions */ |
306
|
|
|
$permissions = $this->permissions; |
307
|
|
|
|
308
|
|
|
if ($this->roles) { |
|
|
|
|
309
|
|
|
$permissions = $permissions->merge($this->getPermissionsViaRoles()); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $permissions->sort()->values(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Grant the given permission(s) to a role. |
317
|
|
|
* |
318
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
319
|
|
|
* |
320
|
|
|
* @return $this |
321
|
|
|
*/ |
322
|
|
View Code Duplication |
public function givePermissionTo(...$permissions) |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
$permissions = collect($permissions) |
325
|
|
|
->flatten() |
326
|
|
|
->map(function ($permission) { |
327
|
|
|
if (empty($permission)) { |
328
|
|
|
return false; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return $this->getStoredPermission($permission); |
332
|
|
|
}) |
333
|
|
|
->filter(function ($permission) { |
334
|
|
|
return $permission instanceof Permission; |
335
|
|
|
}) |
336
|
|
|
->each(function ($permission) { |
337
|
|
|
$this->ensureModelSharesGuard($permission); |
338
|
|
|
}) |
339
|
|
|
->map->id |
340
|
|
|
->all(); |
341
|
|
|
|
342
|
|
|
$model = $this->getModel(); |
|
|
|
|
343
|
|
|
|
344
|
|
|
if ($model->exists) { |
345
|
|
|
$this->permissions()->sync($permissions, false); |
346
|
|
|
$model->load('permissions'); |
347
|
|
|
} else { |
348
|
|
|
$class = \get_class($model); |
349
|
|
|
|
350
|
|
|
$class::saved( |
351
|
|
|
function ($object) use ($permissions, $model) { |
352
|
|
|
static $modelLastFiredOn; |
353
|
|
|
if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) { |
354
|
|
|
return; |
355
|
|
|
} |
356
|
|
|
$object->permissions()->sync($permissions, false); |
357
|
|
|
$object->load('permissions'); |
358
|
|
|
$modelLastFiredOn = $object; |
359
|
|
|
} |
360
|
|
|
); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$this->forgetCachedPermissions(); |
364
|
|
|
|
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Remove all current permissions and set the given ones. |
370
|
|
|
* |
371
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
372
|
|
|
* |
373
|
|
|
* @return $this |
374
|
|
|
*/ |
375
|
|
|
public function syncPermissions(...$permissions) |
376
|
|
|
{ |
377
|
|
|
$this->permissions()->detach(); |
378
|
|
|
|
379
|
|
|
return $this->givePermissionTo($permissions); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Revoke the given permission. |
384
|
|
|
* |
385
|
|
|
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
386
|
|
|
* |
387
|
|
|
* @return $this |
388
|
|
|
*/ |
389
|
|
|
public function revokePermissionTo($permission) |
390
|
|
|
{ |
391
|
|
|
$this->permissions()->detach($this->getStoredPermission($permission)); |
392
|
|
|
|
393
|
|
|
$this->forgetCachedPermissions(); |
394
|
|
|
|
395
|
|
|
$this->load('permissions'); |
|
|
|
|
396
|
|
|
|
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
public function getPermissionNames(): Collection |
401
|
|
|
{ |
402
|
|
|
return $this->permissions->pluck('name'); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
407
|
|
|
* |
408
|
|
|
* @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
409
|
|
|
*/ |
410
|
|
|
protected function getStoredPermission($permissions) |
411
|
|
|
{ |
412
|
|
|
$permissionClass = $this->getPermissionClass(); |
413
|
|
|
|
414
|
|
|
if (is_numeric($permissions)) { |
415
|
|
|
return $permissionClass->findById($permissions, $this->getDefaultGuardName()); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
if (is_string($permissions)) { |
419
|
|
|
return $permissionClass->findByName($permissions, $this->getDefaultGuardName()); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
if (is_array($permissions)) { |
423
|
|
|
return $permissionClass |
424
|
|
|
->whereIn('name', $permissions) |
425
|
|
|
->whereIn('guard_name', $this->getGuardNames()) |
426
|
|
|
->get(); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
return $permissions; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
434
|
|
|
* |
435
|
|
|
* @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
436
|
|
|
*/ |
437
|
|
|
protected function ensureModelSharesGuard($roleOrPermission) |
438
|
|
|
{ |
439
|
|
|
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) { |
440
|
|
|
throw GuardDoesNotMatch::create($roleOrPermission->guard_name, $this->getGuardNames()); |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
protected function getGuardNames(): Collection |
445
|
|
|
{ |
446
|
|
|
return Guard::getNames($this); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
protected function getDefaultGuardName(): string |
450
|
|
|
{ |
451
|
|
|
return Guard::getDefaultName($this); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Forget the cached permissions. |
456
|
|
|
*/ |
457
|
|
|
public function forgetCachedPermissions() |
458
|
|
|
{ |
459
|
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions(); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Check if the model has All of the requested Direct permissions. |
464
|
|
|
* @param array ...$permissions |
465
|
|
|
* @return bool |
466
|
|
|
*/ |
467
|
|
View Code Duplication |
public function hasAllDirectPermissions(...$permissions): bool |
|
|
|
|
468
|
|
|
{ |
469
|
|
|
$permissions = collect($permissions)->flatten(); |
470
|
|
|
|
471
|
|
|
foreach ($permissions as $permission) { |
472
|
|
|
if (! $this->hasDirectPermission($permission)) { |
473
|
|
|
return false; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return true; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Check if the model has Any of the requested Direct permissions. |
482
|
|
|
* @param array ...$permissions |
483
|
|
|
* @return bool |
484
|
|
|
*/ |
485
|
|
View Code Duplication |
public function hasAnyDirectPermission(...$permissions): bool |
|
|
|
|
486
|
|
|
{ |
487
|
|
|
$permissions = collect($permissions)->flatten(); |
488
|
|
|
|
489
|
|
|
foreach ($permissions as $permission) { |
490
|
|
|
if ($this->hasDirectPermission($permission)) { |
491
|
|
|
return true; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
return false; |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
|
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.