1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
55 | class User |
||
|
|||
56 | extends AbstractModel |
||
57 | implements UserContract, AuthenticatableContract, AuthorizableContract, CanResetPasswordContract |
||
58 | { |
||
59 | /* ----------------------------------------------------------------- |
||
60 | | Traits |
||
61 | | ----------------------------------------------------------------- |
||
62 | */ |
||
63 | |||
64 | use Roleable, |
||
65 | Authenticatable, |
||
66 | Authorizable, |
||
67 | CanResetPassword, |
||
68 | Activatable, |
||
69 | SoftDeletes; |
||
70 | |||
71 | /* ----------------------------------------------------------------- |
||
72 | | Properties |
||
73 | | ----------------------------------------------------------------- |
||
74 | |||
75 | */ |
||
76 | /** |
||
77 | * The attributes that are mass assignable. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $fillable = [ |
||
82 | 'username', |
||
83 | 'first_name', |
||
84 | 'last_name', |
||
85 | 'email', |
||
86 | 'password', |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * The attributes excluded from the model's JSON form. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $hidden = [ |
||
95 | 'password', |
||
96 | 'remember_token', |
||
97 | 'confirmation_code', |
||
98 | ]; |
||
99 | |||
100 | /** |
||
101 | * The attributes that should be casted to native types. |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $casts = [ |
||
106 | 'is_admin' => 'boolean', |
||
107 | 'is_active' => 'boolean', |
||
108 | 'is_confirmed' => 'boolean', |
||
109 | ]; |
||
110 | |||
111 | /** |
||
112 | * The attributes that should be mutated to dates. |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $dates = [ |
||
117 | 'confirmed_at', |
||
118 | 'last_activity', |
||
119 | 'deleted_at', |
||
120 | ]; |
||
121 | |||
122 | /* ----------------------------------------------------------------- |
||
123 | | Constructor |
||
124 | | ----------------------------------------------------------------- |
||
125 | */ |
||
126 | |||
127 | /** |
||
128 | * Create a new Eloquent model instance. |
||
129 | * |
||
130 | * @param array $attributes |
||
131 | */ |
||
132 | 231 | public function __construct(array $attributes = []) |
|
138 | |||
139 | /** |
||
140 | * Setup the model. |
||
141 | */ |
||
142 | 231 | protected function setupModel() |
|
151 | |||
152 | /* ----------------------------------------------------------------- |
||
153 | | Relationships |
||
154 | | ----------------------------------------------------------------- |
||
155 | */ |
||
156 | |||
157 | /** |
||
158 | * User belongs to many roles. |
||
159 | * |
||
160 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
161 | */ |
||
162 | 42 | public function roles() |
|
171 | |||
172 | /** |
||
173 | * Get all user permissions. |
||
174 | * |
||
175 | * @return \Illuminate\Support\Collection |
||
176 | */ |
||
177 | 9 | public function getPermissionsAttribute() |
|
186 | |||
187 | /* ----------------------------------------------------------------- |
||
188 | | Scopes |
||
189 | | ----------------------------------------------------------------- |
||
190 | */ |
||
191 | |||
192 | /** |
||
193 | * Scope unconfirmed users by code. |
||
194 | * |
||
195 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
196 | * @param string $code |
||
197 | * |
||
198 | * @return \Illuminate\Database\Eloquent\Builder |
||
199 | */ |
||
200 | 12 | public function scopeUnconfirmed($query, $code) |
|
206 | |||
207 | /** |
||
208 | * Scope last active users. |
||
209 | * |
||
210 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
211 | * @param int|null $minutes |
||
212 | * |
||
213 | * @return \Illuminate\Database\Eloquent\Builder |
||
214 | */ |
||
215 | 3 | public function scopeLastActive($query, $minutes = null) |
|
223 | |||
224 | /* ----------------------------------------------------------------- |
||
225 | | Getters & Setters |
||
226 | | ----------------------------------------------------------------- |
||
227 | */ |
||
228 | |||
229 | /** |
||
230 | * Set the `email` attribute. |
||
231 | * |
||
232 | * @param string $email |
||
233 | */ |
||
234 | 72 | public function setEmailAttribute($email) |
|
238 | |||
239 | /** |
||
240 | * Set the `username` attribute. |
||
241 | * |
||
242 | * @param string $username |
||
243 | */ |
||
244 | 69 | public function setUsernameAttribute($username) |
|
248 | |||
249 | /** |
||
250 | * Set the `first_name` attribute. |
||
251 | * |
||
252 | * @param string $firstName |
||
253 | */ |
||
254 | 72 | public function setFirstNameAttribute($firstName) |
|
258 | |||
259 | /** |
||
260 | * Set the `last_name` attribute. |
||
261 | * |
||
262 | * @param string $lastName |
||
263 | */ |
||
264 | 72 | public function setLastNameAttribute($lastName) |
|
268 | |||
269 | /** |
||
270 | * Get the `full_name` attribute. |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | 6 | public function getFullNameAttribute() |
|
278 | |||
279 | /** |
||
280 | * Set the `password` attribute. |
||
281 | * |
||
282 | * @param string $password |
||
283 | */ |
||
284 | 69 | public function setPasswordAttribute($password) |
|
288 | |||
289 | /* ----------------------------------------------------------------- |
||
290 | | Main Methods |
||
291 | | ----------------------------------------------------------------- |
||
292 | */ |
||
293 | |||
294 | /** |
||
295 | * Attach a role to a user. |
||
296 | * |
||
297 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
298 | * @param bool $reload |
||
299 | */ |
||
300 | 30 | public function attachRole($role, $reload = true) |
|
310 | |||
311 | /** |
||
312 | * Sync the roles by its slugs. |
||
313 | * |
||
314 | * @param array|\Illuminate\Support\Collection $slugs |
||
315 | * @param bool $reload |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | 3 | public function syncRoles($slugs, $reload = true) |
|
332 | |||
333 | /** |
||
334 | * Detach a role from a user. |
||
335 | * |
||
336 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
337 | * @param bool $reload |
||
338 | * |
||
339 | * @return int |
||
340 | */ |
||
341 | 3 | public function detachRole($role, $reload = true) |
|
351 | |||
352 | /** |
||
353 | * Detach all roles from a user. |
||
354 | * |
||
355 | * @param bool $reload |
||
356 | * |
||
357 | * @return int |
||
358 | */ |
||
359 | 3 | public function detachAllRoles($reload = true) |
|
369 | |||
370 | /** |
||
371 | * Confirm the unconfirmed user account by confirmation code. |
||
372 | * |
||
373 | * @param string $code |
||
374 | * |
||
375 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
376 | * |
||
377 | * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException |
||
378 | */ |
||
379 | 12 | public function findUnconfirmed($code) |
|
389 | |||
390 | /** |
||
391 | * Confirm the new user account. |
||
392 | * |
||
393 | * @param \Arcanesoft\Contracts\Auth\Models\User|string $code |
||
394 | * |
||
395 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
396 | */ |
||
397 | 6 | public function confirm($code) |
|
406 | |||
407 | /** |
||
408 | * Update the user's last activity. |
||
409 | * |
||
410 | * @param bool $save |
||
411 | */ |
||
412 | 3 | public function updateLastActivity($save = true) |
|
418 | |||
419 | /* ----------------------------------------------------------------- |
||
420 | | Permission Check Methods |
||
421 | | ----------------------------------------------------------------- |
||
422 | */ |
||
423 | /** |
||
424 | * Check if the user has a permission. |
||
425 | * |
||
426 | * @param string $slug |
||
427 | * |
||
428 | * @return bool |
||
429 | */ |
||
430 | 9 | public function may($slug) |
|
436 | |||
437 | /** |
||
438 | * Check if the user has at least one permission. |
||
439 | * |
||
440 | * @param \Illuminate\Support\Collection|array $permissions |
||
441 | * @param \Illuminate\Support\Collection &$failed |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | 6 | public function mayOne($permissions, &$failed = null) |
|
455 | |||
456 | /** |
||
457 | * Check if the user has all permissions. |
||
458 | * |
||
459 | * @param \Illuminate\Support\Collection|array $permissions |
||
460 | * @param \Illuminate\Support\Collection &$failed |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | 3 | public function mayAll($permissions, &$failed = null) |
|
470 | |||
471 | /* ----------------------------------------------------------------- |
||
472 | | Check Methods |
||
473 | | ----------------------------------------------------------------- |
||
474 | */ |
||
475 | |||
476 | /** |
||
477 | * Check if user is an administrator. |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | 15 | public function isAdmin() |
|
485 | |||
486 | /** |
||
487 | * Check if user is a moderator. |
||
488 | * |
||
489 | * @return bool |
||
490 | */ |
||
491 | 6 | public function isModerator() |
|
496 | |||
497 | /** |
||
498 | * Check if user is a member. |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | 6 | public function isMember() |
|
506 | |||
507 | /** |
||
508 | * Check if user has a confirmed account. |
||
509 | * |
||
510 | * @return bool |
||
511 | */ |
||
512 | 9 | public function isConfirmed() |
|
516 | |||
517 | /** |
||
518 | * Check if user can be impersonated. |
||
519 | * |
||
520 | * @return bool |
||
521 | */ |
||
522 | public function canBeImpersonated() |
||
526 | } |
||
527 |