Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RemoveCompanyEmployee.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Actions\FilamentCompanies;
  3. use App\Models\Company;
  4. use App\Models\User;
  5. use Illuminate\Auth\Access\AuthorizationException;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Validation\ValidationException;
  8. use Wallo\FilamentCompanies\Contracts\RemovesCompanyEmployees;
  9. use Wallo\FilamentCompanies\Events\CompanyEmployeeRemoved;
  10. class RemoveCompanyEmployee implements RemovesCompanyEmployees
  11. {
  12. /**
  13. * Remove the company employee from the given company.
  14. *
  15. * @throws AuthorizationException
  16. */
  17. public function remove(User $user, Company $company, User $companyEmployee): void
  18. {
  19. $this->authorize($user, $company, $companyEmployee);
  20. $this->ensureUserDoesNotOwnCompany($companyEmployee, $company);
  21. $company->removeUser($companyEmployee);
  22. CompanyEmployeeRemoved::dispatch($company, $companyEmployee);
  23. }
  24. /**
  25. * Authorize that the user can remove the company employee.
  26. *
  27. * @throws AuthorizationException
  28. */
  29. protected function authorize(User $user, Company $company, User $companyEmployee): void
  30. {
  31. if (! Gate::forUser($user)->check('removeCompanyEmployee', $company) &&
  32. $user->id !== $companyEmployee->id) {
  33. throw new AuthorizationException;
  34. }
  35. }
  36. /**
  37. * Ensure that the currently authenticated user does not own the company.
  38. */
  39. protected function ensureUserDoesNotOwnCompany(User $companyEmployee, Company $company): void
  40. {
  41. if ($companyEmployee->id === $company->owner->id) {
  42. throw ValidationException::withMessages([
  43. 'company' => [__('filament-companies::default.errors.cannot_leave_company')],
  44. ])->errorBag('removeCompanyEmployee');
  45. }
  46. }
  47. }