src/Controller/SecurityController.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\NewPassFormType;
  5. use App\Form\ResetPassFormType;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\Mime\Address;
  17. use Symfony\Bridge\Twig\Mime\TemplatedEmail as Email;
  18. class SecurityController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/login" , name="app_login")
  22.      */
  23.     public function login(AuthenticationUtils $authenticationUtils): Response
  24.     {
  25.         if ($this->getUser()) {
  26.             return $this->redirectToRoute('app_homepage');
  27.         }
  28.         // get the login error if there is one
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         // last username entered by the user
  31.         $lastUsername $authenticationUtils->getLastUsername();
  32.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  33.     }
  34.     /**
  35.      * @Route("/logout", name="app_logout")
  36.      */
  37.     public function logout()
  38.     {
  39.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  40.     }
  41.     /**
  42.      * @Route("/pass-reset/request", name="app_pass_reset_request")
  43.      */
  44.     public function passReset(Request $requestTranslatorInterface $translatorMailerInterface $mailer): Response
  45.     {
  46.         $form $this->createForm(ResetPassFormType::class);
  47.         $form->handleRequest($request);
  48.         if ($form->isSubmitted() && $form->isValid()) {
  49.             $email $form->get('email')->getData();
  50.             $entityManager $this->getDoctrine()->getManager();
  51.             $user $entityManager->getRepository(User::class)
  52.                 ->findOneBy(['email' => $email]);
  53.             if ($user instanceof User && $user->isEnabled()) {
  54.                 $email = (new Email())
  55.                     ->from(new Address($this->get('parameter_bag')->get('mailer_from'), $this->get('parameter_bag')->get('mailer_from_name')))
  56.                     ->to($user->getEmail())
  57.                     ->subject('Resetowanie hasła')
  58.                     // path of the Twig template to render
  59.                     ->htmlTemplate('emails/pass-reset.html.twig')
  60.                     // pass variables (name => value) to the template
  61.                     ->context([
  62.                         'link' => $this->get('router')->generate('app_pass_reset_new_pass', ['token' => urlencode($user->getPassword())], UrlGeneratorInterface::ABSOLUTE_URL)
  63.                     ]);
  64.                 $mailer->send($email);
  65.                 $this->addFlash(
  66.                     'success',
  67.                     'Wiadomość resetująca została wysłana.'
  68.                 );
  69.                 return new RedirectResponse($this->get('router')->generate('app_login'));
  70.             } else {
  71.                 $this->addFlash(
  72.                     'error',
  73.                     'Nie ma takiego użytkownika.'
  74.                 );
  75.             }
  76.         }
  77.         return $this->render('security/pass-reset.html.twig', [
  78.             'form' => $form->createView(),
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/pass-reset/new/{token}", name="app_pass_reset_new_pass", requirements={"token"=".+"})
  83.      */
  84.     public function newPass(Request $requestTranslatorInterface $translatorUserPasswordEncoderInterface $passwordEncoder$token)
  85.     {
  86.         $entityManager $this->getDoctrine()->getManager();
  87.         $user $entityManager->getRepository(User::class)
  88.             ->findOneBy(['password' => urldecode($token)]);
  89.         if ($user instanceof User && $user->isEnabled()) {
  90.             $form $this->createForm(NewPassFormType::class, $user);
  91.             $form->handleRequest($request);
  92.             if ($form->isSubmitted() && $form->isValid()) {
  93.                 $user->setPassword(
  94.                     $passwordEncoder->encodePassword(
  95.                         $user,
  96.                         $form->get('newPassword')->getData()
  97.                     )
  98.                 );
  99.                 $entityManager->flush();
  100.                 $this->addFlash(
  101.                     'success',
  102.                     'Hasło zostało zmienione. Teraz możesz się zalogować.'
  103.                 );
  104.                 return new RedirectResponse($this->get('router')->generate('app_login'));
  105.             }
  106.             return $this->render('security/new-pass.html.twig', [
  107.                 'form' => $form->createView(),
  108.             ]);
  109.         } else {
  110.             $this->addFlash(
  111.                 'error',
  112.                 $translator->trans('passreset.new-pass.wrong-token', [], 'security')
  113.             );
  114.             return new RedirectResponse($this->get('router')->generate('app_login'));
  115.         }
  116.     }
  117. }