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 | * @var array |
||
147 | */ |
||
148 | protected $dispatchesEvents = [ |
||
149 | 'creating' => CreatingUser::class, |
||
150 | 'created' => CreatedUser::class, |
||
151 | 'updating' => UpdatingUser::class, |
||
152 | 'updated' => UpdatedUser::class, |
||
153 | 'saving' => SavingUser::class, |
||
154 | 'saved' => SavedUser::class, |
||
155 | 'deleting' => DeletingUser::class, |
||
156 | 'deleted' => DeletedUser::class, |
||
157 | 'restoring' => RestoringUser::class, |
||
158 | 'restored' => RestoredUser::class, |
||
159 | ]; |
||
160 | |||
161 | /* ----------------------------------------------------------------- |
||
162 | | Constructor |
||
163 | | ----------------------------------------------------------------- |
||
164 | */ |
||
165 | |||
166 | /** |
||
167 | * Create a new Eloquent model instance. |
||
168 | * |
||
169 | * @param array $attributes |
||
170 | */ |
||
171 | 68 | public function __construct(array $attributes = []) |
|
177 | |||
178 | /** |
||
179 | * Setup the model. |
||
180 | */ |
||
181 | 68 | protected function setupModel() |
|
190 | |||
191 | /* ----------------------------------------------------------------- |
||
192 | | Relationships |
||
193 | | ----------------------------------------------------------------- |
||
194 | */ |
||
195 | |||
196 | /** |
||
197 | * User belongs to many roles. |
||
198 | * |
||
199 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
200 | */ |
||
201 | 28 | public function roles() |
|
210 | |||
211 | /** |
||
212 | * Get all user permissions. |
||
213 | * |
||
214 | * @return \Illuminate\Support\Collection |
||
215 | */ |
||
216 | 6 | public function getPermissionsAttribute() |
|
225 | |||
226 | /* ----------------------------------------------------------------- |
||
227 | | Scopes |
||
228 | | ----------------------------------------------------------------- |
||
229 | */ |
||
230 | |||
231 | /** |
||
232 | * Scope unconfirmed users by code. |
||
233 | * |
||
234 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
235 | * @param string $code |
||
236 | * |
||
237 | * @return \Illuminate\Database\Eloquent\Builder |
||
238 | */ |
||
239 | 8 | public function scopeUnconfirmed($query, $code) |
|
245 | |||
246 | /** |
||
247 | * Scope last active users. |
||
248 | * |
||
249 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
250 | * @param int|null $minutes |
||
251 | * |
||
252 | * @return \Illuminate\Database\Eloquent\Builder |
||
253 | */ |
||
254 | 2 | public function scopeLastActive($query, $minutes = null) |
|
262 | |||
263 | /* ----------------------------------------------------------------- |
||
264 | | Getters & Setters |
||
265 | | ----------------------------------------------------------------- |
||
266 | */ |
||
267 | |||
268 | /** |
||
269 | * Set the `email` attribute. |
||
270 | * |
||
271 | * @param string $email |
||
272 | */ |
||
273 | 54 | public function setEmailAttribute($email) |
|
277 | |||
278 | /** |
||
279 | * Set the `username` attribute. |
||
280 | * |
||
281 | * @param string $username |
||
282 | */ |
||
283 | 52 | public function setUsernameAttribute($username) |
|
287 | |||
288 | /** |
||
289 | * Set the `first_name` attribute. |
||
290 | * |
||
291 | * @param string $firstName |
||
292 | */ |
||
293 | 54 | public function setFirstNameAttribute($firstName) |
|
297 | |||
298 | /** |
||
299 | * Set the `last_name` attribute. |
||
300 | * |
||
301 | * @param string $lastName |
||
302 | */ |
||
303 | 54 | public function setLastNameAttribute($lastName) |
|
307 | |||
308 | /** |
||
309 | * Get the `full_name` attribute. |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | 4 | public function getFullNameAttribute() |
|
317 | |||
318 | /** |
||
319 | * Set the `password` attribute. |
||
320 | * |
||
321 | * @param string $password |
||
322 | */ |
||
323 | 52 | public function setPasswordAttribute($password) |
|
327 | |||
328 | /* ----------------------------------------------------------------- |
||
329 | | Main Methods |
||
330 | | ----------------------------------------------------------------- |
||
331 | */ |
||
332 | |||
333 | /** |
||
334 | * Activate the model. |
||
335 | * |
||
336 | * @param bool $save |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | 2 | public function activate($save = true) |
|
348 | |||
349 | /** |
||
350 | * Deactivate the model. |
||
351 | * |
||
352 | * @param bool $save |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | 2 | public function deactivate($save = true) |
|
364 | |||
365 | /** |
||
366 | * Attach a role to a user. |
||
367 | * |
||
368 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
369 | * @param bool $reload |
||
370 | */ |
||
371 | 22 | public function attachRole($role, $reload = true) |
|
381 | |||
382 | /** |
||
383 | * Sync the roles by its slugs. |
||
384 | * |
||
385 | * @param array|\Illuminate\Support\Collection $slugs |
||
386 | * @param bool $reload |
||
387 | * |
||
388 | * @return array |
||
389 | */ |
||
390 | 2 | public function syncRoles($slugs, $reload = true) |
|
403 | |||
404 | /** |
||
405 | * Detach a role from a user. |
||
406 | * |
||
407 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
408 | * @param bool $reload |
||
409 | * |
||
410 | * @return int |
||
411 | */ |
||
412 | 2 | public function detachRole($role, $reload = true) |
|
422 | |||
423 | /** |
||
424 | * Detach all roles from a user. |
||
425 | * |
||
426 | * @param bool $reload |
||
427 | * |
||
428 | * @return int |
||
429 | */ |
||
430 | 2 | public function detachAllRoles($reload = true) |
|
440 | |||
441 | /** |
||
442 | * Confirm the unconfirmed user account by confirmation code. |
||
443 | * |
||
444 | * @param string $code |
||
445 | * |
||
446 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
447 | * |
||
448 | * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException |
||
449 | */ |
||
450 | 8 | public function findUnconfirmed($code) |
|
460 | |||
461 | /** |
||
462 | * Confirm the new user account. |
||
463 | * |
||
464 | * @param \Arcanesoft\Contracts\Auth\Models\User|string $code |
||
465 | * |
||
466 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
467 | */ |
||
468 | 4 | public function confirm($code) |
|
477 | |||
478 | /** |
||
479 | * Update the user's last activity. |
||
480 | * |
||
481 | * @param bool $save |
||
482 | */ |
||
483 | 2 | public function updateLastActivity($save = true) |
|
489 | |||
490 | /* ----------------------------------------------------------------- |
||
491 | | Permission Check Methods |
||
492 | | ----------------------------------------------------------------- |
||
493 | */ |
||
494 | /** |
||
495 | * Check if the user has a permission. |
||
496 | * |
||
497 | * @param string $slug |
||
498 | * |
||
499 | * @return bool |
||
500 | */ |
||
501 | public function may($slug) |
||
507 | |||
508 | /** |
||
509 | * Check if the user has at least one permission. |
||
510 | * |
||
511 | * @param \Illuminate\Support\Collection|array $permissions |
||
512 | * @param \Illuminate\Support\Collection &$failed |
||
513 | * |
||
514 | * @return bool |
||
515 | */ |
||
516 | 4 | public function mayOne($permissions, &$failed = null) |
|
526 | |||
527 | /** |
||
528 | * Check if the user has all permissions. |
||
529 | * |
||
530 | * @param \Illuminate\Support\Collection|array $permissions |
||
531 | * @param \Illuminate\Support\Collection &$failed |
||
532 | * |
||
533 | * @return bool |
||
534 | */ |
||
535 | 2 | public function mayAll($permissions, &$failed = null) |
|
541 | |||
542 | /* ----------------------------------------------------------------- |
||
543 | | Check Methods |
||
544 | | ----------------------------------------------------------------- |
||
545 | */ |
||
546 | |||
547 | /** |
||
548 | * Check if user is an administrator. |
||
549 | * |
||
550 | * @return bool |
||
551 | */ |
||
552 | 8 | public function isAdmin() |
|
556 | |||
557 | /** |
||
558 | * Check if user is a moderator. |
||
559 | * |
||
560 | * @return bool |
||
561 | */ |
||
562 | 4 | public function isModerator() |
|
567 | |||
568 | /** |
||
569 | * Check if user is a member. |
||
570 | * |
||
571 | * @return bool |
||
572 | */ |
||
573 | 6 | public function isMember() |
|
577 | |||
578 | /** |
||
579 | * Check if user has a confirmed account. |
||
580 | * |
||
581 | * @return bool |
||
582 | */ |
||
583 | 6 | public function isConfirmed() |
|
587 | |||
588 | /** |
||
589 | * Check if user can be impersonated. |
||
590 | * |
||
591 | * @return bool |
||
592 | */ |
||
593 | 2 | public function canBeImpersonated() |
|
597 | } |
||
598 |