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