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