<?php
namespace App\Controller;
use App\Entity\User;
use App\Service\Notifications;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Twig\Environment;
class SecurityController extends AbstractController
{
private $notifications;
private $twig;
public function __construct(Notifications $notifications, Environment $twig)
{
$this->notifications = $notifications;
$this->twig = $twig;
}
/**
* @Route("/login", name="app_login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
{
// get the login error iff there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/index.html.twig',
['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout()
{
// throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("public/restore/password", name="app_restore_password")
*/
public function restore( Request $request, UserPasswordHasherInterface $passwordEncoder ) {
$em = $this->getDoctrine()->getManager();
$email = $request->get('emailR');
$userD = $em->getRepository(User::class)->findOneBy(['email' => $email]);
if ( ! empty($userD)) {
$passNew = rand(10000, 99999999);
$this->changePass($userD->getId(), $passNew, $passwordEncoder);
$this->addFlash(
'success',
'Te enviamos un correo con tu nueva contraseña.'
);
$subject = 'Nueva Contraseña';
$message = "Estimado Usuario, hemos restablecido tu contraseña para el acceso a nuestra plataforma. <br> <br>
Tu nueva contraseña es: <strong>" . $passNew . " </strong> <br><br>
Dentro del sistema podrás visualizar el estado de las licitaciones. Solo debes ingresar con tu correo y la nueva contraseña. <br><br> ¡Te esperamos! <br> Saludos Cordiales";
$htmlContents = $this->twig->render('security/email.html.twig', [
'title' => $subject,
'messages' => $message,
]);
$this->notifications->newNotificationSystem(
$email,
$subject,
$htmlContents
);
} else {
$this->addFlash(
'error',
'El correo ingresado no esta disponible en la plataforma'
);
}
return $this->redirectToRoute('app_login');
}
function changePass($id, $pass, $passwordEncoder)
{
$em = $this->getDoctrine()->getManager();
$userD = $em->getRepository(User::class)->find($id);
$userD->setPassword($passwordEncoder->hashPassword($userD, $pass));
$em->flush();
return true;
}
}