src/Controller/LoginController.php line 87
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\Security\NewPasswordType;
use App\Form\Security\RequestPasswordType;
use App\Form\Security\StepperType;
use App\Repository\ProductRepository;
use App\Repository\UserRepository;
use App\Service\admin\RequestService;
use App\Service\Mail\MailService;
use App\Service\Twig\Strings;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class LoginController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly RequestStack $requestStack,
private readonly ProductRepository $productRepository,
private readonly MailService $mailService,
private readonly RequestService $requestService,
private readonly UserPasswordHasherInterface $passwordHasher,
private readonly UserRepository $userRepository,
private readonly Strings $strings
){}
#[Route([
'hr' => '/hr/prijava',
'en' => '/en/login',
'de' => '/de/einloggen',
'it' => '/it/accedi',
'sl' => '/sl/prijava',
], name: 'user_login')]
public function index(AuthenticationUtils $authenticationUtils): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'controller_name' => 'LoginController',
]);
}
#[Route([
'hr' => '/hr/prijava-u-sustav',
'en' => '/en/system-login',
'de' => '/de/system-login',
'it' => '/it/accesso-sistema',
'sl' => '/sl/prijava-v-sistem',
], name: 'user_portal_login')]
public function portalLogin(): Response
{
$hash = $this->requestService->userPortalLogin($this->getUser());
if($hash) {
return $this->redirect($this->getParameter('portal_url') . '/user/login?webLoginHash=' . $hash);
}
return $this->redirect($this->getParameter('portal_url') . '/user/login');
}
/**
* @throws TransportExceptionInterface
* @throws NonUniqueResultException
*/
#[Route([
'hr' => '/hr/registracija-isprobaj-besplatno',
'en' => '/en/registration-free-trial',
'de' => '/de/registrierungs-testversion',
'it' => '/it/versione-di-prova-senza-registrazione',
'sl' => '/sl/registracija-preizkusite-brezplacno',
], name: 'user_register_free_trial')]
public function freeTrial(UserPasswordHasherInterface $passwordHasher): RedirectResponse|Response
{
$form = $this->createForm(StepperType::class);
$form->handleRequest($this->requestStack->getCurrentRequest());
if ($form->isSubmitted() && $form->isValid()) {
/** @var User $user */
$user = $this->userRepository->findUnfinishedRegistrationsWithEmail($form->get("email")->getData());
$exUser = $this->userRepository->findOneBy(["email" => $form->get("email")->getData(), "registrationFinished" => 1]);
if($exUser){
$form->addError(new FormError($this->strings->getTranslationString("email_taken_flash_error")));
return $this->render(
'security/register-stepper.html.twig',
array('form' => $form->createView())
);
}
if($user){
$hashedPassword = $passwordHasher->hashPassword(
$user,
$form->get("password")->getData()
);
$user->setPassword($hashedPassword);
$user->setRoles(["ROLE_USER"]);
$confirmationToken = date("dmyhis");
$user->setConfirmationToken($confirmationToken);
$user->setNewsletter(TRUE);
$user->setFirstName($form->get("firstName")->getData());
$user->setLastName($form->get("lastName")->getData());
$user->setPhone($form->get("phone")->getData());
$user->setCompanyName($form->get("companyName")->getData());
$user->setPin($form->get("pin")->getData());
$user->setRegistrationFinished(TRUE);
// 4) save the User!
$this->em->persist($user);
$this->em->flush();
if($this->requestStack->getCurrentRequest()->get('product')) {
$product = $this->productRepository->find($this->requestStack->getCurrentRequest()->get('product'));
}
if(!isset($product) || !$product) {
$product = $this->productRepository->findOneBy(['alias' => 'professional'], ['sequence' => 'DESC']);
}
$this->mailService->sendActivationEmail($user, true, $product?->getId());
$this->requestService->setDoctorRemoteId($user);
return $this->redirectToRoute('register_success',["form" => TRUE]);
}
}
return $this->render(
'security/register-stepper.html.twig',
array('form' => $form->createView())
);
}
/**
* @throws TransportExceptionInterface
* @throws NonUniqueResultException
*/
#[Route([
'hr' => '/hr/korisnik/aktivacija/{hash}',
'en' => '/en/user/activation/{hash}',
'de' => '/de/benutzer/aktivierung/{hash}',
'it' => '/it/utente/attivazione/{hash}',
'sl' => '/sl/korisnik/aktivacija/{hash}',
], name: 'user_activation')]
public function activateUser($hash, Request $request): RedirectResponse
{
$freeTrial = $request->get('freetrial');
$productId = $request->get('productId');
if(!$hash){
$this->addFlash("error", $this->strings->getTranslationString("activation_flash_error_message"));
return $this->redirectToRoute("user_login");
}
$user = $this->em->getRepository(User::class)->findOneBy(["confirmationToken" => $hash]);
if(!$user){
$this->addFlash("error", $this->strings->getTranslationString("activation_flash_error_message"));
return $this->redirectToRoute("user_login");
}else{
$user->setIsActive(1);
$user->setConfirmationToken(1);
$this->em->persist($user);
$this->em->flush();
if($freeTrial == '1' && $productId > 0) {
$product = $this->productRepository->find($productId);
if($product) {
$this->requestService->registerUserPortal($product, $user, true);
}
}
}
$this->addFlash("success", $this->strings->getTranslationString("activation_flash_success_message"));
return $this->redirectToRoute("user_login");
}
/**
* @throws NonUniqueResultException
*/
#[Route([
'hr' => '/hr/korisnik/lozinka',
'en' => '/en/user/password',
'de' => '/de/benutzer/passwort',
'it' => '/it/utente/chiave',
'sl' => '/sl/korisnik/geslo',
], name: 'user_new_password')]
public function requestNewPassword(): Response
{
$form = $this->createForm(RequestPasswordType::class);
$form->handleRequest($this->requestStack->getCurrentRequest());
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->get("email")->getData();
/** @var User $user */
$user = $this->em->getRepository(User::class)->findOneBy(["email" => $email]);
if(!$user){
$form->get("email")->addError(new FormError($this->strings->getTranslationString("email_not_found")));
return $this->render("security/request_password.html.twig", [
"form" => $form->createView()
]);
}else{
$user->setNewPassword(date("ymdhis"));
$this->em->persist($user);
$this->em->flush();
$this->mailService->sendNewPasswordEmail($user);
$this->addFlash("success", $this->strings->getTranslationString("new_password_request_flash_success"));
return $this->redirectToRoute("user_new_password");
}
}
return $this->render("security/request_password.html.twig", [
"form" => $form->createView()
]);
}
/**
* @throws NonUniqueResultException
*/
#[Route([
'hr' => '/hr/korisnik/nova-lozinka/{hash}',
'en' => '/en/user/new-password/{hash}',
'de' => '/de/benutzer/neues-passwort/{hash}',
'it' => '/it/utente/nuova-password/{hash}',
'sl' => '/sl/korisnik/novo-geslo/{hash}',
], name: 'user_type_password')]
public function newPassword($hash): Response
{
$user = $this->em->getRepository(User::class)->findOneBy(["newPassword" => $hash]);
if(!$user){
$this->addFlash("error", $this->strings->getTranslationString("new_password_request_flash_error"));
return $this->redirectToRoute("user_login");
}
$form = $this->createForm(NewPasswordType::class, $user);
$form->handleRequest($this->requestStack->getCurrentRequest());
if ($form->isSubmitted() && $form->isValid()) {
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$user->getPassword()
);
$user->setPassword($hashedPassword);
$user->setNewPassword(NULL);
$this->em->persist($user);
$this->em->flush();
$this->addFlash("success", $this->strings->getTranslationString("password_saved_flash_success"));
return $this->redirectToRoute("user_login");
}
return $this->render("security/password.html.twig",[
"form" => $form->createView()
]);
}
#[Route([
'hr' => '/hr/uspjesna-registracija',
'en' => '/en/successful-registration',
'de' => '/de/erfolgreiche-registrierung',
'it' => '/it/registrazione-riuscita',
'sl' => '/sl/uspesna-registracija',
], name: 'register_success')]
public function success(): Response
{
return $this->render("security/register_success.html.twig");
}
/**
* @throws Exception
*/
#[Route([
'hr' => '/hr/odjava',
'en' => '/en/logout',
'de' => '/de/ausloggen',
'it' => '/it/disconnettersi',
'sl' => '/sl/odjava',
], name: 'logout')]
public function logout()
{
}
}