src/Controller/SecurityController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ResetPasswordType;
  4. use App\Form\ResetType;
  5. use App\Service\Mail;
  6. use App\Service\MailFailedException;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Psr\Log\LoggerInterface;
  9. use App\Entity\MailOutBox;
  10. use App\Entity\User;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. class SecurityController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/login", name="login")
  24.      */
  25.     public function loginAction(Request $requestAuthenticationUtils $authUtils): Response
  26.     {
  27.         // get the login error if there is one
  28.         $error $authUtils->getLastAuthenticationError();
  29.         // last username entered by the user
  30.         $lastUsername $authUtils->getLastUsername();
  31.         return $this->render('security/login.html.twig', array(
  32.             'last_username' => $lastUsername,
  33.             'error' => $error,
  34.         ));
  35.     }
  36.     /**
  37.      * @Route("/resetPassword", name="resetPassword")
  38.      * @param Request $request
  39.      * @param AuthenticationUtils $authUtils
  40.      * @param TranslatorInterface $translator
  41.      * @param LoggerInterface $logger
  42.      * @return RedirectResponse|Response
  43.      */
  44.     public function resetPasswordEmailAction(
  45.         Request $request,
  46.         AuthenticationUtils $authUtils,
  47.         TranslatorInterface $translator,
  48.         LoggerInterface $logger,
  49.         ValidatorInterface $validator)
  50.     {
  51.         $em $this->getDoctrine()->getManager();
  52.         $form $this->createFormBuilder()
  53.             ->add('mail'EmailType::class,[
  54.                 'label' => false,
  55.                 'attr' => [
  56.                     'placeholder' => 'user.email'
  57.                 ]
  58.             ])
  59.             ->getForm();
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted())
  62.         {
  63.             try {
  64.                 $user $em->getRepository(User::class)->findOneBy(['email' => $form->getData()['mail']]);
  65.                 if ($user != null)
  66.                 {
  67.                     if ($user->getUserStatus()->getId() == 2)
  68.                     {
  69.                         $user->setToken(md5($user->getId().$user->getFirstnamelastname().time()));
  70.                         $em->persist($user);
  71.                         $em->flush();
  72.                         $this->addFlash('success'$translator->trans('login.resetPasswordSuccess'));
  73.                     } else {
  74.                         $this->addFlash('error'$translator->trans('login.accountNotActivated'));
  75.                     }
  76.                     $template $em->getRepository(\App\Entity\MailTemplates::class)->findOneBy(['id' => 2]);
  77.                     $mailOut = new MailOutBox();
  78.                     $mailOut->setRecipient($user->getEmail());
  79.                     $mailOut->setSubject($template->getSubject());
  80.                     $mailOut->setBody($template->getBody());
  81.                     $em->persist($mailOut);
  82.                     $em->flush();
  83.                     return $this->redirectToRoute('homepage');
  84.                 } else {
  85.                     $this->addFlash('success'$translator->trans('login.noAccount'));
  86.                     return $this->redirectToRoute('homepage');
  87.                 }
  88.             } catch (\Exception $exception) {
  89.                 $this->addFlash('error'$translator->trans('supplier.create_error'.$exception->getMessage()));
  90.                 $logger->error('Error while create Admin Supplier', ['e' => $exception]);
  91.             }
  92.         }
  93.         return $this->render('security/resetPassword.html.twig', array(
  94.             'form' => $form->createView(),
  95.         ));
  96.     }
  97. }