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