ldc-gen-game-web/app/Models/User.php

96 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Users\RoleEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
/**
* User.
*
* @property string $firstname Firstname
* @property string $lastname Lastname
* @property string $email Email
* @property-read \Illuminate\Support\Carbon $email_verified_at Date of email verification.
* @property string $password Password
* @property-read string $remember_token Remember token
* @property \App\Enums\Users\RoleEnum $role Role of the user
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
*
* @method protected static function booted() The "booted" method of the model.
* @method private static function updatePassword($model) Update the password if needed.
*/
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname',
'lastname',
'email',
'password',
'role',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Cast the property to an enum.
*
* @var array
*/
protected $enumCasts = [
'role' => RoleEnum::class,
'email_verified_at' => 'datetime',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
static::updatePassword($model);
static::setOrder($model);
});
static::updating(function (self $model) {
static::updatePassword($model);
});
}
// * 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);
}
}
}