*/ #[\Override] protected $fillable = [ 'first_name', 'last_name', 'email', 'password', 'role', 'published', 'published_at', ]; /** * The attributes that should be hidden for serialization. * * @var list */ #[\Override] protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array */ #[\Override] protected $casts = [ 'role' => RoleEnum::class, 'email_verified_at' => 'datetime', 'published' => 'boolean', ]; // * EVENTS /** * Perform any actions required after the model boots. */ protected static function booted(): void { static::creating(function (self $model): void { self::updatePassword($model); // * Prevent user elevation privileges throw_if( auth('backend')->user() && auth('backend')->user()->role->value() > $model->role->value(), AuthorizationException::class ); }); static::updating(function (self $model): void { self::updatePassword($model); if (auth('backend')->user()?->getKey() === $model->getKey()) { \validator( ['published' => $model->published], ['published' => 'required|boolean|accepted'], ['published.accepted' => trans('Vous ne pouvez pas déactiver votre propre compte')], )->validate(); } // * Prevent user elevation privileges throw_if( auth('backend')->user() && auth('backend')->user()->role->value() > $model->role->value(), AuthorizationException::class ); }); } // * METHODS /** * Update password. */ private static function updatePassword(self $model): void { if (Hash::needsRehash($model->password)) { $model->password = Hash::make($model->password); } } // * RELATIONS /** * Activities logs One-to-many relationship. * * @return HasMany */ public function activityLogs(): HasMany { return $this->hasMany(ActivityLog::class); } }