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