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 | ]; |
||
92 | |||
93 | /** |
||
94 | * The attributes excluded from the model's JSON form. |
||
95 | * |
||
96 | * @var array |
||
97 | */ |
||
98 | protected $hidden = [ |
||
99 | 'password', |
||
100 | 'remember_token', |
||
101 | ]; |
||
102 | |||
103 | /** |
||
104 | * The attributes that should be casted to native types. |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | protected $casts = [ |
||
109 | 'id' => 'integer', |
||
110 | 'is_admin' => 'boolean', |
||
111 | 'is_active' => 'boolean', |
||
112 | 'is_confirmed' => 'boolean', |
||
113 | ]; |
||
114 | |||
115 | /** |
||
116 | * The attributes that should be mutated to dates. |
||
117 | * |
||
118 | * @var array |
||
119 | */ |
||
120 | protected $dates = [ |
||
121 | 'email_verified_at', |
||
122 | 'last_activity', |
||
123 | 'activated_at', |
||
124 | 'deleted_at', |
||
125 | ]; |
||
126 | |||
127 | /** |
||
128 | * The event map for the model. |
||
129 | * |
||
130 | * @var array |
||
131 | */ |
||
132 | protected $dispatchesEvents = [ |
||
133 | 'creating' => CreatingUser::class, |
||
134 | 'created' => CreatedUser::class, |
||
135 | 'updating' => UpdatingUser::class, |
||
136 | 'updated' => UpdatedUser::class, |
||
137 | 'saving' => SavingUser::class, |
||
138 | 'saved' => SavedUser::class, |
||
139 | 'deleting' => DeletingUser::class, |
||
140 | 'deleted' => DeletedUser::class, |
||
141 | 'restoring' => RestoringUser::class, |
||
142 | 'restored' => RestoredUser::class, |
||
143 | ]; |
||
144 | |||
145 | /* ----------------------------------------------------------------- |
||
146 | | Constructor |
||
147 | | ----------------------------------------------------------------- |
||
148 | */ |
||
149 | |||
150 | /** |
||
151 | * Create a new Eloquent model instance. |
||
152 | * |
||
153 | * @param array $attributes |
||
154 | */ |
||
155 | 93 | public function __construct(array $attributes = []) |
|
166 | |||
167 | /* ----------------------------------------------------------------- |
||
168 | | Relationships |
||
169 | | ----------------------------------------------------------------- |
||
170 | */ |
||
171 | |||
172 | /** |
||
173 | * User belongs to many roles. |
||
174 | * |
||
175 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
176 | */ |
||
177 | 42 | public function roles() |
|
186 | |||
187 | /** |
||
188 | * Get all user permissions. |
||
189 | * |
||
190 | * @return \Illuminate\Support\Collection |
||
191 | */ |
||
192 | 9 | public function getPermissionsAttribute() |
|
201 | |||
202 | /* ----------------------------------------------------------------- |
||
203 | | Scopes |
||
204 | | ----------------------------------------------------------------- |
||
205 | */ |
||
206 | |||
207 | /** |
||
208 | * Scope last active users. |
||
209 | * |
||
210 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
211 | * @param int|null $minutes |
||
212 | * |
||
213 | * @return \Illuminate\Database\Eloquent\Builder |
||
214 | */ |
||
215 | 3 | public function scopeLastActive($query, $minutes = null) |
|
223 | |||
224 | /** |
||
225 | * Scope unverified emails' users. |
||
226 | * |
||
227 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
228 | * |
||
229 | * @return \Illuminate\Database\Eloquent\Builder |
||
230 | */ |
||
231 | 3 | public function scopeUnverifiedEmail($query) |
|
235 | |||
236 | /** |
||
237 | * Scope unverified emails' users. |
||
238 | * |
||
239 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
240 | * |
||
241 | * @return \Illuminate\Database\Eloquent\Builder |
||
242 | */ |
||
243 | 3 | public function scopeVerifiedEmail($query) |
|
247 | |||
248 | /* ----------------------------------------------------------------- |
||
249 | | Getters & Setters |
||
250 | | ----------------------------------------------------------------- |
||
251 | */ |
||
252 | |||
253 | /** |
||
254 | * Set the `email` attribute. |
||
255 | * |
||
256 | * @param string $email |
||
257 | */ |
||
258 | 75 | public function setEmailAttribute($email) |
|
262 | |||
263 | /** |
||
264 | * Set the `username` attribute. |
||
265 | * |
||
266 | * @param string $username |
||
267 | */ |
||
268 | 72 | public function setUsernameAttribute($username) |
|
272 | |||
273 | /** |
||
274 | * Set the `first_name` attribute. |
||
275 | * |
||
276 | * @param string $firstName |
||
277 | */ |
||
278 | 75 | public function setFirstNameAttribute($firstName) |
|
282 | |||
283 | /** |
||
284 | * Set the `last_name` attribute. |
||
285 | * |
||
286 | * @param string $lastName |
||
287 | */ |
||
288 | 75 | public function setLastNameAttribute($lastName) |
|
292 | |||
293 | /** |
||
294 | * Get the `full_name` attribute. |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 6 | public function getFullNameAttribute() |
|
302 | |||
303 | /** |
||
304 | * Set the `password` attribute. |
||
305 | * |
||
306 | * @param string $password |
||
307 | */ |
||
308 | 72 | public function setPasswordAttribute($password) |
|
312 | |||
313 | /* ----------------------------------------------------------------- |
||
314 | | Main Methods |
||
315 | | ----------------------------------------------------------------- |
||
316 | */ |
||
317 | |||
318 | /** |
||
319 | * Activate the model. |
||
320 | * |
||
321 | * @param bool $save |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | 3 | public function activate($save = true) |
|
333 | |||
334 | /** |
||
335 | * Deactivate the model. |
||
336 | * |
||
337 | * @param bool $save |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | 3 | public function deactivate($save = true) |
|
349 | |||
350 | /** |
||
351 | * Attach a role to a user. |
||
352 | * |
||
353 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
354 | * @param bool $reload |
||
355 | */ |
||
356 | 33 | public function attachRole($role, $reload = true) |
|
366 | |||
367 | /** |
||
368 | * Sync the roles by its slugs. |
||
369 | * |
||
370 | * @param array|\Illuminate\Support\Collection $slugs |
||
371 | * @param bool $reload |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | 3 | public function syncRoles($slugs, $reload = true) |
|
388 | |||
389 | /** |
||
390 | * Detach a role from a user. |
||
391 | * |
||
392 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
393 | * @param bool $reload |
||
394 | * |
||
395 | * @return int |
||
396 | */ |
||
397 | 3 | public function detachRole($role, $reload = true) |
|
407 | |||
408 | /** |
||
409 | * Detach all roles from a user. |
||
410 | * |
||
411 | * @param bool $reload |
||
412 | * |
||
413 | * @return int |
||
414 | */ |
||
415 | 3 | public function detachAllRoles($reload = true) |
|
425 | |||
426 | /** |
||
427 | * Update the user's last activity. |
||
428 | * |
||
429 | * @param bool $save |
||
430 | */ |
||
431 | 3 | public function updateLastActivity($save = true) |
|
437 | |||
438 | /* ----------------------------------------------------------------- |
||
439 | | Permission Check Methods |
||
440 | | ----------------------------------------------------------------- |
||
441 | */ |
||
442 | /** |
||
443 | * Check if the user has a permission. |
||
444 | * |
||
445 | * @param string $slug |
||
446 | * |
||
447 | * @return bool |
||
448 | */ |
||
449 | 9 | public function may($slug) |
|
455 | |||
456 | /** |
||
457 | * Check if the user has at least one permission. |
||
458 | * |
||
459 | * @param \Illuminate\Support\Collection|array $permissions |
||
460 | * @param \Illuminate\Support\Collection &$failed |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | 6 | public function mayOne($permissions, &$failed = null) |
|
474 | |||
475 | /** |
||
476 | * Check if the user has all permissions. |
||
477 | * |
||
478 | * @param \Illuminate\Support\Collection|array $permissions |
||
479 | * @param \Illuminate\Support\Collection &$failed |
||
480 | * |
||
481 | * @return bool |
||
482 | */ |
||
483 | 3 | public function mayAll($permissions, &$failed = null) |
|
489 | |||
490 | /* ----------------------------------------------------------------- |
||
491 | | Check Methods |
||
492 | | ----------------------------------------------------------------- |
||
493 | */ |
||
494 | |||
495 | /** |
||
496 | * Check if user is an administrator. |
||
497 | * |
||
498 | * @return bool |
||
499 | */ |
||
500 | 15 | public function isAdmin() |
|
504 | |||
505 | /** |
||
506 | * Check if user is a moderator. |
||
507 | * |
||
508 | * @return bool |
||
509 | */ |
||
510 | 6 | public function isModerator() |
|
515 | |||
516 | /** |
||
517 | * Check if user is a member. |
||
518 | * |
||
519 | * @return bool |
||
520 | */ |
||
521 | 9 | public function isMember() |
|
525 | |||
526 | /** |
||
527 | * Check if user can be impersonated. |
||
528 | * |
||
529 | * @return bool |
||
530 | */ |
||
531 | 3 | public function canBeImpersonated() |
|
535 | } |
||
536 |