2023-03-14 14:57:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2023-07-13 08:34:54 +00:00
|
|
|
use App\Enums\UserRolesEnum;
|
2023-03-14 14:57:03 +01:00
|
|
|
|
2025-05-20 10:16:12 +02:00
|
|
|
/**
|
|
|
|
|
* @property integer $firstname User first name.
|
|
|
|
|
* @property string $lastname User last name.
|
|
|
|
|
* @property string $email User email.
|
|
|
|
|
* @property string $email_verified_at Email verified date.
|
|
|
|
|
* @property string $password User password.
|
|
|
|
|
* @property string $remember_token User token remember.
|
|
|
|
|
* @property \App\Enums\UserRolesEnum $role User role.
|
|
|
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
|
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
|
|
|
*/
|
2023-03-14 14:57:03 +01:00
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
use Notifiable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @var list<string>
|
2023-03-14 14:57:03 +01:00
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'firstname',
|
|
|
|
|
'lastname',
|
|
|
|
|
'email',
|
2023-03-27 12:17:31 +02:00
|
|
|
'email_verified_at',
|
2023-03-14 14:57:03 +01:00
|
|
|
'password',
|
2023-03-27 12:17:31 +02:00
|
|
|
'role'
|
2023-03-14 14:57:03 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be hidden for serialization.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @var list<string>
|
2023-03-14 14:57:03 +01:00
|
|
|
*/
|
|
|
|
|
protected $hidden = [
|
|
|
|
|
'password',
|
|
|
|
|
'remember_token',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
2025-05-20 10:16:12 +02:00
|
|
|
'role' => UserRolesEnum::class
|
2023-07-13 08:34:54 +00:00
|
|
|
];
|
|
|
|
|
|
2023-03-14 14:57:03 +01:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
protected static function booted(): void
|
2023-03-14 14:57:03 +01:00
|
|
|
{
|
|
|
|
|
// * EVENT CLOSURES.
|
2023-07-25 12:55:22 +02:00
|
|
|
static::creating(function (self $model) {
|
2025-05-20 10:16:12 +02:00
|
|
|
self::updatePassword($model);
|
2023-03-14 14:57:03 +01:00
|
|
|
});
|
2023-07-25 12:55:22 +02:00
|
|
|
static::updating(function (self $model) {
|
2025-05-20 10:16:12 +02:00
|
|
|
self::updatePassword($model);
|
2023-03-14 14:57:03 +01:00
|
|
|
});
|
|
|
|
|
}
|
2023-07-25 12:55:22 +02:00
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update password.
|
|
|
|
|
*
|
|
|
|
|
* @param self $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private static function updatePassword(self $model): void
|
|
|
|
|
{
|
|
|
|
|
if (Hash::needsRehash($model->password)) {
|
|
|
|
|
$model->password = Hash::make($model->password);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-21 17:12:26 +02:00
|
|
|
|
2023-07-21 14:51:02 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MorphToMany directories relation.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function directories(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
2023-07-21 14:51:02 +02:00
|
|
|
{
|
|
|
|
|
return $this->morphToMany(Directory::class, 'directorables');
|
|
|
|
|
}
|
2023-03-14 14:57:03 +01:00
|
|
|
}
|