<?php
namespace App\Controller;
use App\Libs\Constantes;
use App\Repository\UserRepository;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\ORM\EntityManagerInterface;
use SendGrid\Mail\HtmlContent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
class SecurityController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $manager;
public function __construct(EntityManagerInterface $manager)
{
$this->manager = $manager;
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_account');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
//dump($lastUsername);exit();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername, 'error' => $error,
'promo_code' => null,
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/login_check", name="login_check")
*/
public function check()
{
throw new \LogicException('This code should never be reached');
}
/**
* @Route("/loginlink", name="loginlink")
*/
public function requestLoginLink(LoginLinkHandlerInterface $loginLinkHandler, UserRepository $userRepository, Request $request)
{
// check if login form is submitted
if ($request->isMethod('POST')) {
// load the user in some way (e.g. using the form input)
$form = $request->request->get('form');
$user = $userRepository->findOneBy(['email' => $_POST['email']]);
if($user){
$user->setResetPin(1);
$this->manager->persist($user);
$this->manager->flush();
}
$loginLinkDetails = $loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
$msg = <<<YYY
<html>
<p>Presione <a href="{$loginLink}">aquí</a></p>
</html>
YYY;
if($user->getEmail()){
$from_email = new \SendGrid\Mail\From(Constantes::SENDER); // Sender's email address
$to_email = new \SendGrid\Mail\To($user->getEmail()); // Recipient's email address
$subject = 'Nueva Contraseña'; // Email subject
$content = new HtmlContent($msg);
$email = new \SendGrid\Mail\Mail($from_email, $to_email, $subject, $content); // Create email object
$email->setClickTracking(false);
$sendgrid = new \SendGrid(Constantes::SENDGRID_API_KEY); // Create Sendgrid object
try {
$response = $sendgrid->send($email); // Send email
echo "Email sent successfully. Response: " . $response->statusCode(); // Print success message and response status code
} catch (Exception $e) {
echo "Error sending email: " . $e->getMessage(); // Print error message
}
}
$this->addFlash('success', 'Si el email existe, debe haber recibido su contraseña.');
return $this->redirectToRoute('app_login', ['last_username'=>'']);
}
return $this->render('home/forgot.html.twig');
}
}