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 | * Determine if the user has (one of) the given role(s). |
||
65 | * |
||
66 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function hasRole($roles) |
||
92 | |||
93 | /** |
||
94 | * Determine if the user has any of the given role(s). |
||
95 | * |
||
96 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function hasAnyRole($roles) |
||
104 | |||
105 | /** |
||
106 | * Determine if the user has all of the given role(s). |
||
107 | * |
||
108 | * @param string|Role|\Illuminate\Support\Collection $roles |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function hasAllRoles($roles) |
||
128 | |||
129 | /** |
||
130 | * Determine if the user may perform the given permission. |
||
131 | * |
||
132 | * @param string|Permission $permission |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function hasPermissionTo($permission) |
||
144 | |||
145 | /** |
||
146 | * @deprecated deprecated since version 1.0.1, use hasPermissionTo instead |
||
147 | * |
||
148 | * Determine if the user may perform the given permission. |
||
149 | * |
||
150 | * @param Permission $permission |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function hasPermission($permission) |
||
158 | |||
159 | /** |
||
160 | * Determine if the user has, via roles, has the given permission. |
||
161 | * |
||
162 | * @param Permission $permission |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | protected function hasPermissionViaRole(Permission $permission) |
||
170 | |||
171 | /** |
||
172 | * Determine if the user has has the given permission. |
||
173 | * |
||
174 | * @param string|Permission $permission |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | protected function hasDirectPermission($permission) |
||
190 | |||
191 | /** |
||
192 | * @param $role |
||
193 | * |
||
194 | * @return Role |
||
195 | */ |
||
196 | protected function getStoredRole($role) |
||
204 | } |
||
205 |
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.