<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\SsoPersonaRepository;
use App\Form\OlvidoPassType;
use App\Entity\SsoPersona;
use App\Service\PortalConsejoService;
use App\Entity\Auditoria\Registro;
use App\Service\AuditoriaService;
class SsoSecurityController extends AbstractController
{
#[Route('/login', name: 'app_login', methods: ['GET', 'POST'])]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$form = $this->createForm(OlvidoPassType::class, null, [
'action' => $this->generateUrl('app_forgot'),
// // enable/disable CSRF protection for this form
// 'csrf_protection' => true,
// // the name of the hidden HTML field that stores the token
// 'csrf_field_name' => '_token_forgot',
]);
return $this->renderForm('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'formForgot' => $form,
]);
}
#[Route('/login/aspirante', name: 'app_login_aspirante', methods: ['GET', 'POST'])]
public function loginAspirante(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$form = $this->createForm(OlvidoPassType::class, null, [
'action' => $this->generateUrl('app_forgot'),
// // enable/disable CSRF protection for this form
// 'csrf_protection' => true,
// // the name of the hidden HTML field that stores the token
// 'csrf_field_name' => '_token_forgot',
]);
return $this->renderForm('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'formForgot' => $form,
'aspirante' => true,
]);
}
#[Route('/logout', name: 'app_logout')]
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route('/portal', name: 'app_portal')]
public function portal()
{
if (in_array('ROLE_CONSEJO', $this->getUser()->getRoles()))
return $this->redirectToRoute('portal_consejo', [], Response::HTTP_SEE_OTHER);
// return new RedirectResponse($this->urlGenerator->generate('portal_consejo'));
elseif (in_array('ROLE_ASPIRANTE', $this->getUser()->getRoles()))
return $this->redirectToRoute('portal_aspirante', [], Response::HTTP_SEE_OTHER);
// return new RedirectResponse($this->urlGenerator->generate('portal_aspirante'));
}
#[Route('/forgot', name:'app_forgot', methods: ['POST'])]
public function forgot(
Request $request,
SsoPersonaRepository $ssoPersonaRepository
): Response
{
$form = $this->createForm(OlvidoPassType::class, null, [
'action' => $this->generateUrl('app_forgot'),
// // enable/disable CSRF protection for this form
// 'csrf_protection' => true,
// // the name of the hidden HTML field that stores the token
// 'csrf_field_name' => '_token_forgot',
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$persona = $ssoPersonaRepository->findOneBy([
'dni' => $form->get('dni')->getData()
]);
if ($persona)
return $this->renderForm('security/login.html.twig', [
'last_username' => "",
'error' => "",
'persona' => $persona,
'formForgot' => $form,
]);
}
$request->getSession()->getFlashBag()->add(
'error', // notice / warning / error
'No se encuentra un usuario registrado con el DNI ingresado.'
);
return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
}
#[Route('/forgot_ini/{id}', name: 'app_forgot_ini', methods: ['POST'])]
public function forgotIni(
Request $request,
SsoPersona $persona,
PortalConsejoService $portalService,
AuditoriaService $auditoriaService
): Response
{
$submittedToken = $request->request->get('ini_forgot_token');
if ($this->isCsrfTokenValid('iniciar-forgot', $submittedToken)) {
$user = $persona->getUsuariosPortal()[0];
if ($portalService->procesarCorreo(
$request,
$user,
$user->getPersona()->getEmailPersonal(),
'RECUPERO',
'[Portal del Consejo] Recupero de contraseƱa',
'mail/recuperoPassword.html.twig'
)) {
$auditoriaService->auditarEvento(Registro::EVENTO_PASSWORD_OLVIDO, $user);
$request->getSession()->getFlashBag()->add(
'notice', // notice / warning / error
'Fue enviado un correo para continuar con el recupero de la contraseƱa.'
);
}
}
return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
}
#[Route('/recupero/{mail}/{token}', name: 'app_recupero', methods: ['GET', 'POST'])]
public function recupero(
Request $request,
PortalConsejoService $portalService,
String $mail,
String $token
): Response
{
$resultado = $portalService->procesarRecuperoPassword($request, $mail, $token);
if ($resultado['login'])
return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
else
return $this->renderForm('security/forgotPass.html.twig', [
'formPassword' => $resultado['form'],
]);
}
}