1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Maklad\Permission\Traits; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Jenssegers\Mongodb\Eloquent\Model; |
8
|
|
|
use Jenssegers\Mongodb\Relations\BelongsToMany; |
9
|
|
|
use Maklad\Permission\Contracts\PermissionInterface as Permission; |
10
|
|
|
use Maklad\Permission\Exceptions\GuardDoesNotMatch; |
11
|
|
|
use Maklad\Permission\Helpers; |
12
|
|
|
use Maklad\Permission\PermissionRegistrar; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait HasPermissions |
16
|
|
|
* @package Maklad\Permission\Traits |
17
|
|
|
*/ |
18
|
|
|
trait HasPermissions |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* A role may be given various permissions. |
22
|
|
|
* @return BelongsToMany |
23
|
|
|
*/ |
24
|
29 |
|
public function permissions(): BelongsToMany |
25
|
|
|
{ |
26
|
29 |
|
return $this->belongsToMany( |
|
|
|
|
27
|
29 |
|
config('permission.models.permission'), |
28
|
29 |
|
config('permission.collection_names.role_has_permissions') |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A role belongs to some users of the model associated with its guard. |
34
|
|
|
*/ |
35
|
2 |
|
public function users(): BelongsToMany |
36
|
|
|
{ |
37
|
2 |
|
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name'])); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Grant the given permission(s) to a role. |
42
|
|
|
* |
43
|
|
|
* @param string|array|Permission|\Illuminate\Support\Collection $permissions |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
* @throws GuardDoesNotMatch |
47
|
|
|
*/ |
48
|
46 |
View Code Duplication |
public function givePermissionTo(...$permissions) |
|
|
|
|
49
|
|
|
{ |
50
|
46 |
|
$permissions = collect($permissions) |
51
|
46 |
|
->flatten() |
52
|
46 |
|
->map(function ($permission) { |
53
|
44 |
|
return $this->getStoredPermission($permission); |
54
|
46 |
|
}) |
55
|
44 |
|
->each(function ($permission) { |
56
|
42 |
|
$this->ensureModelSharesGuard($permission); |
57
|
44 |
|
}) |
58
|
41 |
|
->all(); |
59
|
|
|
|
60
|
41 |
|
$this->permissions()->saveMany($permissions); |
61
|
|
|
|
62
|
41 |
|
$this->forgetCachedPermissions(); |
63
|
|
|
|
64
|
41 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Remove all current permissions and set the given ones. |
69
|
|
|
* |
70
|
|
|
* @param string|array|Permission|\Illuminate\Support\Collection $permissions |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
* @throws GuardDoesNotMatch |
74
|
|
|
*/ |
75
|
4 |
|
public function syncPermissions(...$permissions) |
76
|
|
|
{ |
77
|
4 |
|
$this->permissions()->sync([]); |
78
|
|
|
|
79
|
4 |
|
return $this->givePermissionTo($permissions); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Revoke the given permission. |
84
|
|
|
* |
85
|
|
|
* @param string|array|Permission|\Illuminate\Support\Collection $permissions |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
* @throws \Maklad\Permission\Exceptions\GuardDoesNotMatch |
89
|
|
|
*/ |
90
|
6 |
View Code Duplication |
public function revokePermissionTo(...$permissions) |
|
|
|
|
91
|
|
|
{ |
92
|
6 |
|
collect($permissions) |
93
|
6 |
|
->flatten() |
94
|
6 |
|
->map(function ($permission) { |
95
|
6 |
|
$permission = $this->getStoredPermission($permission); |
96
|
6 |
|
$this->permissions()->detach($permission); |
|
|
|
|
97
|
|
|
|
98
|
6 |
|
return $permission; |
99
|
6 |
|
}); |
100
|
|
|
|
101
|
6 |
|
$this->forgetCachedPermissions(); |
102
|
|
|
|
103
|
6 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string|Permission $permission |
108
|
|
|
* |
109
|
|
|
* @return Permission |
110
|
|
|
*/ |
111
|
45 |
|
protected function getStoredPermission($permission): Permission |
112
|
|
|
{ |
113
|
45 |
|
if (\is_string($permission)) { |
114
|
32 |
|
return \app(Permission::class)->findByName($permission, $this->getDefaultGuardName()); |
115
|
|
|
} |
116
|
|
|
|
117
|
16 |
|
return $permission; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Model $roleOrPermission |
122
|
|
|
* |
123
|
|
|
* @throws GuardDoesNotMatch |
124
|
|
|
*/ |
125
|
81 |
|
protected function ensureModelSharesGuard(Model $roleOrPermission) |
126
|
|
|
{ |
127
|
81 |
View Code Duplication |
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) { |
|
|
|
|
128
|
5 |
|
$expected = $this->getGuardNames(); |
129
|
5 |
|
$given = $roleOrPermission->guard_name; |
130
|
5 |
|
$helpers = new Helpers(); |
131
|
|
|
|
132
|
5 |
|
throw new GuardDoesNotMatch($helpers->getGuardDoesNotMatchMessage($expected, $given)); |
133
|
|
|
} |
134
|
77 |
|
} |
135
|
|
|
|
136
|
95 |
|
protected function getGuardNames(): Collection |
137
|
|
|
{ |
138
|
95 |
|
if ($this->guard_name) { |
139
|
33 |
|
return collect($this->guard_name); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
73 |
|
return collect(config('auth.guards')) |
143
|
73 |
|
->map(function ($guard) { |
144
|
73 |
|
return config("auth.providers.{$guard['provider']}.model"); |
145
|
73 |
|
}) |
146
|
73 |
|
->filter(function ($model) { |
147
|
73 |
|
return \get_class($this) === $model; |
148
|
73 |
|
}) |
149
|
73 |
|
->keys(); |
150
|
|
|
} |
151
|
|
|
|
152
|
75 |
|
protected function getDefaultGuardName(): string |
153
|
|
|
{ |
154
|
75 |
|
$default = config('auth.defaults.guard'); |
155
|
|
|
|
156
|
75 |
|
return $this->getGuardNames()->first() ?: $default; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Forget the cached permissions. |
161
|
|
|
*/ |
162
|
78 |
|
public function forgetCachedPermissions() |
163
|
|
|
{ |
164
|
78 |
|
app(PermissionRegistrar::class)->forgetCachedPermissions(); |
165
|
78 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Convert to Permission Models |
169
|
|
|
* |
170
|
|
|
* @param string|array|Collection $permissions |
171
|
|
|
* |
172
|
|
|
* @return Collection |
173
|
|
|
*/ |
174
|
7 |
View Code Duplication |
private function convertToPermissionModels($permissions): Collection |
|
|
|
|
175
|
|
|
{ |
176
|
7 |
|
if (\is_array($permissions)) { |
177
|
3 |
|
$permissions = collect($permissions); |
178
|
|
|
} |
179
|
|
|
|
180
|
7 |
|
if (! $permissions instanceof Collection) { |
181
|
5 |
|
$permissions = collect([$permissions]); |
182
|
|
|
} |
183
|
|
|
|
184
|
7 |
|
$permissions = $permissions->map(function ($permission) { |
185
|
7 |
|
return $this->getStoredPermission($permission); |
186
|
7 |
|
}); |
187
|
|
|
|
188
|
6 |
|
return $permissions; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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.