1 | <?php |
||
8 | trait HasRole |
||
9 | { |
||
10 | /** |
||
11 | * Check if user have access using any of the acl. |
||
12 | * |
||
13 | * @param array $acl |
||
14 | * @return boolean |
||
15 | */ |
||
16 | public function canAccess(array $acl) |
||
20 | |||
21 | /** |
||
22 | * Check if user has at least one of the given permissions |
||
23 | * |
||
24 | * @param array $permissions |
||
25 | * @return bool |
||
26 | */ |
||
27 | public function canAtLeast(array $permissions) |
||
47 | |||
48 | /** |
||
49 | * Check if user has the given role. |
||
50 | * |
||
51 | * @param string|array $role |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function hasRole($role) |
||
71 | |||
72 | /** |
||
73 | * Get all user roles. |
||
74 | * |
||
75 | * @return array|null |
||
76 | */ |
||
77 | public function getRoleSlugs() |
||
85 | |||
86 | /** |
||
87 | * Attach a role to user using slug. |
||
88 | * |
||
89 | * @param $slug |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function attachRoleBySlug($slug) |
||
98 | |||
99 | /** |
||
100 | * Attach a role to user |
||
101 | * |
||
102 | * @param Role $role |
||
103 | * @return boolean |
||
104 | */ |
||
105 | public function attachRole(Role $role) |
||
109 | |||
110 | /** |
||
111 | * Assigns the given role to the user. |
||
112 | * |
||
113 | * @param int $roleId |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function assignRole($roleId = null) |
||
126 | |||
127 | /** |
||
128 | * Model can have many roles. |
||
129 | * |
||
130 | * @return \Illuminate\Database\Eloquent\Model |
||
131 | */ |
||
132 | public function roles() |
||
136 | |||
137 | /** |
||
138 | * Query scope for user having the given roles. |
||
139 | * |
||
140 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
141 | * @param array $roles |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function scopeHavingRoles($query, array $roles) |
||
153 | |||
154 | /** |
||
155 | * Revokes the given role from the user using slug. |
||
156 | * |
||
157 | * @param string $slug |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function revokeRoleBySlug($slug) |
||
166 | |||
167 | /** |
||
168 | * Revokes the given role from the user. |
||
169 | * |
||
170 | * @param mixed $role |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function revokeRole($role = "") |
||
177 | |||
178 | /** |
||
179 | * Syncs the given role(s) with the user. |
||
180 | * |
||
181 | * @param array $roles |
||
182 | * @return bool |
||
183 | */ |
||
184 | public function syncRoles(array $roles) |
||
188 | |||
189 | /** |
||
190 | * Revokes all roles from the user. |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function revokeAllRoles() |
||
198 | |||
199 | /** |
||
200 | * Get all user role permissions. |
||
201 | * |
||
202 | * @return array|null |
||
203 | */ |
||
204 | public function getPermissions() |
||
214 | |||
215 | /** |
||
216 | * Magic __call method to handle dynamic methods. |
||
217 | * |
||
218 | * @param string $method |
||
219 | * @param array $arguments |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function __call($method, $arguments = []) |
||
240 | |||
241 | /** |
||
242 | * Checks if the user has the given role. |
||
243 | * |
||
244 | * @param string $slug |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function isRole($slug) |
||
259 | |||
260 | /** |
||
261 | * Check if the given entity/model is owned by the user. |
||
262 | * |
||
263 | * @param \Illuminate\Database\Eloquent\Model $entity |
||
264 | * @param string $relation |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function owns($entity, $relation = 'user_id') |
||
271 | } |
||
272 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: