authorize('addCompanyEmployee', $company); $this->validate($company, $email, $role); $newCompanyEmployee = FilamentCompanies::findUserByEmailOrFail($email); AddingCompanyEmployee::dispatch($company, $newCompanyEmployee); $company->users()->attach( $newCompanyEmployee, ['role' => $role] ); CompanyEmployeeAdded::dispatch($company, $newCompanyEmployee); } /** * Validate the add employee operation. */ protected function validate(Company $company, string $email, ?string $role): void { Validator::make([ 'email' => $email, 'role' => $role, ], $this->rules(), [ 'email.exists' => __('filament-companies::default.errors.email_not_found'), ])->after( $this->ensureUserIsNotAlreadyOnCompany($company, $email) )->validateWithBag('addCompanyEmployee'); } /** * Get the validation rules for adding a company employee. * * @return array */ protected function rules(): array { return array_filter([ 'email' => ['required', 'email', 'exists:users'], 'role' => FilamentCompanies::hasRoles() ? ['required', 'string', new Role] : null, ]); } /** * Ensure that the user is not already on the company. */ protected function ensureUserIsNotAlreadyOnCompany(Company $company, string $email): Closure { return static function ($validator) use ($company, $email) { $validator->errors()->addIf( $company->hasUserWithEmail($email), 'email', __('filament-companies::default.errors.user_belongs_to_company') ); }; } }