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
|
|
|
public static function bootHasPermissions() |
17
|
|
|
{ |
18
|
|
|
static::deleting(function ($model) { |
19
|
|
|
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$model->permissions()->detach(); |
24
|
|
|
}); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A model may have multiple direct permissions. |
29
|
|
|
*/ |
30
|
|
|
public function permissions(): MorphToMany |
31
|
|
|
{ |
32
|
|
|
return $this->morphToMany( |
|
|
|
|
33
|
|
|
config('permission.models.permission'), |
34
|
|
|
'model', |
35
|
|
|
config('permission.table_names.model_has_permissions'), |
36
|
|
|
'model_id', |
37
|
|
|
'permission_id' |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Scope the model query to certain permissions only. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
45
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
48
|
|
|
*/ |
49
|
|
|
public function scopePermission(Builder $query, $permissions): Builder |
50
|
|
|
{ |
51
|
|
|
$permissions = $this->convertToPermissionModels($permissions); |
52
|
|
|
|
53
|
|
|
$rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) { |
54
|
|
|
return array_merge($result, $permission->roles->all()); |
55
|
|
|
}, [])); |
56
|
|
|
|
57
|
|
|
return $query->where(function ($query) use ($permissions, $rolesWithPermissions) { |
58
|
|
|
$query->whereHas('permissions', function ($query) use ($permissions) { |
59
|
|
|
$query->where(function ($query) use ($permissions) { |
60
|
|
|
foreach ($permissions as $permission) { |
61
|
|
|
$query->orWhere(config('permission.table_names.permissions').'.id', $permission->id); |
62
|
|
|
} |
63
|
|
|
}); |
64
|
|
|
}); |
65
|
|
|
if (count($rolesWithPermissions) > 0) { |
66
|
|
|
$query->orWhereHas('roles', function ($query) use ($rolesWithPermissions) { |
67
|
|
|
$query->where(function ($query) use ($rolesWithPermissions) { |
68
|
|
|
foreach ($rolesWithPermissions as $role) { |
69
|
|
|
$query->orWhere(config('permission.table_names.roles').'.id', $role->id); |
70
|
|
|
} |
71
|
|
|
}); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function convertToPermissionModels($permissions): array |
83
|
|
|
{ |
84
|
|
|
if ($permissions instanceof Collection) { |
85
|
|
|
$permissions = $permissions->all(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$permissions = array_wrap($permissions); |
89
|
|
|
|
90
|
|
|
return array_map(function ($permission) { |
91
|
|
|
if ($permission instanceof Permission) { |
92
|
|
|
return $permission; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return app(Permission::class)->findByName($permission, $this->getDefaultGuardName()); |
96
|
|
|
}, $permissions); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Determine if the model may perform the given permission. |
101
|
|
|
* |
102
|
|
|
* @param string|int|\Spatie\Permission\Contracts\Permission $permission |
103
|
|
|
* @param string|null $guardName |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function hasPermissionTo($permission, $guardName = null): bool |
108
|
|
|
{ |
109
|
|
View Code Duplication |
if (is_string($permission)) { |
|
|
|
|
110
|
|
|
$permission = app(Permission::class)->findByName( |
111
|
|
|
$permission, |
112
|
|
|
$guardName ?? $this->getDefaultGuardName() |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
if (is_int($permission)) { |
|
|
|
|
117
|
|
|
$permission = app(Permission::class)->findById( |
118
|
|
|
$permission, |
119
|
|
|
$guardName ?? $this->getDefaultGuardName() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (! $permission instanceof Permission) { |
124
|
|
|
throw new PermissionDoesNotExist; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Determine if the model has any of the given permissions. |
132
|
|
|
* |
133
|
|
|
* @param array ...$permissions |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function hasAnyPermission(...$permissions): bool |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
if (is_array($permissions[0])) { |
140
|
|
|
$permissions = $permissions[0]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
foreach ($permissions as $permission) { |
144
|
|
|
if ($this->hasPermissionTo($permission)) { |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Determine if the model has all of the given permissions. |
154
|
|
|
* |
155
|
|
|
* @param array ...$permissions |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function hasAllPermissions(...$permissions): bool |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
if (is_array($permissions[0])) { |
162
|
|
|
$permissions = $permissions[0]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
foreach ($permissions as $permission) { |
166
|
|
|
if (! $this->hasPermissionTo($permission)) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Determine if the model has, via roles, the given permission. |
176
|
|
|
* |
177
|
|
|
* @param \Spatie\Permission\Contracts\Permission $permission |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
protected function hasPermissionViaRole(Permission $permission): bool |
182
|
|
|
{ |
183
|
|
|
return $this->hasRole($permission->roles); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Determine if the model has the given permission. |
188
|
|
|
* |
189
|
|
|
* @param string|int|\Spatie\Permission\Contracts\Permission $permission |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function hasDirectPermission($permission): bool |
194
|
|
|
{ |
195
|
|
View Code Duplication |
if (is_string($permission)) { |
|
|
|
|
196
|
|
|
$permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName()); |
197
|
|
|
if (! $permission) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
View Code Duplication |
if (is_int($permission)) { |
|
|
|
|
203
|
|
|
$permission = app(Permission::class)->findById($permission, $this->getDefaultGuardName()); |
204
|
|
|
if (! $permission) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (! $permission instanceof Permission) { |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this->permissions->contains('id', $permission->id); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return all the permissions the model has via roles. |
218
|
|
|
*/ |
219
|
|
|
public function getPermissionsViaRoles(): Collection |
220
|
|
|
{ |
221
|
|
|
return $this->load('roles', 'roles.permissions') |
|
|
|
|
222
|
|
|
->roles->flatMap(function ($role) { |
223
|
|
|
return $role->permissions; |
224
|
|
|
})->sort()->values(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Return all the permissions the model has, both directly and via roles. |
229
|
|
|
*/ |
230
|
|
|
public function getAllPermissions(): Collection |
231
|
|
|
{ |
232
|
|
|
return $this->permissions |
233
|
|
|
->merge($this->getPermissionsViaRoles()) |
234
|
|
|
->sort() |
235
|
|
|
->values(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Grant the given permission(s) to a role. |
240
|
|
|
* |
241
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
242
|
|
|
* |
243
|
|
|
* @return $this |
244
|
|
|
*/ |
245
|
|
View Code Duplication |
public function givePermissionTo(...$permissions) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$permissions = collect($permissions) |
248
|
|
|
->flatten() |
249
|
|
|
->map(function ($permission) { |
250
|
|
|
return $this->getStoredPermission($permission); |
251
|
|
|
}) |
252
|
|
|
->filter(function ($permission) { |
253
|
|
|
return $permission instanceof Permission; |
254
|
|
|
}) |
255
|
|
|
->each(function ($permission) { |
256
|
|
|
$this->ensureModelSharesGuard($permission); |
257
|
|
|
}) |
258
|
|
|
->all(); |
259
|
|
|
|
260
|
|
|
$this->permissions()->saveMany($permissions); |
261
|
|
|
|
262
|
|
|
$this->forgetCachedPermissions(); |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Remove all current permissions and set the given ones. |
269
|
|
|
* |
270
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
271
|
|
|
* |
272
|
|
|
* @return $this |
273
|
|
|
*/ |
274
|
|
|
public function syncPermissions(...$permissions) |
275
|
|
|
{ |
276
|
|
|
$this->permissions()->detach(); |
277
|
|
|
|
278
|
|
|
return $this->givePermissionTo($permissions); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Revoke the given permission. |
283
|
|
|
* |
284
|
|
|
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
285
|
|
|
* |
286
|
|
|
* @return $this |
287
|
|
|
*/ |
288
|
|
|
public function revokePermissionTo($permission) |
289
|
|
|
{ |
290
|
|
|
$this->permissions()->detach($this->getStoredPermission($permission)); |
291
|
|
|
|
292
|
|
|
$this->forgetCachedPermissions(); |
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
299
|
|
|
* |
300
|
|
|
* @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
301
|
|
|
*/ |
302
|
|
|
protected function getStoredPermission($permissions) |
303
|
|
|
{ |
304
|
|
|
if (is_numeric($permissions)) { |
305
|
|
|
return app(Permission::class)->findById($permissions, $this->getDefaultGuardName()); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if (is_string($permissions)) { |
309
|
|
|
return app(Permission::class)->findByName($permissions, $this->getDefaultGuardName()); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if (is_array($permissions)) { |
313
|
|
|
return app(Permission::class) |
314
|
|
|
->whereIn('name', $permissions) |
315
|
|
|
->whereIn('guard_name', $this->getGuardNames()) |
316
|
|
|
->get(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return $permissions; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
324
|
|
|
* |
325
|
|
|
* @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
326
|
|
|
*/ |
327
|
|
|
protected function ensureModelSharesGuard($roleOrPermission) |
328
|
|
|
{ |
329
|
|
|
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) { |
330
|
|
|
throw GuardDoesNotMatch::create($roleOrPermission->guard_name, $this->getGuardNames()); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
protected function getGuardNames(): Collection |
335
|
|
|
{ |
336
|
|
|
return Guard::getNames($this); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
protected function getDefaultGuardName(): string |
340
|
|
|
{ |
341
|
|
|
return Guard::getDefaultName($this); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Forget the cached permissions. |
346
|
|
|
*/ |
347
|
|
|
public function forgetCachedPermissions() |
348
|
|
|
{ |
349
|
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions(); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
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.