src/Controller/Admin/SecurityController.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Exception;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. /**
  18.  * Class SecurityController
  19.  * @package App\Controller\Admin
  20.  * @Route(path="/admin", name="admin_")
  21.  */
  22. class SecurityController extends AbstractController
  23. {
  24.     private EntityManagerInterface $em;
  25.     public function __construct(
  26.         EntityManagerInterface $em
  27.     ) {
  28.         $this->em $em;
  29.     }
  30.     /**
  31.      * @param AuthenticationUtils $authenticationUtils
  32.      * @return Response
  33.      * @Route("/login", name="login")
  34.      */
  35.     public function login(AuthenticationUtils $authenticationUtils): Response
  36.     {
  37.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  38.             return $this->redirectToRoute('admin_index');
  39.         }
  40.         // get the login error if there is one
  41.         $error $authenticationUtils->getLastAuthenticationError();
  42.         // last username entered by the user
  43.         $lastUsername $authenticationUtils->getLastUsername();
  44.         return $this->render('admin/security/login.html.twig', [
  45.             'last_username' => $lastUsername,
  46.             'error' => $error,
  47.         ]);
  48.     }
  49.     /**
  50.      * @return void
  51.      * @throws Exception
  52.      * @Route("/logout", name="logout")
  53.      */
  54.     public function logout(): void
  55.     {
  56.         throw new Exception('This should never be reached!');
  57.     }
  58.     /**
  59.      * @param Request $request
  60.      * @param MailerInterface $mailer
  61.      * @return RedirectResponse|Response
  62.      * @Route("/password-forget", name="password_forget")
  63.      * @throws TransportExceptionInterface
  64.      */
  65.     public function passwordForget(Request $requestMailerInterface $mailer): RedirectResponse|Response
  66.     {
  67.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  68.             return $this->redirectToRoute('admin_index');
  69.         }
  70.         $email $request->get('email');
  71.         $error "";
  72.         if ($request->get('submit_password_forget')) {
  73.             /** @var User $user */
  74.             $user $this->em->getRepository(User::class)->findOneBy([
  75.                 'email' => $email
  76.             ]);
  77.             if ($user) {
  78.                 $hash md5(uniqid('pf'));
  79.                 $user->setHash($hash);
  80.                 $message = (new TemplatedEmail())
  81.                     ->from(new Address($this->getParameter('MAILER_FROM'), $this->getParameter('MAILER_FROM_NAME')))
  82.                     ->to($user->getEmail())
  83.                     ->subject("Mot de passe oublié")
  84.                     ->htmlTemplate('email/user_admin/password_forget.html.twig')
  85.                     ->context([
  86.                         'user' => $user,
  87.                         'hash' => $hash,
  88.                     ]);
  89.                 $mailer->send($message);
  90.                 $this->em->flush();
  91.             }
  92.             $this->addFlash('success'"Un mail vient d'être envoyé à " $email);
  93.             return $this->redirectToRoute('admin_login');
  94.         }
  95.         return $this->render('admin/security/password_forget.html.twig', [
  96.             'email' => $email,
  97.             'error' => $error,
  98.             'body_class' => 'login-container',
  99.         ]);
  100.     }
  101.     /**
  102.      * @param string $hash
  103.      * @param Request $request
  104.      * @param UserPasswordEncoderInterface $passwordEncoder
  105.      * @return RedirectResponse|Response
  106.      * @Route("/password-reset/{hash}", name="password_reset")
  107.      */
  108.     public function passwordReset(string $hashRequest $requestUserPasswordEncoderInterface $passwordEncoder): RedirectResponse|Response
  109.     {
  110.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  111.             return $this->redirectToRoute('admin_index');
  112.         }
  113.         $error "";
  114.         /** @var User $user */
  115.         $user $this->em->getRepository(User::class)->findOneBy([
  116.             'hash' => $hash
  117.         ]);
  118.         if ($user && $request->get('submit_password_reset') && ($password $request->get('password'))) {
  119.             if (strlen($password) > 5) {
  120.                 $password $passwordEncoder->encodePassword($user$password);
  121.                 $user->setPassword($password);
  122.                 $user->setHash(null);
  123.                 $this->em->persist($user);
  124.                 $this->em->flush();
  125.                 return $this->redirectToRoute('admin_login');
  126.             } else {
  127.                 $error "Veuillez saisir un mot de passe d'au moins 6 caractères";
  128.             }
  129.         }
  130.         return $this->render('admin/security/password_reset.html.twig', [
  131.             'error' => $error,
  132.             'body_class' => 'login-container',
  133.         ]);
  134.     }
  135. }