1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
53 | class User |
||
54 | extends AbstractModel |
||
55 | implements UserContract, AuthenticatableContract, AuthorizableContract, CanResetPasswordContract |
||
56 | { |
||
57 | /* ----------------------------------------------------------------- |
||
58 | | Traits |
||
59 | | ----------------------------------------------------------------- |
||
60 | */ |
||
61 | |||
62 | use Authenticatable, |
||
63 | Authorizable, |
||
64 | CanResetPassword, |
||
65 | Traits\Roleable, |
||
66 | Traits\Activatable, |
||
67 | Traits\Confirmable, |
||
68 | SoftDeletes; |
||
69 | |||
70 | /* ----------------------------------------------------------------- |
||
71 | | Properties |
||
72 | | ----------------------------------------------------------------- |
||
73 | |||
74 | */ |
||
75 | /** |
||
76 | * The attributes that are mass assignable. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $fillable = [ |
||
81 | 'username', |
||
82 | 'first_name', |
||
83 | 'last_name', |
||
84 | 'email', |
||
85 | 'password', |
||
86 | 'confirmation_code', |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * The attributes excluded from the model's JSON form. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $hidden = [ |
||
95 | 'password', |
||
96 | 'remember_token', |
||
97 | 'confirmation_code', |
||
98 | ]; |
||
99 | |||
100 | /** |
||
101 | * The attributes that should be casted to native types. |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $casts = [ |
||
106 | 'id' => 'integer', |
||
107 | 'is_admin' => 'boolean', |
||
108 | 'is_active' => 'boolean', |
||
109 | 'is_confirmed' => 'boolean', |
||
110 | ]; |
||
111 | |||
112 | /** |
||
113 | * The attributes that should be mutated to dates. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $dates = [ |
||
118 | 'confirmed_at', |
||
119 | 'last_activity', |
||
120 | 'activated_at', |
||
121 | 'deleted_at', |
||
122 | ]; |
||
123 | |||
124 | /** |
||
125 | * The event map for the model. |
||
126 | * |
||
127 | * @var array |
||
128 | */ |
||
129 | protected $dispatchesEvents = [ |
||
130 | 'creating' => CreatingUser::class, |
||
131 | 'created' => CreatedUser::class, |
||
132 | 'updating' => UpdatingUser::class, |
||
133 | 'updated' => UpdatedUser::class, |
||
134 | 'saving' => SavingUser::class, |
||
135 | 'saved' => SavedUser::class, |
||
136 | 'deleting' => DeletingUser::class, |
||
137 | 'deleted' => DeletedUser::class, |
||
138 | 'restoring' => RestoringUser::class, |
||
139 | 'restored' => RestoredUser::class, |
||
140 | ]; |
||
141 | |||
142 | /* ----------------------------------------------------------------- |
||
143 | | Constructor |
||
144 | | ----------------------------------------------------------------- |
||
145 | */ |
||
146 | |||
147 | /** |
||
148 | * Create a new Eloquent model instance. |
||
149 | * |
||
150 | * @param array $attributes |
||
151 | */ |
||
152 | 105 | public function __construct(array $attributes = []) |
|
158 | |||
159 | /** |
||
160 | * Setup the model. |
||
161 | */ |
||
162 | 105 | protected function setupModel() |
|
171 | |||
172 | /* ----------------------------------------------------------------- |
||
173 | | Relationships |
||
174 | | ----------------------------------------------------------------- |
||
175 | */ |
||
176 | |||
177 | /** |
||
178 | * User belongs to many roles. |
||
179 | * |
||
180 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
181 | */ |
||
182 | 42 | public function roles() |
|
191 | |||
192 | /** |
||
193 | * Get all user permissions. |
||
194 | * |
||
195 | * @return \Illuminate\Support\Collection |
||
196 | */ |
||
197 | 9 | public function getPermissionsAttribute() |
|
206 | |||
207 | /* ----------------------------------------------------------------- |
||
208 | | Scopes |
||
209 | | ----------------------------------------------------------------- |
||
210 | */ |
||
211 | |||
212 | /** |
||
213 | * Scope last active users. |
||
214 | * |
||
215 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
216 | * @param int|null $minutes |
||
217 | * |
||
218 | * @return \Illuminate\Database\Eloquent\Builder |
||
219 | */ |
||
220 | 3 | public function scopeLastActive($query, $minutes = null) |
|
228 | |||
229 | /* ----------------------------------------------------------------- |
||
230 | | Getters & Setters |
||
231 | | ----------------------------------------------------------------- |
||
232 | */ |
||
233 | |||
234 | /** |
||
235 | * Set the `email` attribute. |
||
236 | * |
||
237 | * @param string $email |
||
238 | */ |
||
239 | 84 | public function setEmailAttribute($email) |
|
243 | |||
244 | /** |
||
245 | * Set the `username` attribute. |
||
246 | * |
||
247 | * @param string $username |
||
248 | */ |
||
249 | 81 | public function setUsernameAttribute($username) |
|
253 | |||
254 | /** |
||
255 | * Set the `first_name` attribute. |
||
256 | * |
||
257 | * @param string $firstName |
||
258 | */ |
||
259 | 84 | public function setFirstNameAttribute($firstName) |
|
263 | |||
264 | /** |
||
265 | * Set the `last_name` attribute. |
||
266 | * |
||
267 | * @param string $lastName |
||
268 | */ |
||
269 | 84 | public function setLastNameAttribute($lastName) |
|
273 | |||
274 | /** |
||
275 | * Get the `full_name` attribute. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 6 | public function getFullNameAttribute() |
|
283 | |||
284 | /** |
||
285 | * Set the `password` attribute. |
||
286 | * |
||
287 | * @param string $password |
||
288 | */ |
||
289 | 81 | public function setPasswordAttribute($password) |
|
293 | |||
294 | /* ----------------------------------------------------------------- |
||
295 | | Main Methods |
||
296 | | ----------------------------------------------------------------- |
||
297 | */ |
||
298 | |||
299 | /** |
||
300 | * Activate the model. |
||
301 | * |
||
302 | * @param bool $save |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | 3 | public function activate($save = true) |
|
314 | |||
315 | /** |
||
316 | * Deactivate the model. |
||
317 | * |
||
318 | * @param bool $save |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | 3 | public function deactivate($save = true) |
|
330 | |||
331 | /** |
||
332 | * Attach a role to a user. |
||
333 | * |
||
334 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
335 | * @param bool $reload |
||
336 | */ |
||
337 | 33 | public function attachRole($role, $reload = true) |
|
347 | |||
348 | /** |
||
349 | * Sync the roles by its slugs. |
||
350 | * |
||
351 | * @param array|\Illuminate\Support\Collection $slugs |
||
352 | * @param bool $reload |
||
353 | * |
||
354 | * @return array |
||
355 | */ |
||
356 | 3 | public function syncRoles($slugs, $reload = true) |
|
369 | |||
370 | /** |
||
371 | * Detach a role from a user. |
||
372 | * |
||
373 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
374 | * @param bool $reload |
||
375 | * |
||
376 | * @return int |
||
377 | */ |
||
378 | 3 | public function detachRole($role, $reload = true) |
|
388 | |||
389 | /** |
||
390 | * Detach all roles from a user. |
||
391 | * |
||
392 | * @param bool $reload |
||
393 | * |
||
394 | * @return int |
||
395 | */ |
||
396 | 3 | public function detachAllRoles($reload = true) |
|
406 | |||
407 | /** |
||
408 | * Update the user's last activity. |
||
409 | * |
||
410 | * @param bool $save |
||
411 | */ |
||
412 | 3 | public function updateLastActivity($save = true) |
|
418 | |||
419 | /* ----------------------------------------------------------------- |
||
420 | | Permission Check Methods |
||
421 | | ----------------------------------------------------------------- |
||
422 | */ |
||
423 | /** |
||
424 | * Check if the user has a permission. |
||
425 | * |
||
426 | * @param string $slug |
||
427 | * |
||
428 | * @return bool |
||
429 | */ |
||
430 | 9 | public function may($slug) |
|
436 | |||
437 | /** |
||
438 | * Check if the user has at least one permission. |
||
439 | * |
||
440 | * @param \Illuminate\Support\Collection|array $permissions |
||
441 | * @param \Illuminate\Support\Collection &$failed |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | 6 | public function mayOne($permissions, &$failed = null) |
|
455 | |||
456 | /** |
||
457 | * Check if the user has all permissions. |
||
458 | * |
||
459 | * @param \Illuminate\Support\Collection|array $permissions |
||
460 | * @param \Illuminate\Support\Collection &$failed |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | 3 | public function mayAll($permissions, &$failed = null) |
|
470 | |||
471 | /* ----------------------------------------------------------------- |
||
472 | | Check Methods |
||
473 | | ----------------------------------------------------------------- |
||
474 | */ |
||
475 | |||
476 | /** |
||
477 | * Check if user is an administrator. |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | 15 | public function isAdmin() |
|
485 | |||
486 | /** |
||
487 | * Check if user is a moderator. |
||
488 | * |
||
489 | * @return bool |
||
490 | */ |
||
491 | 6 | public function isModerator() |
|
496 | |||
497 | /** |
||
498 | * Check if user is a member. |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | 9 | public function isMember() |
|
506 | |||
507 | /** |
||
508 | * Check if user can be impersonated. |
||
509 | * |
||
510 | * @return bool |
||
511 | */ |
||
512 | 3 | public function canBeImpersonated() |
|
516 | } |
||
517 |