src/Controller/SecurityController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use SendinBlue\Client\Model\SendEmail;
  12. use DateTime;
  13. use SendinBlue\Client\Api\SMTPApi;
  14. class SecurityController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/", name="app_login")
  18.      */
  19.     public function login(AuthenticationUtils $authenticationUtils): Response
  20.     {
  21.         // if ($this->getUser()) {
  22.         //     return $this->redirectToRoute('target_path');
  23.         // }
  24.         // get the login error if there is one
  25.         $error $authenticationUtils->getLastAuthenticationError();
  26.         // last username entered by the user
  27.         $lastUsername $authenticationUtils->getLastUsername();
  28. //        if($lastUsername){
  29. //            return $this->redirectToRoute("app_coupons");
  30. //        }
  31.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  32.     }
  33.     /**
  34.      * @Route("/logout", name="app_logout")
  35.      */
  36.     public function logout()
  37.     {
  38.         throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
  39.     }
  40.     /**
  41.      * @Route("/forgot", name="forgot_password")
  42.      */
  43.     public function forgotPassword(Request $requestRouterInterface $router) {
  44.         $email $request->get('email');
  45.         $manager $this->getDoctrine()->getManager();
  46.         $user $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $email]);
  47.         if (!$email) {
  48.             return $this->render'security/forgot-pass.html.twig');
  49.         }
  50.         if (!$user) {
  51.             $this->addFlash('danger''Email could not be found.');
  52.             return $this->render'security/forgot-pass.html.twig');
  53.         }
  54.         $token md5(random_bytes(20));
  55.         $user->setToken($token);
  56.         $date = new DateTime('+24 hours');
  57.         $user->setTokenExpire($date->getTimestamp());
  58.         $manager->flush();
  59.         $url $router->generate('reset_password', [
  60.             'token' => $token
  61.         ], RouterInterface::ABSOLUTE_URL);
  62.         $sendEmail = new SendEmail();
  63.         $sendEmail->setEmailTo([$email])
  64.             ->setAttributes(["RESET_LINK" => $url]);
  65.         $api_instance = new SMTPApi();
  66.         $api_instance->getConfig()->setApiKey("api-key","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");
  67.         $api_instance->sendTemplate($_ENV["TEMPLATE_RESET_PASSWORD"], $sendEmail);
  68.         $this->addFlash('success''Email sent.');
  69.         return $this->render'security/forgot-pass.html.twig' );
  70.     }
  71.     /**
  72.      * @Route("/reset/{token}", name="reset_password")
  73.      */
  74.     public function resetPassword(Request $request$tokenUserPasswordEncoderInterface $passwordEncoder) {
  75.         $user $this->getDoctrine()->getRepository(User::class)->findOneByToken($token);
  76.         if (!$user) {
  77.             $this->addFlash('danger''Invalid token');
  78.             return $this->redirectToRoute('app_coupons');
  79.         }
  80.         $manager $this->getDoctrine()->getManager();
  81.         $newPassword $request->get('new_password');
  82.         $repeatPassword $request->get('repeat_password');
  83.         if ($newPassword) {
  84.             if ($newPassword != $repeatPassword) {
  85.                 $this->addFlash('danger''Les mots de passe ne sont pas identiques');
  86.                 return $this->render'security/reset-pass.html.twig', [ 'email' => $user->getEmail()] );
  87.             }
  88.             if (strlen($newPassword) < 8) {
  89.                 $this->addFlash('danger''Password must have at least 8 characters');
  90.                 return $this->render'security/reset-pass.html.twig', [ 'email' => $user->getEmail()] );
  91.             }
  92.             $user->setPassword($passwordEncoder->encodePassword($user$newPassword));
  93.             $user->setToken(NULL);
  94.             $user->setTokenExpire(NULL);
  95.             $manager->flush();
  96.             $this->addFlash('success''Password reseted');
  97.             return $this->redirectToRoute('app_login');
  98.         }
  99.         return $this->render'security/reset-pass.html.twig', [ 'email' => $user->getEmail() ] );
  100.     }
  101. }