Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RemoveCompanyEmployee.php 1.7KB

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