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() |
||
19 | { |
||
20 | return $this->belongsToMany( |
||
|
|||
21 | config('laravel-permission.models.role'), |
||
22 | config('laravel-permission.table_names.user_has_roles') |
||
23 | ); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * A user may have multiple direct permissions. |
||
28 | * |
||
29 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
30 | */ |
||
31 | public function permissions() |
||
32 | { |
||
33 | return $this->belongsToMany( |
||
34 | config('laravel-permission.models.permission'), |
||
35 | config('laravel-permission.table_names.user_has_permissions') |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Assign the given role to the user. |
||
41 | * |
||
42 | * @param string|Role $role |
||
43 | * |
||
44 | * @return Role |
||
45 | */ |
||
46 | public function assignRole($role) |
||
50 | |||
51 | /** |
||
52 | * Revoke the given role from the user. |
||
53 | * |
||
54 | * @param string|Role $role |
||
55 | * |
||
56 | * @return mixed |
||
57 | */ |
||
58 | public function removeRole($role) |
||
62 | |||
63 | /** |
||
64 | * Sync the given roles. |
||
65 | * |
||
66 | * @param array ...$roles |
||
67 | */ |
||
68 | public function syncRoles(...$roles) |
||
80 | |||
81 | /** |
||
82 | * Determine if the user has (one of) the given role(s). |
||
83 | * |
||
84 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | public function hasRole($roles) |
||
110 | |||
111 | /** |
||
112 | * Determine if the user has any of the given role(s). |
||
113 | * |
||
114 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function hasAnyRole($roles) |
||
122 | |||
123 | /** |
||
124 | * Determine if the user has all of the given role(s). |
||
125 | * |
||
126 | * @param string|Role|\Illuminate\Support\Collection $roles |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function hasAllRoles($roles) |
||
146 | |||
147 | /** |
||
148 | * Determine if the user may perform the given permission. |
||
149 | * |
||
150 | * @param string|Permission $permission |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function hasPermissionTo($permission) |
||
162 | |||
163 | /** |
||
164 | * @deprecated deprecated since version 1.0.1, use hasPermissionTo instead |
||
165 | * |
||
166 | * Determine if the user may perform the given permission. |
||
167 | * |
||
168 | * @param Permission $permission |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function hasPermission($permission) |
||
176 | |||
177 | /** |
||
178 | * Determine if the user has, via roles, the given permission. |
||
179 | * |
||
180 | * @param Permission $permission |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | protected function hasPermissionViaRole(Permission $permission) |
||
188 | |||
189 | /** |
||
190 | * Determine if the user has the given permission. |
||
191 | * |
||
192 | * @param string|Permission $permission |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | protected function hasDirectPermission($permission) |
||
208 | |||
209 | /** |
||
210 | * @param $role |
||
211 | * |
||
212 | * @return Role |
||
213 | */ |
||
214 | protected function getStoredRole($role) |
||
222 | } |
||
223 |
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.