*/ protected $fillable = [ 'first_name', 'last_name', 'email', 'role', 'published', 'published_at', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'remember_token', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'role' => RoleEnum::class, 'last_connection' => 'date', 'published' => 'boolean', 'published_at' => 'datetime', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::checkElevationPrivileges($model); }); static::updating(function (self $model) { self::updatePublishedStatus($model); self::checkElevationPrivileges($model); }); } // * METHODS /** * Check if the authenticable user can update the published status. * * @param self $user * @return void */ private static function updatePublishedStatus(self $user): void { if (optional(auth('backend')->user())->getKey() === $user->getKey()) { \validator( ['published' => $user->published], ['published' => 'required|boolean|accepted'], ['published.accepted' => trans('Vous ne pouvez pas désactiver votre propre compte')], )->validate(); } } /** * Prevent user elevation privileges. * * @param self $user * @return void */ private static function checkElevationPrivileges(self $user): void { throw_if( auth('backend')->user() and auth('backend')->user()->role->value() > $user->role->value(), AuthorizationException::class ); } }