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 | 96 | public function __construct(array $attributes = []) |
|
179 | |||
180 | /** |
||
181 | * Setup the model. |
||
182 | */ |
||
183 | 96 | protected function setupModel() |
|
184 | { |
||
185 | 96 | $this->setTable(config('laravel-auth.users.table', 'users')); |
|
186 | |||
187 | 96 | if (SocialAuthenticator::isEnabled()) { |
|
188 | 96 | $this->hidden = array_merge($this->hidden, ['social_provider_id']); |
|
189 | 96 | $this->fillable = array_merge($this->fillable, ['social_provider', 'social_provider_id']); |
|
190 | } |
||
191 | 96 | } |
|
192 | |||
193 | /* ----------------------------------------------------------------- |
||
194 | | Relationships |
||
195 | | ----------------------------------------------------------------- |
||
196 | */ |
||
197 | |||
198 | /** |
||
199 | * User belongs to many roles. |
||
200 | * |
||
201 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
202 | */ |
||
203 | 39 | public function roles() |
|
204 | { |
||
205 | 39 | return $this->belongsToMany( |
|
206 | 39 | config('laravel-auth.roles.model', Role::class), |
|
207 | 39 | $this->getPrefix().config('laravel-auth.role-user.table', 'role_user') |
|
208 | ) |
||
209 | 39 | ->using(Pivots\RoleUser::class) |
|
210 | 39 | ->withTimestamps(); |
|
211 | } |
||
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) |
|
257 | { |
||
258 | 3 | $date = Carbon::now()->subMinutes( |
|
259 | 3 | $minutes ?: config('laravel_auth.track-activity.minutes', 5) |
|
260 | ); |
||
261 | |||
262 | 3 | return $query->where('last_activity', '>=', $date->toDateTimeString()); |
|
263 | } |
||
264 | |||
265 | /* ----------------------------------------------------------------- |
||
266 | | Getters & Setters |
||
267 | | ----------------------------------------------------------------- |
||
268 | */ |
||
269 | |||
270 | /** |
||
271 | * Set the `email` attribute. |
||
272 | * |
||
273 | * @param string $email |
||
274 | */ |
||
275 | 75 | public function setEmailAttribute($email) |
|
279 | |||
280 | /** |
||
281 | * Set the `username` attribute. |
||
282 | * |
||
283 | * @param string $username |
||
284 | */ |
||
285 | 72 | public function setUsernameAttribute($username) |
|
289 | |||
290 | /** |
||
291 | * Set the `first_name` attribute. |
||
292 | * |
||
293 | * @param string $firstName |
||
294 | */ |
||
295 | 75 | public function setFirstNameAttribute($firstName) |
|
299 | |||
300 | /** |
||
301 | * Set the `last_name` attribute. |
||
302 | * |
||
303 | * @param string $lastName |
||
304 | */ |
||
305 | 75 | 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 | 72 | 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) |
|
352 | |||
353 | /** |
||
354 | * Deactivate the model. |
||
355 | * |
||
356 | * @param bool $save |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | 3 | public function deactivate($save = true) |
|
370 | |||
371 | /** |
||
372 | * Attach a role to a user. |
||
373 | * |
||
374 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
375 | * @param bool $reload |
||
376 | */ |
||
377 | 30 | public function attachRole($role, $reload = true) |
|
387 | |||
388 | /** |
||
389 | * Sync the roles by its slugs. |
||
390 | * |
||
391 | * @param array|\Illuminate\Support\Collection $slugs |
||
392 | * @param bool $reload |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | 3 | public function syncRoles($slugs, $reload = true) |
|
409 | |||
410 | /** |
||
411 | * Detach a role from a user. |
||
412 | * |
||
413 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
414 | * @param bool $reload |
||
415 | * |
||
416 | * @return int |
||
417 | */ |
||
418 | 3 | public function detachRole($role, $reload = true) |
|
428 | |||
429 | /** |
||
430 | * Detach all roles from a user. |
||
431 | * |
||
432 | * @param bool $reload |
||
433 | * |
||
434 | * @return int |
||
435 | */ |
||
436 | 3 | public function detachAllRoles($reload = true) |
|
446 | |||
447 | /** |
||
448 | * Confirm the unconfirmed user account by confirmation code. |
||
449 | * |
||
450 | * @param string $code |
||
451 | * |
||
452 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
453 | * |
||
454 | * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException |
||
455 | */ |
||
456 | 12 | public function findUnconfirmed($code) |
|
466 | |||
467 | /** |
||
468 | * Confirm the new user account. |
||
469 | * |
||
470 | * @param \Arcanesoft\Contracts\Auth\Models\User|string $code |
||
471 | * |
||
472 | * @return \Arcanesoft\Contracts\Auth\Models\User |
||
473 | */ |
||
474 | 6 | public function confirm($code) |
|
475 | { |
||
476 | 6 | if ($code instanceof self) |
|
477 | 3 | $code = $code->confirmation_code; |
|
478 | |||
479 | 6 | return (new UserConfirmator)->confirm( |
|
480 | 6 | $this->findUnconfirmed($code) |
|
481 | ); |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Update the user's last activity. |
||
486 | * |
||
487 | * @param bool $save |
||
488 | */ |
||
489 | 3 | public function updateLastActivity($save = true) |
|
495 | |||
496 | /* ----------------------------------------------------------------- |
||
497 | | Permission Check Methods |
||
498 | | ----------------------------------------------------------------- |
||
499 | */ |
||
500 | /** |
||
501 | * Check if the user has a permission. |
||
502 | * |
||
503 | * @param string $slug |
||
504 | * |
||
505 | * @return bool |
||
506 | */ |
||
507 | 9 | public function may($slug) |
|
513 | |||
514 | /** |
||
515 | * Check if the user has at least one permission. |
||
516 | * |
||
517 | * @param \Illuminate\Support\Collection|array $permissions |
||
518 | * @param \Illuminate\Support\Collection &$failed |
||
519 | * |
||
520 | * @return bool |
||
521 | */ |
||
522 | 6 | public function mayOne($permissions, &$failed = null) |
|
532 | |||
533 | /** |
||
534 | * Check if the user has all permissions. |
||
535 | * |
||
536 | * @param \Illuminate\Support\Collection|array $permissions |
||
537 | * @param \Illuminate\Support\Collection &$failed |
||
538 | * |
||
539 | * @return bool |
||
540 | */ |
||
541 | 3 | public function mayAll($permissions, &$failed = null) |
|
547 | |||
548 | /* ----------------------------------------------------------------- |
||
549 | | Check Methods |
||
550 | | ----------------------------------------------------------------- |
||
551 | */ |
||
552 | |||
553 | /** |
||
554 | * Check if user is an administrator. |
||
555 | * |
||
556 | * @return bool |
||
557 | */ |
||
558 | 12 | public function isAdmin() |
|
562 | |||
563 | /** |
||
564 | * Check if user is a moderator. |
||
565 | * |
||
566 | * @return bool |
||
567 | */ |
||
568 | 6 | public function isModerator() |
|
573 | |||
574 | /** |
||
575 | * Check if user is a member. |
||
576 | * |
||
577 | * @return bool |
||
578 | */ |
||
579 | 9 | public function isMember() |
|
583 | |||
584 | /** |
||
585 | * Check if user has a confirmed account. |
||
586 | * |
||
587 | * @return bool |
||
588 | */ |
||
589 | 9 | public function isConfirmed() |
|
593 | |||
594 | /** |
||
595 | * Check if user can be impersonated. |
||
596 | * |
||
597 | * @return bool |
||
598 | */ |
||
599 | 3 | public function canBeImpersonated() |
|
603 | } |
||
604 |