src/Controller/SecurityController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Libs\Constantes;
  4. use App\Repository\UserRepository;
  5. use Doctrine\DBAL\Driver\Exception;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use SendGrid\Mail\HtmlContent;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  14. class SecurityController extends AbstractController
  15. {
  16. /**
  17. * @var EntityManagerInterface
  18. */
  19. private $manager;
  20. public function __construct(EntityManagerInterface $manager)
  21. {
  22. $this->manager = $manager;
  23. }
  24. /**
  25. * @Route("/login", name="app_login")
  26. */
  27. public function login(AuthenticationUtils $authenticationUtils): Response
  28. {
  29. if ($this->getUser()) {
  30. return $this->redirectToRoute('app_account');
  31. }
  32. // get the login error if there is one
  33. $error = $authenticationUtils->getLastAuthenticationError();
  34. // last username entered by the user
  35. $lastUsername = $authenticationUtils->getLastUsername();
  36. //dump($lastUsername);exit();
  37. return $this->render('security/login.html.twig', [
  38. 'last_username' => $lastUsername, 'error' => $error,
  39. 'promo_code' => null,
  40. ]);
  41. }
  42. /**
  43. * @Route("/logout", name="app_logout")
  44. */
  45. public function logout(): void
  46. {
  47. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  48. }
  49. /**
  50. * @Route("/login_check", name="login_check")
  51. */
  52. public function check()
  53. {
  54. throw new \LogicException('This code should never be reached');
  55. }
  56. /**
  57. * @Route("/loginlink", name="loginlink")
  58. */
  59. public function requestLoginLink(LoginLinkHandlerInterface $loginLinkHandler, UserRepository $userRepository, Request $request)
  60. {
  61. // check if login form is submitted
  62. if ($request->isMethod('POST')) {
  63. // load the user in some way (e.g. using the form input)
  64. $form = $request->request->get('form');
  65. $user = $userRepository->findOneBy(['email' => $_POST['email']]);
  66. if($user){
  67. $user->setResetPin(1);
  68. $this->manager->persist($user);
  69. $this->manager->flush();
  70. }
  71. $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
  72. $loginLink = $loginLinkDetails->getUrl();
  73. $msg = <<<YYY
  74. <html>
  75. <p>Presione <a href="{$loginLink}">aquí</a></p>
  76. </html>
  77. YYY;
  78. if($user->getEmail()){
  79. $from_email = new \SendGrid\Mail\From(Constantes::SENDER); // Sender's email address
  80. $to_email = new \SendGrid\Mail\To($user->getEmail()); // Recipient's email address
  81. $subject = 'Nueva Contraseña'; // Email subject
  82. $content = new HtmlContent($msg);
  83. $email = new \SendGrid\Mail\Mail($from_email, $to_email, $subject, $content); // Create email object
  84. $email->setClickTracking(false);
  85. $sendgrid = new \SendGrid(Constantes::SENDGRID_API_KEY); // Create Sendgrid object
  86. try {
  87. $response = $sendgrid->send($email); // Send email
  88. echo "Email sent successfully. Response: " . $response->statusCode(); // Print success message and response status code
  89. } catch (Exception $e) {
  90. echo "Error sending email: " . $e->getMessage(); // Print error message
  91. }
  92. }
  93. $this->addFlash('success', 'Si el email existe, debe haber recibido su contraseña.');
  94. return $this->redirectToRoute('app_login', ['last_username'=>'']);
  95. }
  96. return $this->render('home/forgot.html.twig');
  97. }
  98. }