src/Controller/SecurityController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\User\ChangePasswordType;
  4. use App\Form\User\ForgotPasswordType;
  5. use App\Repository\Tracking\TimeTrackRepository;
  6. use App\Repository\UserRepository;
  7. use App\Service\MailService;
  8. use App\Service\TrackingService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class SecurityController extends AbstractController
  18. {
  19.     private $entityManager;
  20.     private $userRepository;
  21.     private $mailService;
  22.     private $translator;
  23.     private $trackingService;
  24.     public function __construct(EntityManagerInterface $entityManager,
  25.                                 UserRepository $userRepository,
  26.                                 MailService $mailService,
  27.                                 TranslatorInterface $translator,
  28.                                 TrackingService $trackingService)
  29.     {
  30.         $this->entityManager $entityManager;
  31.         $this->userRepository $userRepository;
  32.         $this->mailService $mailService;
  33.         $this->translator $translator;
  34.         $this->trackingService $trackingService;
  35.     }
  36.     /**
  37.      * @Route("/prihlaseni/", name="app_login")
  38.      * @param AuthenticationUtils $authenticationUtils
  39.      * @return Response
  40.      */
  41.     public function login(AuthenticationUtils $authenticationUtils): Response
  42.     {
  43.          if ($this->getUser()) {
  44.              return $this->redirectToRoute('order_index');
  45.          }
  46.         // get the login error if there is one
  47.         $error $authenticationUtils->getLastAuthenticationError();
  48.         // last username entered by the user
  49.         $lastUsername $authenticationUtils->getLastUsername();
  50.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  51.     }
  52.     /**
  53.      * @Route("/logout/", name="app_logout")
  54.      */
  55.     public function logout()
  56.     {
  57.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  58.     }
  59.     /**
  60.      * @Route("/odhlasit/", name="app_before_logout")
  61.      */
  62.     public function beforeLogout()
  63.     {
  64.         $this->trackingService->stopActiveTrack($this->getUser());
  65.         $this->entityManager->flush();
  66.         return $this->redirectToRoute('app_logout');
  67.     }
  68.     /**
  69.      * @Route("/zapomenute-heslo/", name="app_forgotten_password")
  70.      * @param Request $request
  71.      * @param UserPasswordEncoderInterface $passwordEncoder
  72.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  73.      */
  74.     public function forgotPassword(Request $request)
  75.     {
  76.         $form $this->createForm(ForgotPasswordType::class);
  77.         $form->handleRequest($request);
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             $email $form->get('email')->getData();
  80.             $user $this->userRepository->findOneBy(['email' => $email]);
  81.             if (!$user) {
  82.                 $this->addFlash('danger'$this->translator->trans('security.email_not_found'));
  83.                 return $this->redirectToRoute('app_forgotten_password');
  84.             }
  85.             $hash bin2hex(random_bytes(32));
  86.             $user->setResetPasswordHash($hash)
  87.                 ->setRequestPasswordDate(new \DateTime());
  88.             $this->entityManager->persist($user);
  89.             $this->entityManager->flush();
  90.             $this->mailService->sendPasswordResetLink($user->getEmail(), $hash);
  91.             $this->addFlash('success'$this->translator->trans('security.reset_link_sent'));
  92.             return $this->redirectToRoute('app_forgotten_password');
  93.         }
  94.         return $this->render('security/forgot_password.html.twig', [
  95.             'form' => $form->createView(),
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/obnovit-heslo/", name="app_reset_password")
  100.      * @param Request $request
  101.      * @param UserPasswordEncoderInterface $passwordEncoder
  102.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  103.      */
  104.     public function resetPassword(Request $requestUserPasswordEncoderInterface $passwordEncoder)
  105.     {
  106.         $errorMessage '';
  107.         $hasError false;
  108.         $hash $request->query->get('hash');
  109.         $user $this->userRepository->findOneBy(['resetPasswordHash' => $request->query->get('hash')]);
  110.         if (!is_null($hash) && $user) {
  111.             $yesterday = new \DateTime();
  112.             $yesterday $yesterday->modify('-1days');
  113.             if ($user->getRequestPasswordDate() > $yesterday) {
  114.                 $form $this->createForm(ChangePasswordType::class, null, [
  115.                     'showCurrentPassword' => false,
  116.                 ]);
  117.                 $form->handleRequest($request);
  118.                 if ($form->isSubmitted()) {
  119.                     if ($form->isValid()) {
  120.                         $encodedPassword $passwordEncoder->encodePassword($user$form->get('password')->getData());
  121.                         $user->setPassword($encodedPassword);
  122.                         $this->entityManager->persist($user);
  123.                         $this->entityManager->flush();
  124.                         $this->addFlash('success'$this->translator->trans('security.password_changed'));
  125.                         return $this->redirectToRoute('app_login');
  126.                     }
  127.                 }
  128.             } else {
  129.                 $errorMessage $this->translator->trans('security.link_expired');
  130.                 $hasError true;
  131.             }
  132.         } else {
  133.             $errorMessage $this->translator->trans('security.wrong_link');
  134.             $hasError true;
  135.         }
  136.         return $this->render('security/reset_password.html.twig', [
  137.             'form' => isset($form) ? $form->createView() : null,
  138.             'errorMessage' => $errorMessage,
  139.             'hasError' => $hasError,
  140.         ]);
  141.     }
  142. }