Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AddCompanyEmployee.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 $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,
  30. ['role' => $role]
  31. );
  32. CompanyEmployeeAdded::dispatch($company, $newCompanyEmployee);
  33. }
  34. /**
  35. * Validate the add employee operation.
  36. */
  37. protected function validate(Company $company, string $email, ?string $role): void
  38. {
  39. Validator::make([
  40. 'email' => $email,
  41. 'role' => $role,
  42. ], $this->rules(), [
  43. 'email.exists' => __('filament-companies::default.errors.email_not_found'),
  44. ])->after(
  45. $this->ensureUserIsNotAlreadyOnCompany($company, $email)
  46. )->validateWithBag('addCompanyEmployee');
  47. }
  48. /**
  49. * Get the validation rules for adding a company employee.
  50. *
  51. * @return array<string, Rule|array|string>
  52. */
  53. protected function rules(): array
  54. {
  55. return array_filter([
  56. 'email' => ['required', 'email', 'exists:users'],
  57. 'role' => FilamentCompanies::hasRoles()
  58. ? ['required', 'string', new Role]
  59. : null,
  60. ]);
  61. }
  62. /**
  63. * Ensure that the user is not already on the company.
  64. */
  65. protected function ensureUserIsNotAlreadyOnCompany(Company $company, string $email): Closure
  66. {
  67. return static function ($validator) use ($company, $email) {
  68. $validator->errors()->addIf(
  69. $company->hasUserWithEmail($email),
  70. 'email',
  71. __('filament-companies::default.errors.user_belongs_to_company')
  72. );
  73. };
  74. }
  75. }