From 3e59031ba943fc25e300351853869d9e92e7469b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Tue, 3 Dec 2024 16:08:06 +0100 Subject: [PATCH] feat: registration and password reset --- src/BusProNet/ApiClient.php | 65 +++++++++++++++------- src/Controller/RegistrationController.php | 50 +++++++++++++++++ src/Controller/ResetPasswordController.php | 61 ++++++++++++++++++++ src/Form/Model/ParticipantData.php | 1 + src/Form/Model/RegistrationData.php | 22 ++++++++ src/Form/RegistrationType.php | 45 +++++++++++++++ templates/layout.html.twig | 4 +- templates/registration/index.html.twig | 26 +++++++++ templates/reset_password/index.html.twig | 23 ++++++++ templates/security/login.html.twig | 10 +++- 10 files changed, 285 insertions(+), 22 deletions(-) create mode 100644 src/Controller/RegistrationController.php create mode 100644 src/Controller/ResetPasswordController.php create mode 100644 src/Form/Model/RegistrationData.php create mode 100644 src/Form/RegistrationType.php create mode 100644 templates/registration/index.html.twig create mode 100644 templates/reset_password/index.html.twig diff --git a/src/BusProNet/ApiClient.php b/src/BusProNet/ApiClient.php index 0faabe1..71c809e 100644 --- a/src/BusProNet/ApiClient.php +++ b/src/BusProNet/ApiClient.php @@ -13,6 +13,7 @@ use App\BusProNet\Model\File; use App\BusProNet\Model\Notification; use App\BusProNet\Model\PersonalData; use App\Form\Model\BookingData; +use App\Form\Model\RegistrationData; use Psr\Log\LoggerInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Serializer\Encoder\XmlEncoder; @@ -33,10 +34,11 @@ class ApiClient public function __construct( private readonly HttpClientInterface $httpClient, private readonly SerializerInterface $serializer, - private readonly ResponseParser $responseParser, - private readonly LoggerInterface $logger, - array $options - ) { + private readonly ResponseParser $responseParser, + private readonly LoggerInterface $logger, + array $options + ) + { $this->config = $this->resolveOptions($options); } @@ -57,6 +59,45 @@ class ApiClient return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data); } + /** + * @throws ApiClientException + */ + public function register(RegistrationData $registrationData): Notification + { + $data = [ + 'user' => $this->config['bpn_username'], + 'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], static::TYPE_CUSTOMER_DATA), + 'satz' => ['@typ' => static::TYPE_CUSTOMER_DATA], + 'art' => 'Adresse_Neu', + 'adressdaten' => [ + 'geschlecht' => $registrationData->gender, + 'vorname' => $registrationData->firstName, + 'name' => $registrationData->name, + 'kommunikation' => [ + 'email' => $registrationData->email, + ], + ], + ]; + + return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data); + } + + /** + * @throws ApiClientException + */ + public function resetPassword(string $email): Notification + { + $data = [ + 'user' => $this->config['bpn_username'], + 'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], static::TYPE_CUSTOMER_DATA), + 'satz' => ['@typ' => static::TYPE_CUSTOMER_DATA], + 'art' => 'Passwort_Anfrage', + 'email' => $email, + ]; + + return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data); + } + /** * @throws ApiClientException */ @@ -180,22 +221,6 @@ class ApiClient return $this->sendRequest(static::TYPE_AVAILABILITY, $data); } - /** - * @throws ApiClientException - */ - public function resetPassword(string $email): Notification - { - $data = [ - 'user' => $this->config['bpn_username'], - 'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], static::TYPE_CUSTOMER_DATA), - 'satz' => ['@typ' => static::TYPE_CUSTOMER_DATA], - 'art' => 'Passwort_Anfrage', - 'email' => $email, - ]; - - return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data); - } - /** * @throws ApiClientException */ diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php new file mode 100644 index 0000000..8110e87 --- /dev/null +++ b/src/Controller/RegistrationController.php @@ -0,0 +1,50 @@ +createForm(RegistrationType::class, $registrationData,[ + 'action' => $this->generateUrl('app_registration'), + ]); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + try { + $response = $this->apiClient->register($registrationData); + if ($response->isSuccessful()) { + $this->addFlash('success', 'Du erhältst in Kürze eine E-Mail mit einem Link zum (Zurück)setzen deines Passworts.'); + } else { + $this->addFlash('error', 'Möglicherweise bist du bereits registriert. Bitte setze dein Passwort zurück.'); + } + } catch (ApiClientException $e) { + $this->addFlash('error', $e->getMessage()); + } + + return $this->redirectToRoute('app_login'); + } + + return $this->render('registration/index.html.twig', [ + 'form' => $form->createView(), + ]); + } +} \ No newline at end of file diff --git a/src/Controller/ResetPasswordController.php b/src/Controller/ResetPasswordController.php new file mode 100644 index 0000000..034cc22 --- /dev/null +++ b/src/Controller/ResetPasswordController.php @@ -0,0 +1,61 @@ +createFormBuilder(null, [ + 'action' => $this->generateUrl('app_reset_password'), + ]) + ->add('email', EmailType::class, [ + 'label' => 'E-Mail', + 'constraints' => [ + new NotBlank([ + 'message' => 'Bitte angeben', + ]), + new Email([ + 'mode' => 'strict', + 'message' => 'Bitte eine gültige E-Mail Adresse angeben', + ]) + ], + ]) + ->getForm() + ; + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + try { + $this->apiClient->resetPassword($form->get('email')->getData()); + $this->addFlash('success', 'Du erhältst in Kürze eine E-Mail mit einem Link zum (Zurück)setzen deines Passworts.'); + } catch (ApiClientException $e) { + $this->addFlash('error', $e->getMessage()); + } + + return $this->redirectToRoute('app_login'); + } + + return $this->render('reset_password/index.html.twig', [ + 'form' => $form->createView(), + ]); + } +} \ No newline at end of file diff --git a/src/Form/Model/ParticipantData.php b/src/Form/Model/ParticipantData.php index 13ac0ff..613aab2 100644 --- a/src/Form/Model/ParticipantData.php +++ b/src/Form/Model/ParticipantData.php @@ -37,6 +37,7 @@ class ParticipantData public ?\DateTimeImmutable $dateOfBirth = null; #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\Email(message: 'Bitte eine gültige E-Mail Adresse angeben', mode: 'strict')] public ?string $email = null; #[Assert\NotBlank(message: 'Bitte angeben')] diff --git a/src/Form/Model/RegistrationData.php b/src/Form/Model/RegistrationData.php new file mode 100644 index 0000000..e5c1321 --- /dev/null +++ b/src/Form/Model/RegistrationData.php @@ -0,0 +1,22 @@ +add('gender', ChoiceType::class, [ + 'label' => 'Gender', + 'choices' => [ + 'männlich' => 'M', + 'weiblich' => 'W', + 'divers' => 'D', + ], + ]) + ->add('firstName', TextType::class, [ + 'label' => 'Name', + ]) + ->add('name', TextType::class, [ + 'label' => 'Nachname', + ]) + ->add('email', EmailType::class, [ + 'label' => 'E-Mail', + 'property_path' => 'email', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => RegistrationData::class, + ]); + } +} \ No newline at end of file diff --git a/templates/layout.html.twig b/templates/layout.html.twig index cb85805..f238ddb 100644 --- a/templates/layout.html.twig +++ b/templates/layout.html.twig @@ -3,7 +3,9 @@ hx-boost="true" hx-indicator="#loading-indicator">
- My E&P Team +
+ MyE&P +
{% if is_granted('ROLE_USER') %} {% endif %}

- Login MyE&P + Login

@@ -47,6 +47,14 @@ +