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
|
|
|
|
13
|
|
|
trait HasPermissions |
14
|
|
|
{ |
15
|
|
|
public static function bootHasPermissions() |
16
|
|
|
{ |
17
|
|
|
static::deleting(function ($model) { |
18
|
|
|
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$model->permissions()->detach(); |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A model may have multiple direct permissions. |
28
|
|
|
*/ |
29
|
|
|
public function permissions(): MorphToMany |
30
|
|
|
{ |
31
|
|
|
return $this->morphToMany( |
|
|
|
|
32
|
|
|
config('permission.models.permission'), |
33
|
|
|
'model', |
34
|
|
|
config('permission.table_names.model_has_permissions'), |
35
|
|
|
'model_id', |
36
|
|
|
'permission_id' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Scope the model query to certain permissions only. |
42
|
|
|
* |
43
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
44
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
45
|
|
|
* |
46
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
47
|
|
|
*/ |
48
|
|
|
public function scopePermission(Builder $query, $permissions): Builder |
49
|
|
|
{ |
50
|
|
|
$permissions = $this->convertToPermissionModels($permissions); |
51
|
|
|
|
52
|
|
|
$rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) { |
53
|
|
|
return array_merge($result, $permission->roles->all()); |
54
|
|
|
}, [])); |
55
|
|
|
|
56
|
|
|
return $query-> |
57
|
|
|
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->toArray(); |
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|\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
|
|
|
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Determine if the model has any of the given permissions. |
121
|
|
|
* |
122
|
|
|
* @param array ...$permissions |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function hasAnyPermission(...$permissions): bool |
127
|
|
|
{ |
128
|
|
|
if (is_array($permissions[0])) { |
129
|
|
|
$permissions = $permissions[0]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
foreach ($permissions as $permission) { |
133
|
|
|
if ($this->hasPermissionTo($permission)) { |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Determine if the model has, via roles, the given permission. |
143
|
|
|
* |
144
|
|
|
* @param \Spatie\Permission\Contracts\Permission $permission |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
protected function hasPermissionViaRole(Permission $permission): bool |
149
|
|
|
{ |
150
|
|
|
return $this->hasRole($permission->roles); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Determine if the model has the given permission. |
155
|
|
|
* |
156
|
|
|
* @param string|\Spatie\Permission\Contracts\Permission $permission |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function hasDirectPermission($permission): bool |
161
|
|
|
{ |
162
|
|
View Code Duplication |
if (is_string($permission)) { |
|
|
|
|
163
|
|
|
$permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName()); |
164
|
|
|
|
165
|
|
|
if (! $permission) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->permissions->contains('id', $permission->id); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return all the permissions the model has via roles. |
175
|
|
|
*/ |
176
|
|
|
public function getPermissionsViaRoles(): Collection |
177
|
|
|
{ |
178
|
|
|
return $this->load('roles', 'roles.permissions') |
|
|
|
|
179
|
|
|
->roles->flatMap(function ($role) { |
180
|
|
|
return $role->permissions; |
181
|
|
|
})->sort()->values(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return all the permissions the model has, both directly and via roles. |
186
|
|
|
*/ |
187
|
|
|
public function getAllPermissions(): Collection |
188
|
|
|
{ |
189
|
|
|
return $this->permissions |
190
|
|
|
->merge($this->getPermissionsViaRoles()) |
191
|
|
|
->sort() |
192
|
|
|
->values(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Grant the given permission(s) to a role. |
197
|
|
|
* |
198
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
199
|
|
|
* |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
|
View Code Duplication |
public function givePermissionTo(...$permissions) |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$permissions = collect($permissions) |
205
|
|
|
->flatten() |
206
|
|
|
->map(function ($permission) { |
207
|
|
|
return $this->getStoredPermission($permission); |
208
|
|
|
}) |
209
|
|
|
->each(function ($permission) { |
210
|
|
|
$this->ensureModelSharesGuard($permission); |
211
|
|
|
}) |
212
|
|
|
->all(); |
213
|
|
|
|
214
|
|
|
$this->permissions()->saveMany($permissions); |
215
|
|
|
|
216
|
|
|
$this->forgetCachedPermissions(); |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Remove all current permissions and set the given ones. |
223
|
|
|
* |
224
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
225
|
|
|
* |
226
|
|
|
* @return $this |
227
|
|
|
*/ |
228
|
|
|
public function syncPermissions(...$permissions) |
229
|
|
|
{ |
230
|
|
|
$this->permissions()->detach(); |
231
|
|
|
|
232
|
|
|
return $this->givePermissionTo($permissions); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Revoke the given permission. |
237
|
|
|
* |
238
|
|
|
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
239
|
|
|
* |
240
|
|
|
* @return $this |
241
|
|
|
*/ |
242
|
|
|
public function revokePermissionTo($permission) |
243
|
|
|
{ |
244
|
|
|
$this->permissions()->detach($this->getStoredPermission($permission)); |
245
|
|
|
|
246
|
|
|
$this->forgetCachedPermissions(); |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
253
|
|
|
* |
254
|
|
|
* @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
255
|
|
|
*/ |
256
|
|
|
protected function getStoredPermission($permissions) |
257
|
|
|
{ |
258
|
|
|
if (is_numeric($permissions)) { |
259
|
|
|
return app(Permission::class)->findById($permissions, $this->getDefaultGuardName()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
if (is_string($permissions)) { |
263
|
|
|
return app(Permission::class)->findByName($permissions, $this->getDefaultGuardName()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if (is_array($permissions)) { |
267
|
|
|
return app(Permission::class) |
268
|
|
|
->whereIn('name', $permissions) |
269
|
|
|
->whereIn('guard_name', $this->getGuardNames()) |
270
|
|
|
->get(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $permissions; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
278
|
|
|
* |
279
|
|
|
* @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
280
|
|
|
*/ |
281
|
|
|
protected function ensureModelSharesGuard($roleOrPermission) |
282
|
|
|
{ |
283
|
|
|
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) { |
284
|
|
|
throw GuardDoesNotMatch::create($roleOrPermission->guard_name, $this->getGuardNames()); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
protected function getGuardNames(): Collection |
289
|
|
|
{ |
290
|
|
|
return Guard::getNames($this); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
protected function getDefaultGuardName(): string |
294
|
|
|
{ |
295
|
|
|
return Guard::getDefaultName($this); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Forget the cached permissions. |
300
|
|
|
*/ |
301
|
|
|
public function forgetCachedPermissions() |
302
|
|
|
{ |
303
|
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions(); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
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.