106 lines
2.7 KiB
PHP
106 lines
2.7 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;
|
|
|
|
/**
|
|
* @property string $firstname Title.
|
|
* @property string $lastname Simple title.
|
|
* @property string $email Slug of the title.
|
|
* @property string $password SEO title.
|
|
* @property \App\Enums\Users\RoleEnum $role SEO description.
|
|
* @property \Illuminate\Support\Carbon $published Path of a picture used as banner.
|
|
* @property \Illuminate\Support\Carbon $published_at Picture's attribute title.
|
|
* @property integer $order Picture's attribute alt.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*/
|
|
class User extends Authenticatable
|
|
{
|
|
use HasFactory;
|
|
use Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'firstname',
|
|
'lastname',
|
|
'email',
|
|
'password',
|
|
'role',
|
|
'published',
|
|
'published_at',
|
|
'order'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'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
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
}
|