1 | <?php |
||
9 | trait HasRoles |
||
10 | { |
||
11 | use HasPermissions; |
||
12 | use RefreshesPermissionCache; |
||
13 | |||
14 | /** |
||
15 | * A user may have multiple roles. |
||
16 | * |
||
17 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
18 | */ |
||
19 | public function roles() |
||
26 | |||
27 | /** |
||
28 | * A user may have multiple direct permissions. |
||
29 | * |
||
30 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
31 | */ |
||
32 | public function permissions() |
||
39 | |||
40 | /** |
||
41 | * Scope the user query to certain roles only. |
||
42 | * |
||
43 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | public function scopeRole($query, $roles) |
||
73 | |||
74 | /** |
||
75 | * Assign the given role to the user. |
||
76 | * |
||
77 | * @param array|string|\Spatie\Permission\Models\Role ...$roles |
||
78 | * |
||
79 | * @return \Spatie\Permission\Contracts\Role |
||
80 | */ |
||
81 | public function assignRole(...$roles) |
||
96 | |||
97 | /** |
||
98 | * Revoke the given role from the user. |
||
99 | * |
||
100 | * @param string|Role $role |
||
101 | */ |
||
102 | public function removeRole($role) |
||
106 | |||
107 | /** |
||
108 | * Remove all current roles and set the given ones. |
||
109 | * |
||
110 | * @param array ...$roles |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function syncRoles(...$roles) |
||
120 | |||
121 | /** |
||
122 | * Determine if the user has (one of) the given role(s). |
||
123 | * |
||
124 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function hasRole($roles) |
||
150 | |||
151 | /** |
||
152 | * Determine if the user has any of the given role(s). |
||
153 | * |
||
154 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function hasAnyRole($roles) |
||
162 | |||
163 | /** |
||
164 | * Determine if the user has all of the given role(s). |
||
165 | * |
||
166 | * @param string|Role|\Illuminate\Support\Collection $roles |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function hasAllRoles($roles) |
||
186 | |||
187 | /** |
||
188 | * Determine if the user may perform the given permission. |
||
189 | * |
||
190 | * @param string|Permission $permission |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function hasPermissionTo($permission) |
||
202 | |||
203 | /** |
||
204 | * Determine if the user has any of the given permissions. |
||
205 | * |
||
206 | * @param array ...$permissions |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function hasAnyPermission(...$permissions) |
||
211 | { |
||
212 | foreach ($permissions as $permission) { |
||
213 | if ($this->hasPermissionTo($permission)) { |
||
214 | return true; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | return false; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @deprecated deprecated since version 1.0.1, use hasPermissionTo instead |
||
223 | * |
||
224 | * Determine if the user may perform the given permission. |
||
225 | * |
||
226 | * @param Permission $permission |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function hasPermission($permission) |
||
234 | |||
235 | /** |
||
236 | * Determine if the user has, via roles, the given permission. |
||
237 | * |
||
238 | * @param Permission $permission |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | protected function hasPermissionViaRole(Permission $permission) |
||
246 | |||
247 | /** |
||
248 | * Determine if the user has the given permission. |
||
249 | * |
||
250 | * @param string|Permission $permission |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function hasDirectPermission($permission) |
||
266 | |||
267 | /** |
||
268 | * @param $role |
||
269 | * |
||
270 | * @return Role |
||
271 | */ |
||
272 | protected function getStoredRole($role) |
||
280 | |||
281 | /** |
||
282 | * Return all permissions the directory coupled to the user. |
||
283 | * |
||
284 | * @return \Illuminate\Support\Collection |
||
285 | */ |
||
286 | public function getDirectPermissions() |
||
290 | |||
291 | /** |
||
292 | * Return all the permissions the user has via roles. |
||
293 | * |
||
294 | * @return \Illuminate\Support\Collection |
||
295 | */ |
||
296 | public function getPermissionsViaRoles() |
||
303 | |||
304 | /** |
||
305 | * Return all the permissions the user has, both directly and via roles. |
||
306 | * |
||
307 | * @return \Illuminate\Support\Collection |
||
308 | */ |
||
309 | public function getAllPermissions() |
||
313 | } |
||
314 |
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.