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