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