1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Maklad\Permission\Traits; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Jenssegers\Mongodb\Eloquent\Builder; |
8
|
|
|
use Jenssegers\Mongodb\Eloquent\Model; |
9
|
|
|
use Jenssegers\Mongodb\Relations\BelongsToMany; |
10
|
|
|
use Maklad\Permission\Contracts\PermissionInterface as Permission; |
11
|
|
|
use Maklad\Permission\Contracts\RoleInterface as Role; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait HasRoles |
15
|
|
|
* @package Maklad\Permission\Traits |
16
|
|
|
*/ |
17
|
|
|
trait HasRoles |
18
|
|
|
{ |
19
|
|
|
use HasPermissions; |
20
|
|
|
|
21
|
|
|
public static function bootHasRoles() |
22
|
|
|
{ |
23
|
110 |
|
static::deleting(function (Model $model) { |
24
|
2 |
|
foreach ($model->roles as $role) { |
25
|
1 |
|
$role->users()->detach($model); |
26
|
|
|
} |
27
|
2 |
|
foreach ($model->permissions as $permission) { |
28
|
1 |
|
$permission->users()->detach($model); |
29
|
|
|
} |
30
|
110 |
|
}); |
31
|
110 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A model may have multiple roles. |
35
|
|
|
*/ |
36
|
63 |
|
public function roles(): BelongsToMany |
37
|
|
|
{ |
38
|
63 |
|
return $this->belongsToMany(\config('permission.models.role'))->withTimestamps(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* A model may have multiple direct permissions. |
43
|
|
|
*/ |
44
|
30 |
|
public function permissions(): BelongsToMany |
45
|
|
|
{ |
46
|
30 |
|
return $this->belongsToMany(\config('permission.models.permission'))->withTimestamps(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Scope the model query to certain roles only. |
51
|
|
|
* |
52
|
|
|
* @param Builder $query |
53
|
|
|
* @param string|array|Role|Collection $roles |
54
|
|
|
* |
55
|
|
|
* @return Builder |
56
|
|
|
*/ |
57
|
4 |
|
public function scopeRole(Builder $query, $roles): Builder |
58
|
|
|
{ |
59
|
4 |
|
if (\is_array($roles)) { |
60
|
2 |
|
$roles = \collect($roles); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
if (! $roles instanceof Collection) { |
64
|
2 |
|
$roles = \collect([$roles]); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$roles = $roles->map(function ($role) { |
68
|
4 |
|
return $this->getStoredRole($role); |
69
|
4 |
|
}); |
70
|
|
|
|
71
|
4 |
|
return $query->whereIn('role_ids', $roles->pluck('_id')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Scope the model query to certain permissions only. |
76
|
|
|
* |
77
|
|
|
* @param Builder $query |
78
|
|
|
* @param string|array|Permission|Collection $permissions |
79
|
|
|
* |
80
|
|
|
* @return Builder |
81
|
|
|
*/ |
82
|
7 |
|
public function scopePermission(Builder $query, $permissions): Builder |
83
|
|
|
{ |
84
|
7 |
|
if (\is_array($permissions)) { |
85
|
3 |
|
$permissions = \collect($permissions); |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
if (! $permissions instanceof Collection) { |
89
|
5 |
|
$permissions = \collect([$permissions]); |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
$roles = \collect([]); |
93
|
7 |
|
$permissions = $permissions->map(function ($permission) use (&$roles) { |
94
|
7 |
|
$permission = $this->getStoredPermission($permission); |
95
|
6 |
|
$roles = $roles->merge($permission->roles); |
|
|
|
|
96
|
6 |
|
return $permission; |
97
|
7 |
|
}); |
98
|
6 |
|
$roles = $roles->unique(); |
99
|
|
|
|
100
|
6 |
|
return $query->orWhereIn('permission_ids', $permissions->pluck('_id')) |
101
|
6 |
|
->orWhereIn('role_ids', $roles->pluck('_id')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Assign the given role to the model. |
106
|
|
|
* |
107
|
|
|
* @param array|string|Role ...$roles |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
50 |
View Code Duplication |
public function assignRole(...$roles) |
|
|
|
|
112
|
|
|
{ |
113
|
50 |
|
$roles = \collect($roles) |
114
|
50 |
|
->flatten() |
115
|
50 |
|
->map(function ($role) { |
116
|
50 |
|
return $this->getStoredRole($role); |
117
|
50 |
|
}) |
118
|
48 |
|
->each(function ($role) { |
119
|
48 |
|
$this->ensureModelSharesGuard($role); |
120
|
48 |
|
}) |
121
|
46 |
|
->all(); |
122
|
|
|
|
123
|
46 |
|
$this->roles()->saveMany($roles); |
|
|
|
|
124
|
|
|
|
125
|
46 |
|
$this->forgetCachedPermissions(); |
126
|
|
|
|
127
|
46 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Revoke the given role from the model. |
132
|
|
|
* |
133
|
|
|
* @param string|Role $role |
134
|
|
|
*/ |
135
|
1 |
|
public function removeRole($role) |
136
|
|
|
{ |
137
|
1 |
|
$this->roles()->detach($this->getStoredRole($role)); |
|
|
|
|
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Remove all current roles and set the given ones. |
142
|
|
|
* |
143
|
|
|
* @param array ...$roles |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
5 |
|
public function syncRoles(...$roles) |
148
|
|
|
{ |
149
|
5 |
|
$this->roles()->detach(); |
150
|
|
|
|
151
|
5 |
|
return $this->assignRole($roles); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Determine if the model has (one of) the given role(s). |
156
|
|
|
* |
157
|
|
|
* @param string|array|Role|\Illuminate\Support\Collection $roles |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
39 |
|
public function hasRole($roles): bool |
162
|
|
|
{ |
163
|
39 |
View Code Duplication |
if (\is_string($roles) && false !== \strpos($roles, '|')) { |
|
|
|
|
164
|
3 |
|
$roles = \explode('|', $roles); |
165
|
|
|
} |
166
|
|
|
|
167
|
39 |
|
if (\is_string($roles)) { |
168
|
23 |
|
return $this->roles->contains('name', $roles); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
28 |
|
if ($roles instanceof Role) { |
172
|
3 |
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
26 |
|
if (\is_array($roles)) { |
176
|
11 |
|
foreach ($roles as $role) { |
177
|
11 |
|
if ($this->hasRole($role)) { |
178
|
7 |
|
return true; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
5 |
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
16 |
|
return $roles->intersect($this->roles)->isNotEmpty(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Determine if the model has any of the given role(s). |
190
|
|
|
* |
191
|
|
|
* @param string|array|Role|\Illuminate\Support\Collection $roles |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
12 |
|
public function hasAnyRole($roles): bool |
196
|
|
|
{ |
197
|
12 |
|
return $this->hasRole($roles); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Determine if the model has all of the given role(s). |
202
|
|
|
* |
203
|
|
|
* @param string|Role|\Illuminate\Support\Collection $roles |
204
|
|
|
* |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
7 |
|
public function hasAllRoles($roles): bool |
208
|
|
|
{ |
209
|
7 |
View Code Duplication |
if (\is_string($roles) && false !== strpos($roles, '|')) { |
|
|
|
|
210
|
4 |
|
$roles = \explode('|', $roles); |
211
|
|
|
} |
212
|
|
|
|
213
|
7 |
|
if (\is_string($roles)) { |
214
|
2 |
|
return $this->roles->contains('name', $roles); |
215
|
|
|
} |
216
|
|
|
|
217
|
6 |
|
if ($roles instanceof Role) { |
218
|
1 |
|
return $this->roles->contains('id', $roles->id); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
6 |
|
$roles = \collect()->make($roles)->map(function ($role) { |
222
|
6 |
|
return $role instanceof Role ? $role->name : $role; |
|
|
|
|
223
|
6 |
|
}); |
224
|
|
|
|
225
|
6 |
|
return $roles->intersect($this->roles->pluck('name')) == $roles; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Determine if the model may perform the given permission. |
230
|
|
|
* |
231
|
|
|
* @param string|Permission $permission |
232
|
|
|
* @param string|null $guardName |
233
|
|
|
* |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
21 |
|
public function hasPermissionTo($permission, $guardName = null): bool |
237
|
|
|
{ |
238
|
21 |
|
if (\is_string($permission)) { |
239
|
13 |
|
$permission = \app(Permission::class)->findByName( |
240
|
13 |
|
$permission, |
241
|
13 |
|
$guardName ?? $this->getDefaultGuardName() |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
19 |
|
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Determine if the model has any of the given permissions. |
250
|
|
|
* |
251
|
|
|
* @param array ...$permissions |
252
|
|
|
* |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
7 |
|
public function hasAnyPermission(...$permissions): bool |
256
|
|
|
{ |
257
|
7 |
|
if (\is_array($permissions[0])) { |
258
|
5 |
|
$permissions = $permissions[0]; |
259
|
|
|
} |
260
|
|
|
|
261
|
7 |
|
foreach ($permissions as $permission) { |
262
|
7 |
|
if ($this->hasPermissionTo($permission)) { |
263
|
5 |
|
return true; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
4 |
|
return false; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Determine if the model has, via roles, the given permission. |
272
|
|
|
* |
273
|
|
|
* @param Permission $permission |
274
|
|
|
* |
275
|
|
|
* @return bool |
276
|
|
|
*/ |
277
|
15 |
|
protected function hasPermissionViaRole(Permission $permission): bool |
278
|
|
|
{ |
279
|
15 |
|
return $this->hasRole($permission->roles); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Determine if the model has the given permission. |
284
|
|
|
* |
285
|
|
|
* @param string|Permission $permission |
286
|
|
|
* |
287
|
|
|
* @return bool |
288
|
|
|
*/ |
289
|
20 |
|
public function hasDirectPermission($permission): bool |
290
|
|
|
{ |
291
|
20 |
|
if (\is_string($permission)) { |
292
|
1 |
|
$permission = \app(Permission::class)->findByName($permission, $this->getDefaultGuardName()); |
293
|
|
|
} |
294
|
|
|
|
295
|
20 |
|
return $this->permissions->contains('id', $permission->id); |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Return all permissions the directory coupled to the model. |
300
|
|
|
*/ |
301
|
1 |
|
public function getDirectPermissions(): Collection |
302
|
|
|
{ |
303
|
1 |
|
return $this->permissions; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Return all the permissions the model has via roles. |
308
|
|
|
*/ |
309
|
3 |
|
public function getPermissionsViaRoles(): Collection |
310
|
|
|
{ |
311
|
3 |
|
return $this->load('roles', 'roles.permissions') |
|
|
|
|
312
|
3 |
|
->roles->flatMap(function (Role $role) { |
313
|
2 |
|
return $role->permissions; |
|
|
|
|
314
|
3 |
|
})->sort()->values(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Return all the permissions the model has, both directly and via roles. |
319
|
|
|
*/ |
320
|
2 |
|
public function getAllPermissions(): Collection |
321
|
|
|
{ |
322
|
2 |
|
return $this->permissions |
323
|
2 |
|
->merge($this->getPermissionsViaRoles()) |
324
|
2 |
|
->sort() |
325
|
2 |
|
->values(); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Return Role object |
330
|
|
|
* |
331
|
|
|
* @param String|Role $role role name |
332
|
|
|
* |
333
|
|
|
* @return Role |
334
|
|
|
*/ |
335
|
50 |
|
protected function getStoredRole($role): Role |
336
|
|
|
{ |
337
|
50 |
|
if (\is_string($role)) { |
338
|
43 |
|
return \app(Role::class)->findByName($role, $this->getDefaultGuardName()); |
339
|
|
|
} |
340
|
|
|
|
341
|
12 |
|
return $role; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Return a collection of role names associated with this user. |
346
|
|
|
* |
347
|
|
|
* @return Collection |
348
|
|
|
*/ |
349
|
1 |
|
public function getRoleNames(): Collection |
350
|
|
|
{ |
351
|
1 |
|
return $this->roles->pluck('name'); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Return a collection of permission names associated with this user. |
357
|
|
|
* |
358
|
|
|
* @return Collection |
359
|
|
|
*/ |
360
|
1 |
|
public function getPermissionNames(): Collection |
361
|
|
|
{ |
362
|
1 |
|
return $this->getAllPermissions()->pluck('name'); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
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.