1 | <?php |
||
8 | trait HasRoles |
||
9 | { |
||
10 | use HasPermissions; |
||
11 | use RefreshesPermissionCache; |
||
12 | |||
13 | /** |
||
14 | * A user may have multiple roles. |
||
15 | * |
||
16 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
17 | */ |
||
18 | public function roles() |
||
25 | |||
26 | /** |
||
27 | * A user may have multiple direct permissions. |
||
28 | * |
||
29 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
30 | */ |
||
31 | public function permissions() |
||
38 | |||
39 | /** |
||
40 | * Scope the user query to certain roles only |
||
41 | * |
||
42 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
43 | * |
||
44 | * @return bool |
||
45 | */ |
||
46 | public function scopeRole($query, $roles) |
||
78 | |||
79 | /** |
||
80 | * Assign the given role to the user. |
||
81 | * |
||
82 | * @param array|string|\Spatie\Permission\Models\Role ...$roles |
||
83 | * |
||
84 | * @return \Spatie\Permission\Contracts\Role |
||
85 | */ |
||
86 | public function assignRole(...$roles) |
||
101 | |||
102 | /** |
||
103 | * Revoke the given role from the user. |
||
104 | * |
||
105 | * @param string|Role $role |
||
106 | */ |
||
107 | public function removeRole($role) |
||
111 | |||
112 | /** |
||
113 | * Remove all current roles and set the given ones. |
||
114 | * |
||
115 | * @param array ...$roles |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function syncRoles(...$roles) |
||
125 | |||
126 | /** |
||
127 | * Determine if the user has (one of) the given role(s). |
||
128 | * |
||
129 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function hasRole($roles) |
||
155 | |||
156 | /** |
||
157 | * Determine if the user has any of the given role(s). |
||
158 | * |
||
159 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function hasAnyRole($roles) |
||
167 | |||
168 | /** |
||
169 | * Determine if the user has all of the given role(s). |
||
170 | * |
||
171 | * @param string|Role|\Illuminate\Support\Collection $roles |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function hasAllRoles($roles) |
||
191 | |||
192 | /** |
||
193 | * Determine if the user may perform the given permission. |
||
194 | * |
||
195 | * @param string|Permission $permission |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function hasPermissionTo($permission) |
||
207 | |||
208 | /** |
||
209 | * @deprecated deprecated since version 1.0.1, use hasPermissionTo instead |
||
210 | * |
||
211 | * Determine if the user may perform the given permission. |
||
212 | * |
||
213 | * @param Permission $permission |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function hasPermission($permission) |
||
221 | |||
222 | /** |
||
223 | * Determine if the user has, via roles, the given permission. |
||
224 | * |
||
225 | * @param Permission $permission |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | protected function hasPermissionViaRole(Permission $permission) |
||
233 | |||
234 | /** |
||
235 | * Determine if the user has the given permission. |
||
236 | * |
||
237 | * @param string|Permission $permission |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | protected function hasDirectPermission($permission) |
||
253 | |||
254 | /** |
||
255 | * @param $role |
||
256 | * |
||
257 | * @return Role |
||
258 | */ |
||
259 | protected function getStoredRole($role) |
||
267 | } |
||
268 |
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.