Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AddCompanyEmployee.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Actions\FilamentCompanies;
  3. use App\Models\Company;
  4. use App\Models\User;
  5. use Closure;
  6. use Illuminate\Auth\Access\AuthorizationException;
  7. use Illuminate\Contracts\Validation\Rule;
  8. use Illuminate\Support\Facades\Gate;
  9. use Illuminate\Support\Facades\Validator;
  10. use Wallo\FilamentCompanies\Contracts\AddsCompanyEmployees;
  11. use Wallo\FilamentCompanies\Events\AddingCompanyEmployee;
  12. use Wallo\FilamentCompanies\Events\CompanyEmployeeAdded;
  13. use Wallo\FilamentCompanies\FilamentCompanies;
  14. use Wallo\FilamentCompanies\Rules\Role;
  15. class AddCompanyEmployee implements AddsCompanyEmployees
  16. {
  17. /**
  18. * Add a new company employee to the given company.
  19. *
  20. * @throws AuthorizationException
  21. */
  22. public function add(User $user, Company $company, string $email, string|null $role = null): void
  23. {
  24. Gate::forUser($user)->authorize('addCompanyEmployee', $company);
  25. $this->validate($company, $email, $role);
  26. $newCompanyEmployee = FilamentCompanies::findUserByEmailOrFail($email);
  27. AddingCompanyEmployee::dispatch($company, $newCompanyEmployee);
  28. $company->users()->attach(
  29. $newCompanyEmployee, ['role' => $role]
  30. );
  31. CompanyEmployeeAdded::dispatch($company, $newCompanyEmployee);
  32. }
  33. /**
  34. * Validate the add employee operation.
  35. */
  36. protected function validate(Company $company, string $email, ?string $role): void
  37. {
  38. Validator::make([
  39. 'email' => $email,
  40. 'role' => $role,
  41. ], $this->rules(), [
  42. 'email.exists' => __('filament-companies::default.errors.email_not_found'),
  43. ])->after(
  44. $this->ensureUserIsNotAlreadyOnCompany($company, $email)
  45. )->validateWithBag('addCompanyEmployee');
  46. }
  47. /**
  48. * Get the validation rules for adding a company employee.
  49. *
  50. * @return array<string, Rule|array|string>
  51. */
  52. protected function rules(): array
  53. {
  54. return array_filter([
  55. 'email' => ['required', 'email', 'exists:users'],
  56. 'role' => FilamentCompanies::hasRoles()
  57. ? ['required', 'string', new Role]
  58. : null,
  59. ]);
  60. }
  61. /**
  62. * Ensure that the user is not already on the company.
  63. */
  64. protected function ensureUserIsNotAlreadyOnCompany(Company $company, string $email): Closure
  65. {
  66. return static function ($validator) use ($company, $email) {
  67. $validator->errors()->addIf(
  68. $company->hasUserWithEmail($email),
  69. 'email',
  70. __('filament-companies::default.errors.user_belongs_to_company')
  71. );
  72. };
  73. }
  74. }