src/Controller/LoginController.php line 87

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\Security\NewPasswordType;
  5. use App\Form\Security\RequestPasswordType;
  6. use App\Form\Security\StepperType;
  7. use App\Repository\ProductRepository;
  8. use App\Repository\UserRepository;
  9. use App\Service\admin\RequestService;
  10. use App\Service\Mail\MailService;
  11. use App\Service\Twig\Strings;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\NonUniqueResultException;
  14. use Exception;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\Form\FormError;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  24. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  25. class LoginController extends AbstractController
  26. {
  27.     public function __construct(
  28.         private readonly EntityManagerInterface $em,
  29.         private readonly RequestStack $requestStack,
  30.         private readonly ProductRepository $productRepository,
  31.         private readonly MailService $mailService,
  32.         private readonly RequestService $requestService,
  33.         private readonly UserPasswordHasherInterface $passwordHasher,
  34.         private readonly UserRepository $userRepository,
  35.         private readonly Strings $strings
  36.     ){}
  37.     #[Route([
  38.         'hr' => '/hr/prijava',
  39.         'en' => '/en/login',
  40.         'de' => '/de/einloggen',
  41.         'it' => '/it/accedi',
  42.         'sl' => '/sl/prijava',
  43.     ], name'user_login')]
  44.     public function index(AuthenticationUtils $authenticationUtils): Response
  45.     {
  46.         $error $authenticationUtils->getLastAuthenticationError();
  47.         $lastUsername $authenticationUtils->getLastUsername();
  48.         return $this->render('security/login.html.twig', [
  49.             'last_username' => $lastUsername,
  50.             'error'         => $error,
  51.             'controller_name' => 'LoginController',
  52.         ]);
  53.     }
  54.     #[Route([
  55.         'hr' => '/hr/prijava-u-sustav',
  56.         'en' => '/en/system-login',
  57.         'de' => '/de/system-login',
  58.         'it' => '/it/accesso-sistema',
  59.         'sl' => '/sl/prijava-v-sistem',
  60.     ], name'user_portal_login')]
  61.     public function portalLogin(): Response
  62.     {
  63.         $hash $this->requestService->userPortalLogin($this->getUser());
  64.         if($hash) {
  65.             return $this->redirect($this->getParameter('portal_url') . '/user/login?webLoginHash=' $hash);
  66.         }
  67.         return $this->redirect($this->getParameter('portal_url') . '/user/login');
  68.     }
  69.     /**
  70.      * @throws TransportExceptionInterface
  71.      * @throws NonUniqueResultException
  72.      */
  73.     #[Route([
  74.         'hr' => '/hr/registracija-isprobaj-besplatno',
  75.         'en' => '/en/registration-free-trial',
  76.         'de' => '/de/registrierungs-testversion',
  77.         'it' => '/it/versione-di-prova-senza-registrazione',
  78.         'sl' => '/sl/registracija-preizkusite-brezplacno',
  79.     ], name'user_register_free_trial')]
  80.     public function freeTrial(UserPasswordHasherInterface $passwordHasher): RedirectResponse|Response
  81.     {
  82.         $form $this->createForm(StepperType::class);
  83.         $form->handleRequest($this->requestStack->getCurrentRequest());
  84.         if ($form->isSubmitted() && $form->isValid()) {
  85.             /** @var User $user */
  86.             $user $this->userRepository->findUnfinishedRegistrationsWithEmail($form->get("email")->getData());
  87.             $exUser $this->userRepository->findOneBy(["email" => $form->get("email")->getData(), "registrationFinished" => 1]);
  88.             if($exUser){
  89.                 $form->addError(new FormError($this->strings->getTranslationString("email_taken_flash_error")));
  90.                 return $this->render(
  91.                     'security/register-stepper.html.twig',
  92.                     array('form' => $form->createView())
  93.                 );
  94.             }
  95.             if($user){
  96.                 $hashedPassword $passwordHasher->hashPassword(
  97.                     $user,
  98.                     $form->get("password")->getData()
  99.                 );
  100.                 $user->setPassword($hashedPassword);
  101.                 $user->setRoles(["ROLE_USER"]);
  102.                 $confirmationToken date("dmyhis");
  103.                 $user->setConfirmationToken($confirmationToken);
  104.                 $user->setNewsletter(TRUE);
  105.                 $user->setFirstName($form->get("firstName")->getData());
  106.                 $user->setLastName($form->get("lastName")->getData());
  107.                 $user->setPhone($form->get("phone")->getData());
  108.                 $user->setCompanyName($form->get("companyName")->getData());
  109.                 $user->setPin($form->get("pin")->getData());
  110.                 $user->setRegistrationFinished(TRUE);
  111.                 // 4) save the User!
  112.                 $this->em->persist($user);
  113.                 $this->em->flush();
  114.                 if($this->requestStack->getCurrentRequest()->get('product')) {
  115.                     $product $this->productRepository->find($this->requestStack->getCurrentRequest()->get('product'));
  116.                 }
  117.                 if(!isset($product) || !$product) {
  118.                     $product $this->productRepository->findOneBy(['alias' => 'professional'], ['sequence' => 'DESC']);
  119.                 }
  120.                 $this->mailService->sendActivationEmail($usertrue$product?->getId());
  121.                 $this->requestService->setDoctorRemoteId($user);
  122.                 return $this->redirectToRoute('register_success',["form" => TRUE]);
  123.             }
  124.         }
  125.         return $this->render(
  126.             'security/register-stepper.html.twig',
  127.             array('form' => $form->createView())
  128.         );
  129.     }
  130.     /**
  131.      * @throws TransportExceptionInterface
  132.      * @throws NonUniqueResultException
  133.      */
  134.     #[Route([
  135.         'hr' => '/hr/korisnik/aktivacija/{hash}',
  136.         'en' => '/en/user/activation/{hash}',
  137.         'de' => '/de/benutzer/aktivierung/{hash}',
  138.         'it' => '/it/utente/attivazione/{hash}',
  139.         'sl' => '/sl/korisnik/aktivacija/{hash}',
  140.     ], name'user_activation')]
  141.     public function activateUser($hashRequest $request): RedirectResponse
  142.     {
  143.         $freeTrial $request->get('freetrial');
  144.         $productId $request->get('productId');
  145.         if(!$hash){
  146.             $this->addFlash("error"$this->strings->getTranslationString("activation_flash_error_message"));
  147.             return $this->redirectToRoute("user_login");
  148.         }
  149.         $user $this->em->getRepository(User::class)->findOneBy(["confirmationToken" => $hash]);
  150.         if(!$user){
  151.             $this->addFlash("error"$this->strings->getTranslationString("activation_flash_error_message"));
  152.             return $this->redirectToRoute("user_login");
  153.         }else{
  154.             $user->setIsActive(1);
  155.             $user->setConfirmationToken(1);
  156.             $this->em->persist($user);
  157.             $this->em->flush();
  158.             if($freeTrial == '1' && $productId 0) {
  159.                 $product $this->productRepository->find($productId);
  160.                 if($product) {
  161.                     $this->requestService->registerUserPortal($product$usertrue);
  162.                 }
  163.             }
  164.         }
  165.         $this->addFlash("success"$this->strings->getTranslationString("activation_flash_success_message"));
  166.         return $this->redirectToRoute("user_login");
  167.     }
  168.     /**
  169.      * @throws NonUniqueResultException
  170.      */
  171.     #[Route([
  172.         'hr' => '/hr/korisnik/lozinka',
  173.         'en' => '/en/user/password',
  174.         'de' => '/de/benutzer/passwort',
  175.         'it' => '/it/utente/chiave',
  176.         'sl' => '/sl/korisnik/geslo',
  177.     ], name'user_new_password')]
  178.     public function requestNewPassword(): Response
  179.     {
  180.         $form $this->createForm(RequestPasswordType::class);
  181.         $form->handleRequest($this->requestStack->getCurrentRequest());
  182.         if ($form->isSubmitted() && $form->isValid()) {
  183.             $email $form->get("email")->getData();
  184.             /** @var User $user */
  185.             $user $this->em->getRepository(User::class)->findOneBy(["email" => $email]);
  186.             if(!$user){
  187.                 $form->get("email")->addError(new FormError($this->strings->getTranslationString("email_not_found")));
  188.                 return $this->render("security/request_password.html.twig", [
  189.                     "form" => $form->createView()
  190.                 ]);
  191.             }else{
  192.                 $user->setNewPassword(date("ymdhis"));
  193.                 $this->em->persist($user);
  194.                 $this->em->flush();
  195.                 $this->mailService->sendNewPasswordEmail($user);
  196.                 $this->addFlash("success"$this->strings->getTranslationString("new_password_request_flash_success"));
  197.                 return $this->redirectToRoute("user_new_password");
  198.             }
  199.         }
  200.         return $this->render("security/request_password.html.twig", [
  201.             "form" => $form->createView()
  202.         ]);
  203.     }
  204.     /**
  205.      * @throws NonUniqueResultException
  206.      */
  207.     #[Route([
  208.         'hr' => '/hr/korisnik/nova-lozinka/{hash}',
  209.         'en' => '/en/user/new-password/{hash}',
  210.         'de' => '/de/benutzer/neues-passwort/{hash}',
  211.         'it' => '/it/utente/nuova-password/{hash}',
  212.         'sl' => '/sl/korisnik/novo-geslo/{hash}',
  213.     ], name'user_type_password')]
  214.     public function newPassword($hash): Response
  215.     {
  216.         $user $this->em->getRepository(User::class)->findOneBy(["newPassword" => $hash]);
  217.         if(!$user){
  218.             $this->addFlash("error"$this->strings->getTranslationString("new_password_request_flash_error"));
  219.             return $this->redirectToRoute("user_login");
  220.         }
  221.         $form $this->createForm(NewPasswordType::class, $user);
  222.         $form->handleRequest($this->requestStack->getCurrentRequest());
  223.         if ($form->isSubmitted() && $form->isValid()) {
  224.             $hashedPassword $this->passwordHasher->hashPassword(
  225.                 $user,
  226.                 $user->getPassword()
  227.             );
  228.             $user->setPassword($hashedPassword);
  229.             $user->setNewPassword(NULL);
  230.             $this->em->persist($user);
  231.             $this->em->flush();
  232.             $this->addFlash("success"$this->strings->getTranslationString("password_saved_flash_success"));
  233.             return $this->redirectToRoute("user_login");
  234.         }
  235.         return $this->render("security/password.html.twig",[
  236.             "form" => $form->createView()
  237.         ]);
  238.     }
  239.     #[Route([
  240.         'hr' => '/hr/uspjesna-registracija',
  241.         'en' => '/en/successful-registration',
  242.         'de' => '/de/erfolgreiche-registrierung',
  243.         'it' => '/it/registrazione-riuscita',
  244.         'sl' => '/sl/uspesna-registracija',
  245.     ], name'register_success')]
  246.     public function success(): Response
  247.     {
  248.         return $this->render("security/register_success.html.twig");
  249.     }
  250.     /**
  251.      * @throws Exception
  252.      */
  253.     #[Route([
  254.         'hr' => '/hr/odjava',
  255.         'en' => '/en/logout',
  256.         'de' => '/de/ausloggen',
  257.         'it' => '/it/disconnettersi',
  258.         'sl' => '/sl/odjava',
  259.     ], name'logout')]
  260.     public function logout()
  261.     {
  262.     }
  263. }