1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
75 | class User |
||
|
|||
76 | extends AbstractModel |
||
77 | implements UserContract, AuthenticatableContract, AuthorizableContract, CanResetPasswordContract |
||
78 | { |
||
79 | /* ----------------------------------------------------------------- |
||
80 | | Traits |
||
81 | | ----------------------------------------------------------------- |
||
82 | */ |
||
83 | |||
84 | use Roleable, |
||
85 | Authenticatable, |
||
86 | Authorizable, |
||
87 | CanResetPassword, |
||
88 | Activatable, |
||
89 | SoftDeletes; |
||
90 | |||
91 | /* ----------------------------------------------------------------- |
||
92 | | Properties |
||
93 | | ----------------------------------------------------------------- |
||
94 | |||
95 | */ |
||
96 | /** |
||
97 | * The attributes that are mass assignable. |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $fillable = [ |
||
102 | 'username', |
||
103 | 'first_name', |
||
104 | 'last_name', |
||
105 | 'email', |
||
106 | 'password', |
||
107 | 'confirmation_code', |
||
108 | ]; |
||
109 | |||
110 | /** |
||
111 | * The attributes excluded from the model's JSON form. |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | protected $hidden = [ |
||
116 | 'password', |
||
117 | 'remember_token', |
||
118 | 'confirmation_code', |
||
119 | ]; |
||
120 | |||
121 | /** |
||
122 | * The attributes that should be casted to native types. |
||
123 | * |
||
124 | * @var array |
||
125 | */ |
||
126 | protected $casts = [ |
||
127 | 'is_admin' => 'boolean', |
||
128 | 'is_active' => 'boolean', |
||
129 | 'is_confirmed' => 'boolean', |
||
130 | ]; |
||
131 | |||
132 | /** |
||
133 | * The attributes that should be mutated to dates. |
||
134 | * |
||
135 | * @var array |
||
136 | */ |
||
137 | protected $dates = [ |
||
138 | 'confirmed_at', |
||
139 | 'last_activity', |
||
140 | 'deleted_at', |
||
141 | ]; |
||
142 | |||
143 | /** |
||
144 | * The event map for the model. |
||
145 | * |
||
146 | * Allows for object-based events for native Eloquent events. |
||
147 | * |
||
148 | * @var array |
||
149 | */ |
||
150 | protected $events = [ |
||
151 | 'creating' => CreatingUser::class, |
||
152 | 'created' => CreatedUser::class, |
||
153 | 'updating' => UpdatingUser::class, |
||
154 | 'updated' => UpdatedUser::class, |
||
155 | 'saving' => SavingUser::class, |
||
156 | 'saved' => SavedUser::class, |
||
157 | 'deleting' => DeletingUser::class, |
||
158 | 'deleted' => DeletedUser::class, |
||
159 | 'restoring' => RestoringUser::class, |
||
160 | 'restored' => RestoredUser::class, |
||
161 | ]; |
||
162 | |||
163 | /* ----------------------------------------------------------------- |
||
164 | | Constructor |
||
165 | | ----------------------------------------------------------------- |
||
166 | */ |
||
167 | |||
168 | /** |
||
169 | * Create a new Eloquent model instance. |
||
170 | * |
||
171 | * @param array $attributes |
||
172 | */ |
||
173 | 102 | public function __construct(array $attributes = []) |
|
179 | |||
180 | /** |
||
181 | * Setup the model. |
||
182 | */ |
||
183 | 102 | protected function setupModel() |
|
192 | |||
193 | /* ----------------------------------------------------------------- |
||
194 | | Relationships |
||
195 | | ----------------------------------------------------------------- |
||
196 | */ |
||
197 | |||
198 | /** |
||
199 | * User belongs to many roles. |
||
200 | * |
||
201 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
202 | */ |
||
203 | 42 | public function roles() |
|
212 | |||
213 | /** |
||
214 | * Get all user permissions. |
||
215 | * |
||
216 | * @return \Illuminate\Support\Collection |
||
217 | */ |
||
218 | 9 | public function getPermissionsAttribute() |
|
227 | |||
228 | /* ----------------------------------------------------------------- |
||
229 | | Scopes |
||
230 | | ----------------------------------------------------------------- |
||
231 | */ |
||
232 | |||
233 | /** |
||
234 | * Scope unconfirmed users by code. |
||
235 | * |
||
236 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
237 | * @param string $code |
||
238 | * |
||
239 | * @return \Illuminate\Database\Eloquent\Builder |
||
240 | */ |
||
241 | 12 | public function scopeUnconfirmed($query, $code) |
|
247 | |||
248 | /** |
||
249 | * Scope last active users. |
||
250 | * |
||
251 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
252 | * @param int|null $minutes |
||
253 | * |
||
254 | * @return \Illuminate\Database\Eloquent\Builder |
||
255 | */ |
||
256 | 3 | public function scopeLastActive($query, $minutes = null) |
|
264 | |||
265 | /* ----------------------------------------------------------------- |
||
266 | | Getters & Setters |
||
267 | | ----------------------------------------------------------------- |
||
268 | */ |
||
269 | |||
270 | /** |
||
271 | * Set the `email` attribute. |
||
272 | * |
||
273 | * @param string $email |
||
274 | */ |
||
275 | 81 | public function setEmailAttribute($email) |
|
279 | |||
280 | /** |
||
281 | * Set the `username` attribute. |
||
282 | * |
||
283 | * @param string $username |
||
284 | */ |
||
285 | 78 | public function setUsernameAttribute($username) |
|
289 | |||
290 | /** |
||
291 | * Set the `first_name` attribute. |
||
292 | * |
||
293 | * @param string $firstName |
||
294 | */ |
||
295 | 81 | public function setFirstNameAttribute($firstName) |
|
299 | |||
300 | /** |
||
301 | * Set the `last_name` attribute. |
||
302 | * |
||
303 | * @param string $lastName |
||
304 | */ |
||
305 | 81 | public function setLastNameAttribute($lastName) |
|
309 | |||
310 | /** |
||
311 | * Get the `full_name` attribute. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | 6 | public function getFullNameAttribute() |
|
319 | |||
320 | /** |
||
321 | * Set the `password` attribute. |
||
322 | * |
||
323 | * @param string $password |
||
324 | */ |
||
325 | 78 | public function setPasswordAttribute($password) |
|
329 | |||
330 | /* ----------------------------------------------------------------- |
||
331 | | Main Methods |
||
332 | | ----------------------------------------------------------------- |
||
333 | */ |
||
334 | |||
335 | /** |
||
336 | * Activate the model. |
||
337 | * |
||
338 | * @param bool $save |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | 3 | public function activate($save = true) |
|
350 | |||
351 | /** |
||
352 | * Deactivate the model. |
||
353 | * |
||
354 | * @param bool $save |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | 3 | public function deactivate($save = true) |
|
366 | |||
367 | /** |
||
368 | * Attach a role to a user. |
||
369 | * |
||
370 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
371 | * @param bool $reload |
||
372 | */ |
||
373 | 33 | public function attachRole($role, $reload = true) |
|
383 | |||
384 | /** |
||
385 | * Sync the roles by its slugs. |
||
386 | * |
||
387 | * @param array|\Illuminate\Support\Collection $slugs |
||
388 | * @param bool $reload |
||
389 | * |
||
390 | * @return array |
||
391 | */ |
||
392 | 3 | public function syncRoles($slugs, $reload = true) |
|
405 | |||
406 | /** |
||
407 | * Detach a role from a user. |
||
408 | * |
||
409 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
410 | * @param bool $reload |
||
411 | * |
||
412 | * @return int |
||
413 | */ |
||
414 | 3 | public function detachRole($role, $reload = true) |
|
424 | |||
425 | /** |
||
426 | * Detach all roles from a user. |
||
427 | * |
||
428 | * @param bool $reload |
||
429 | * |
||
430 | * @return int |
||
431 | */ |
||
432 | 3 | public function detachAllRoles($reload = true) |
|
442 | |||
443 | /** |
||
444 | * Confirm the unconfirmed user account by confirmation code. |
||
445 | * |
||
446 | * @param string $code |
||
447 | * |
||
448 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
449 | * |
||
450 | * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException |
||
451 | */ |
||
452 | 12 | public function findUnconfirmed($code) |
|
462 | |||
463 | /** |
||
464 | * Confirm the new user account. |
||
465 | * |
||
466 | * @param \Arcanesoft\Contracts\Auth\Models\User|string $code |
||
467 | * |
||
468 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
469 | */ |
||
470 | 6 | public function confirm($code) |
|
479 | |||
480 | /** |
||
481 | * Update the user's last activity. |
||
482 | * |
||
483 | * @param bool $save |
||
484 | */ |
||
485 | 3 | public function updateLastActivity($save = true) |
|
491 | |||
492 | /* ----------------------------------------------------------------- |
||
493 | | Permission Check Methods |
||
494 | | ----------------------------------------------------------------- |
||
495 | */ |
||
496 | /** |
||
497 | * Check if the user has a permission. |
||
498 | * |
||
499 | * @param string $slug |
||
500 | * |
||
501 | * @return bool |
||
502 | */ |
||
503 | 9 | public function may($slug) |
|
509 | |||
510 | /** |
||
511 | * Check if the user has at least one permission. |
||
512 | * |
||
513 | * @param \Illuminate\Support\Collection|array $permissions |
||
514 | * @param \Illuminate\Support\Collection &$failed |
||
515 | * |
||
516 | * @return bool |
||
517 | */ |
||
518 | 6 | public function mayOne($permissions, &$failed = null) |
|
528 | |||
529 | /** |
||
530 | * Check if the user has all permissions. |
||
531 | * |
||
532 | * @param \Illuminate\Support\Collection|array $permissions |
||
533 | * @param \Illuminate\Support\Collection &$failed |
||
534 | * |
||
535 | * @return bool |
||
536 | */ |
||
537 | 3 | public function mayAll($permissions, &$failed = null) |
|
543 | |||
544 | /* ----------------------------------------------------------------- |
||
545 | | Check Methods |
||
546 | | ----------------------------------------------------------------- |
||
547 | */ |
||
548 | |||
549 | /** |
||
550 | * Check if user is an administrator. |
||
551 | * |
||
552 | * @return bool |
||
553 | */ |
||
554 | 12 | public function isAdmin() |
|
558 | |||
559 | /** |
||
560 | * Check if user is a moderator. |
||
561 | * |
||
562 | * @return bool |
||
563 | */ |
||
564 | 6 | public function isModerator() |
|
569 | |||
570 | /** |
||
571 | * Check if user is a member. |
||
572 | * |
||
573 | * @return bool |
||
574 | */ |
||
575 | 9 | public function isMember() |
|
579 | |||
580 | /** |
||
581 | * Check if user has a confirmed account. |
||
582 | * |
||
583 | * @return bool |
||
584 | */ |
||
585 | 9 | public function isConfirmed() |
|
589 | |||
590 | /** |
||
591 | * Check if user can be impersonated. |
||
592 | * |
||
593 | * @return bool |
||
594 | */ |
||
595 | 3 | public function canBeImpersonated() |
|
599 | } |
||
600 |