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