<?php
namespace App\Controller;
use App\Entity\Laboratory;
use App\Entity\User;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
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;
class SecurityController extends AbstractController
{
private $passwordHasher;
private $entityManager;
public function __construct(UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager)
{
$this->passwordHasher = $passwordHasher;
$this->entityManager = $entityManager;
}
/**
* @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("/restorePass", name="app_restore")
*/
public function restore(
Request $request,
ManagerRegistry $doctrine
): Response {
$em = $doctrine->getManager();
$userName = $request->get('userName');
$userD = $em->getRepository(User::class)->findOneBy(['userName' => $userName]);
if ( ! empty($userD)) {
$passNew = rand(10000, 99999);
$this->changePass($userD->getId(), $passNew);
/*
$this->addFlash(
'success',
'Te enviamos un correo con tu nueva contraseña.'
);
$subject = 'Nueva Contraseña';
$message = "Estimado Beneficiario, agradecemos que seas parte de FPYME Ñuble y para seguir conectados, te 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 proceso de avance en tu Transformación Digital. Solo debes ingresar con tu correo y la nueva contraseña. <br><br> ¡Te esperamos! <br> Saludos Cordiales";
$htmlContents = $twig->render('security/email.html.twig', [
'title' => $subject,
'messages' => $message,
]);
$notif->newNotificationSystem(
$email,
$subject,
$htmlContents
);
*/
} else {
$this->addFlash(
'error',
'El usuario ingresado no esta disponible en la plataforma'
);
}
return $this->redirectToRoute('app_login');
}
function changePass($id, $pass)
{
$em = $this->doctrine->getManager();
$userD = $em->getRepository(User::class)->find($id);
$userD->setPassword($this->passwordHasher->encodePassword(
$userD,
$pass
));
$em->flush();
return true;
}
}