authorize($user, $company, $companyEmployee); $this->ensureUserDoesNotOwnCompany($companyEmployee, $company); $company->removeUser($companyEmployee); CompanyEmployeeRemoved::dispatch($company, $companyEmployee); } /** * Authorize that the user can remove the company employee. * * @throws AuthorizationException */ protected function authorize(User $user, Company $company, User $companyEmployee): void { if (! Gate::forUser($user)->check('removeCompanyEmployee', $company) && $user->id !== $companyEmployee->id) { throw new AuthorizationException; } } /** * Ensure that the currently authenticated user does not own the company. */ protected function ensureUserDoesNotOwnCompany(User $companyEmployee, Company $company): void { if ($companyEmployee->id === $company->owner->id) { throw ValidationException::withMessages([ 'company' => [__('filament-companies::default.errors.cannot_leave_company')], ])->errorBag('removeCompanyEmployee'); } } }