src/Controller/RegisterUserController.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Customer;
  4. use App\Exception\PasswordInvalidException;
  5. use App\Helpers\Validation;
  6. use App\Repository\OdbcFieldRepository;
  7. use App\Service\MailService;
  8. use App\Service\RegisterService;
  9. use App\Service\SessionService;
  10. use App\Service\UserService;
  11. use App\Translations\DE\FormularTranslation;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Intl\Countries;
  17. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  18. use Symfony\Component\Mailer\MailerInterface;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Validator\Constraints\Email;
  22. use Symfony\Component\Validator\Constraints\Length;
  23. use Symfony\Component\Validator\Constraints\NotBlank;
  24. use Symfony\Component\Validator\Validator\ValidatorInterface;
  25. class RegisterUserController extends BaseController
  26. {
  27.     /**
  28.      * @Route("/register", name="register", methods={"GET"})
  29.      */
  30.     public function register(SessionService $sessionService): Response
  31.     {
  32.         return $this->baseRender('register.html.twig',
  33.             [
  34.                 "countries" => Countries::getNames('de')
  35.             ]
  36.         );
  37.     }
  38.     /**
  39.      * @Route("/register-validate", name="register-validate", methods={"POST"})
  40.      */
  41.     public function registerValidate(ValidatorInterface $validatorRegisterService $registerService): Response
  42.     {
  43.         $request Request::createFromGlobals();
  44.         return $registerService->validate($request$validator);
  45.     }
  46.     /**
  47.      * @Route("/register_user", name="register_user", methods={"POST"})
  48.      */
  49.     public function register_user(UserService $userServiceSessionService $sessionServiceMailerInterface $mailerOdbcFieldRepository $odbcFieldRepository): Response
  50.     {
  51.         $user $userService->getUserFromRequest();
  52.         try {
  53.             $userService->saveUser($user$mailer);
  54.             $sessionService->setSessionFlash("Registrierung erfolgreich");
  55.         } catch (PasswordInvalidException $e) {
  56.             $sessionService->setSessionFlash($e->getMessage());
  57.         }
  58.         return $this->baseRender('register_success.html.twig');
  59.     }
  60.     /**
  61.      * @Route("/change-password", name="change_password", methods={"POST", "GET"})
  62.      */
  63.     public function changePassword(UserService $userServiceSessionService $sessionServiceUserPasswordHasherInterface $passwordHasher): Response
  64.     {
  65.         $request Request::createFromGlobals();
  66.         $sessionUser $sessionService->getLoggedUser();
  67.         if ($sessionUser === null){
  68.             return $this->redirect("/");
  69.         }
  70.         $password $request->get("password");
  71.         $oldpassword $request->get("oldPassword");
  72.         $passwordConfirm $request->get("password-repeat");
  73.         if ($password !== null){
  74.             $userfromSession $userService->getUserFromInput($sessionUser->getLoginName(), $oldpassword);
  75.             $userFromDb $userService->getUserByName($sessionUser->getLoginName());
  76.             $verify Validation::validateUserLogin($userFromDb$userfromSession$passwordHasher);
  77.             if ($verify === null || $passwordConfirm != $password) {
  78.                 if($verify === null){
  79.                     $sessionService->setSessionFlash("Passwort konnte nicht validiert werden");
  80.                 } else {
  81.                     $sessionService->setSessionFlash("Passworter stimmen nicht überein");
  82.                 }
  83.                 return $this->baseRender('user/changepassword.html.twig');
  84.             }
  85.             try {
  86.                 $userService->changePassword($sessionUser$password);
  87.                 $sessionService->setSessionFlash("Passwort Änderung erfolgreich");
  88.             } catch (PasswordInvalidException $e) {
  89.                 $sessionService->setSessionFlash($e->getMessage());
  90.             }
  91.         }
  92.         return $this->baseRender('user/changepassword.html.twig');
  93.     }
  94. }