From c99b0299a0796769faa6205cc35b011379eb9a06 Mon Sep 17 00:00:00 2001 From: Dmitri Pisarev Date: Fri, 29 Dec 2017 10:09:21 +0300 Subject: [PATCH] BUGFIX: prevent exceptions on bad requests without subscriber --- .../Controller/SubscriptionController.php | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/Classes/Psmb/Newsletter/Controller/SubscriptionController.php b/Classes/Psmb/Newsletter/Controller/SubscriptionController.php index ab6f850..3ea0f80 100644 --- a/Classes/Psmb/Newsletter/Controller/SubscriptionController.php +++ b/Classes/Psmb/Newsletter/Controller/SubscriptionController.php @@ -68,38 +68,40 @@ public function indexAction() * @param Subscriber $subscriber * @return void */ - public function registerAction(Subscriber $subscriber) + public function registerAction(Subscriber $subscriber = null) { - $email = $subscriber->getEmail(); - if (!$email) { - $message = $this->translator->translateById('flash.noEmail', [], null, null, 'Main', 'Psmb.Newsletter'); - $this->addFlashMessage($message, null, Message::SEVERITY_WARNING); - $this->redirect('index'); - } else { - $emailValidator = new EmailAddressValidator(); - $validationResult = $emailValidator->validate($email); - if ($validationResult->hasErrors()) { - $message = $validationResult->getFirstError()->getMessage(); - $this->addFlashMessage($message, null, Message::SEVERITY_WARNING); - $this->redirect('index'); - } elseif ($this->subscriberRepository->countByEmail($email) > 0) { - $message = $this->translator->translateById('flash.alreadyRegistered', [], null, null, 'Main', 'Psmb.Newsletter'); + if ($subscriber) { + $email = $subscriber->getEmail(); + if (!$email) { + $message = $this->translator->translateById('flash.noEmail', [], null, null, 'Main', 'Psmb.Newsletter'); $this->addFlashMessage($message, null, Message::SEVERITY_WARNING); $this->redirect('index'); } else { - $subscriber->setMetadata([ - 'registrationDate' => new \DateTime(), - 'registrationDimensions' => $this->request->getInternalArgument('__node')->getContext()->getDimensions() - ]); - $hash = Algorithms::generateRandomToken(16); - $this->tokenCache->set( - $hash, - $subscriber - ); - $message = $this->translator->translateById('flash.confirm', [], null, null, 'Main', 'Psmb.Newsletter'); - $this->addFlashMessage($message); - $this->fusionMailService->sendActivationLetter($subscriber, $hash); - $this->redirect('feedback'); + $emailValidator = new EmailAddressValidator(); + $validationResult = $emailValidator->validate($email); + if ($validationResult->hasErrors()) { + $message = $validationResult->getFirstError()->getMessage(); + $this->addFlashMessage($message, null, Message::SEVERITY_WARNING); + $this->redirect('index'); + } elseif ($this->subscriberRepository->countByEmail($email) > 0) { + $message = $this->translator->translateById('flash.alreadyRegistered', [], null, null, 'Main', 'Psmb.Newsletter'); + $this->addFlashMessage($message, null, Message::SEVERITY_WARNING); + $this->redirect('index'); + } else { + $subscriber->setMetadata([ + 'registrationDate' => new \DateTime(), + 'registrationDimensions' => $this->request->getInternalArgument('__node')->getContext()->getDimensions() + ]); + $hash = Algorithms::generateRandomToken(16); + $this->tokenCache->set( + $hash, + $subscriber + ); + $message = $this->translator->translateById('flash.confirm', [], null, null, 'Main', 'Psmb.Newsletter'); + $this->addFlashMessage($message); + $this->fusionMailService->sendActivationLetter($subscriber, $hash); + $this->redirect('feedback'); + } } } }