createForm(RegistrationType::class, $registrationData, [ 'action' => $this->generateUrl('app_registration'), ]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $email = $this->normalizeEmail((string) $registrationData->communication->email); $registrationData->communication->email = $email; if (null !== $this->userRepository->findOneByNormalizedEmail($email)) { $this->addFlash('error', self::RESET_PASSWORD_FLASH); return $this->redirectToRoute('app_reset_password'); } if (false === $this->publicFormRateLimiter->isRegistrationAllowed($email, $request)) { $this->addFlash('error', PublicFormRateLimiter::THROTTLED_MESSAGE); return $this->render('registration/index.html.twig', [ 'form' => $form, ], new Response('', Response::HTTP_TOO_MANY_REQUESTS)); } try { $response = $this->apiClient->register($registrationData); if ($response instanceof Notification) { $this->addFlash('error', self::RESET_PASSWORD_FLASH); return $this->redirectToRoute('app_reset_password'); } if (!$response instanceof RegistrationResponse) { throw new ApiClientException('Die Registrierung konnte nicht vollständig lokal gespeichert werden.'); } $user = new User($email); $passwordHash = $this->passwordHasher->hashPassword($user, (string) $registrationData->plainPassword); $this->userBootstrapper->bootstrap( $user, $passwordHash, $response->addressId, $response->personId, [], [], true, ); $this->entityManager->persist($user); $this->entityManager->flush(); $this->addFlash('success', 'Dein Konto wurde erstellt. Du kannst dich jetzt mit deinem Passwort anmelden.'); $this->logger->info('Initiated registration', [ 'email' => $email, ]); return $this->redirectToRoute('app_login'); } catch (ApiClientException $e) { $this->addFlash('error', $e->getMessage()); $this->logger->error('Error initiating registration', [ 'email' => $email, 'error' => $e->getMessage(), ]); return $this->render('registration/index.html.twig', [ 'form' => $form, ], new Response('', Response::HTTP_BAD_GATEWAY)); } } return $this->render('registration/index.html.twig', [ 'form' => $form, ]); } private function normalizeEmail(string $email): string { return mb_strtolower(trim($email)); } }