id === $model->id || parent::view($user, $model); } /** * Nobody may edit an account more privileged than their own, and only a * super admin may edit another super admin. Without this, an `admin` could * grant themselves `super_admin` through the user editor. * * @param User|Model $model */ public function update(User $user, Model $model): bool { if ($model->hasRole('super_admin') && ! $user->hasRole('super_admin')) { return false; } return parent::update($user, $model); } /** * Deleting your own account from the panel would drop you out mid-session, * and the last super admin must survive or the app locks itself out. * * @param User|Model $model */ public function delete(User $user, Model $model): bool { if ($user->id === $model->id) { return false; } if ($model->hasRole('super_admin') && ! $user->hasRole('super_admin')) { return false; } if ($model->hasRole('super_admin') && $this->isLastSuperAdmin($model)) { return false; } return parent::delete($user, $model); } protected function isLastSuperAdmin(User $model): bool { return User::query() ->whereKeyNot($model->getKey()) ->whereHas('roles', fn ($q) => $q->where('name', 'super_admin')) ->doesntExist(); } }