1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
58 | class User |
||
59 | extends AbstractModel |
||
60 | implements UserContract, AuthenticatableContract, AuthorizableContract, CanResetPasswordContract |
||
61 | { |
||
62 | /* ----------------------------------------------------------------- |
||
63 | | Traits |
||
64 | | ----------------------------------------------------------------- |
||
65 | */ |
||
66 | |||
67 | use Authenticatable, |
||
68 | Authorizable, |
||
69 | CanResetPassword, |
||
70 | MustVerifyEmail, |
||
71 | Traits\Roleable, |
||
72 | Traits\Activatable, |
||
73 | SoftDeletes; |
||
74 | |||
75 | /* ----------------------------------------------------------------- |
||
76 | | Properties |
||
77 | | ----------------------------------------------------------------- |
||
78 | |||
79 | */ |
||
80 | /** |
||
81 | * The attributes that are mass assignable. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $fillable = [ |
||
86 | 'username', |
||
87 | 'first_name', |
||
88 | 'last_name', |
||
89 | 'email', |
||
90 | 'password', |
||
91 | 'confirmation_code', |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * The attributes excluded from the model's JSON form. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $hidden = [ |
||
100 | 'password', |
||
101 | 'remember_token', |
||
102 | 'confirmation_code', |
||
103 | ]; |
||
104 | |||
105 | /** |
||
106 | * The attributes that should be casted to native types. |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $casts = [ |
||
111 | 'id' => 'integer', |
||
112 | 'is_admin' => 'boolean', |
||
113 | 'is_active' => 'boolean', |
||
114 | 'is_confirmed' => 'boolean', |
||
115 | ]; |
||
116 | |||
117 | /** |
||
118 | * The attributes that should be mutated to dates. |
||
119 | * |
||
120 | * @var array |
||
121 | */ |
||
122 | protected $dates = [ |
||
123 | 'confirmed_at', |
||
124 | 'last_activity', |
||
125 | 'activated_at', |
||
126 | 'deleted_at', |
||
127 | ]; |
||
128 | |||
129 | /** |
||
130 | * The event map for the model. |
||
131 | * |
||
132 | * @var array |
||
133 | */ |
||
134 | protected $dispatchesEvents = [ |
||
135 | 'creating' => CreatingUser::class, |
||
136 | 'created' => CreatedUser::class, |
||
137 | 'updating' => UpdatingUser::class, |
||
138 | 'updated' => UpdatedUser::class, |
||
139 | 'saving' => SavingUser::class, |
||
140 | 'saved' => SavedUser::class, |
||
141 | 'deleting' => DeletingUser::class, |
||
142 | 'deleted' => DeletedUser::class, |
||
143 | 'restoring' => RestoringUser::class, |
||
144 | 'restored' => RestoredUser::class, |
||
145 | ]; |
||
146 | |||
147 | /* ----------------------------------------------------------------- |
||
148 | | Constructor |
||
149 | | ----------------------------------------------------------------- |
||
150 | */ |
||
151 | |||
152 | /** |
||
153 | * Create a new Eloquent model instance. |
||
154 | * |
||
155 | * @param array $attributes |
||
156 | */ |
||
157 | 93 | public function __construct(array $attributes = []) |
|
163 | |||
164 | /** |
||
165 | * Setup the model. |
||
166 | */ |
||
167 | 93 | protected function setupModel() |
|
176 | |||
177 | /* ----------------------------------------------------------------- |
||
178 | | Relationships |
||
179 | | ----------------------------------------------------------------- |
||
180 | */ |
||
181 | |||
182 | /** |
||
183 | * User belongs to many roles. |
||
184 | * |
||
185 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
186 | */ |
||
187 | 42 | public function roles() |
|
196 | |||
197 | /** |
||
198 | * Get all user permissions. |
||
199 | * |
||
200 | * @return \Illuminate\Support\Collection |
||
201 | */ |
||
202 | 9 | public function getPermissionsAttribute() |
|
211 | |||
212 | /* ----------------------------------------------------------------- |
||
213 | | Scopes |
||
214 | | ----------------------------------------------------------------- |
||
215 | */ |
||
216 | |||
217 | /** |
||
218 | * Scope last active users. |
||
219 | * |
||
220 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
221 | * @param int|null $minutes |
||
222 | * |
||
223 | * @return \Illuminate\Database\Eloquent\Builder |
||
224 | */ |
||
225 | 3 | public function scopeLastActive($query, $minutes = null) |
|
233 | |||
234 | /** |
||
235 | * Scope unverified emails' users. |
||
236 | * |
||
237 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
238 | * |
||
239 | * @return \Illuminate\Database\Eloquent\Builder |
||
240 | */ |
||
241 | 3 | public function scopeUnverifiedEmail($query) |
|
245 | |||
246 | /** |
||
247 | * Scope unverified emails' users. |
||
248 | * |
||
249 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
250 | * |
||
251 | * @return \Illuminate\Database\Eloquent\Builder |
||
252 | */ |
||
253 | 3 | public function scopeVerifiedEmail($query) |
|
257 | |||
258 | /* ----------------------------------------------------------------- |
||
259 | | Getters & Setters |
||
260 | | ----------------------------------------------------------------- |
||
261 | */ |
||
262 | |||
263 | /** |
||
264 | * Set the `email` attribute. |
||
265 | * |
||
266 | * @param string $email |
||
267 | */ |
||
268 | 75 | public function setEmailAttribute($email) |
|
272 | |||
273 | /** |
||
274 | * Set the `username` attribute. |
||
275 | * |
||
276 | * @param string $username |
||
277 | */ |
||
278 | 72 | public function setUsernameAttribute($username) |
|
282 | |||
283 | /** |
||
284 | * Set the `first_name` attribute. |
||
285 | * |
||
286 | * @param string $firstName |
||
287 | */ |
||
288 | 75 | public function setFirstNameAttribute($firstName) |
|
292 | |||
293 | /** |
||
294 | * Set the `last_name` attribute. |
||
295 | * |
||
296 | * @param string $lastName |
||
297 | */ |
||
298 | 75 | public function setLastNameAttribute($lastName) |
|
302 | |||
303 | /** |
||
304 | * Get the `full_name` attribute. |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 6 | public function getFullNameAttribute() |
|
312 | |||
313 | /** |
||
314 | * Set the `password` attribute. |
||
315 | * |
||
316 | * @param string $password |
||
317 | */ |
||
318 | 72 | public function setPasswordAttribute($password) |
|
322 | |||
323 | /* ----------------------------------------------------------------- |
||
324 | | Main Methods |
||
325 | | ----------------------------------------------------------------- |
||
326 | */ |
||
327 | |||
328 | /** |
||
329 | * Activate the model. |
||
330 | * |
||
331 | * @param bool $save |
||
332 | * |
||
333 | * @return bool |
||
334 | */ |
||
335 | 3 | public function activate($save = true) |
|
343 | |||
344 | /** |
||
345 | * Deactivate the model. |
||
346 | * |
||
347 | * @param bool $save |
||
348 | * |
||
349 | * @return bool |
||
350 | */ |
||
351 | 3 | public function deactivate($save = true) |
|
359 | |||
360 | /** |
||
361 | * Attach a role to a user. |
||
362 | * |
||
363 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
364 | * @param bool $reload |
||
365 | */ |
||
366 | 33 | public function attachRole($role, $reload = true) |
|
376 | |||
377 | /** |
||
378 | * Sync the roles by its slugs. |
||
379 | * |
||
380 | * @param array|\Illuminate\Support\Collection $slugs |
||
381 | * @param bool $reload |
||
382 | * |
||
383 | * @return array |
||
384 | */ |
||
385 | 3 | public function syncRoles($slugs, $reload = true) |
|
398 | |||
399 | /** |
||
400 | * Detach a role from a user. |
||
401 | * |
||
402 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
403 | * @param bool $reload |
||
404 | * |
||
405 | * @return int |
||
406 | */ |
||
407 | 3 | public function detachRole($role, $reload = true) |
|
417 | |||
418 | /** |
||
419 | * Detach all roles from a user. |
||
420 | * |
||
421 | * @param bool $reload |
||
422 | * |
||
423 | * @return int |
||
424 | */ |
||
425 | 3 | public function detachAllRoles($reload = true) |
|
435 | |||
436 | /** |
||
437 | * Update the user's last activity. |
||
438 | * |
||
439 | * @param bool $save |
||
440 | */ |
||
441 | 3 | public function updateLastActivity($save = true) |
|
447 | |||
448 | /* ----------------------------------------------------------------- |
||
449 | | Permission Check Methods |
||
450 | | ----------------------------------------------------------------- |
||
451 | */ |
||
452 | /** |
||
453 | * Check if the user has a permission. |
||
454 | * |
||
455 | * @param string $slug |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | 9 | public function may($slug) |
|
465 | |||
466 | /** |
||
467 | * Check if the user has at least one permission. |
||
468 | * |
||
469 | * @param \Illuminate\Support\Collection|array $permissions |
||
470 | * @param \Illuminate\Support\Collection &$failed |
||
471 | * |
||
472 | * @return bool |
||
473 | */ |
||
474 | 6 | public function mayOne($permissions, &$failed = null) |
|
484 | |||
485 | /** |
||
486 | * Check if the user has all permissions. |
||
487 | * |
||
488 | * @param \Illuminate\Support\Collection|array $permissions |
||
489 | * @param \Illuminate\Support\Collection &$failed |
||
490 | * |
||
491 | * @return bool |
||
492 | */ |
||
493 | 3 | public function mayAll($permissions, &$failed = null) |
|
499 | |||
500 | /* ----------------------------------------------------------------- |
||
501 | | Check Methods |
||
502 | | ----------------------------------------------------------------- |
||
503 | */ |
||
504 | |||
505 | /** |
||
506 | * Check if user is an administrator. |
||
507 | * |
||
508 | * @return bool |
||
509 | */ |
||
510 | 15 | public function isAdmin() |
|
514 | |||
515 | /** |
||
516 | * Check if user is a moderator. |
||
517 | * |
||
518 | * @return bool |
||
519 | */ |
||
520 | 6 | public function isModerator() |
|
525 | |||
526 | /** |
||
527 | * Check if user is a member. |
||
528 | * |
||
529 | * @return bool |
||
530 | */ |
||
531 | 9 | public function isMember() |
|
535 | |||
536 | /** |
||
537 | * Check if user can be impersonated. |
||
538 | * |
||
539 | * @return bool |
||
540 | */ |
||
541 | 3 | public function canBeImpersonated() |
|
545 | } |
||
546 |