Files
myep/src/Controller/Account/RegistrationController.php
T

122 lines
4.7 KiB
PHP

<?php
namespace App\Controller\Account;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\Notification;
use App\BusProNet\Model\RegistrationResponse;
use App\Entity\User;
use App\Form\Model\RegistrationDto;
use App\Form\RegistrationType;
use App\Repository\UserRepository;
use App\Service\PublicFormRateLimiter;
use App\Service\UserBootstrapper;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class RegistrationController extends AbstractController
{
private const string RESET_PASSWORD_FLASH = 'Möglicherweise bist du bereits registriert. Bitte setze dein Passwort zurück.';
public function __construct(
private readonly ApiClient $apiClient,
private readonly EntityManagerInterface $entityManager,
private readonly UserRepository $userRepository,
private readonly PublicFormRateLimiter $publicFormRateLimiter,
private readonly UserBootstrapper $userBootstrapper,
private readonly UserPasswordHasherInterface $passwordHasher,
private readonly LoggerInterface $logger,
) {
}
#[Route('/registration', name: 'app_registration')]
#[IsGranted('PUBLIC_ACCESS')]
public function index(Request $request): Response
{
$registrationData = new RegistrationDto();
$form = $this->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));
}
}