src/Controller/SsoSecurityController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use App\Repository\SsoPersonaRepository;
  9. use App\Form\OlvidoPassType;
  10. use App\Entity\SsoPersona;
  11. use App\Service\PortalConsejoService;
  12. use App\Entity\Auditoria\Registro;
  13. use App\Service\AuditoriaService;
  14. class SsoSecurityController extends AbstractController
  15. {
  16.     #[Route('/login'name'app_login'methods: ['GET''POST'])]
  17.     public function login(AuthenticationUtils $authenticationUtils): Response
  18.     {
  19.         // get the login error if there is one
  20.         $error $authenticationUtils->getLastAuthenticationError();
  21.         // last username entered by the user
  22.         $lastUsername $authenticationUtils->getLastUsername();
  23.         $form $this->createForm(OlvidoPassType::class, null, [
  24.             'action' => $this->generateUrl('app_forgot'),
  25.             // // enable/disable CSRF protection for this form
  26.             // 'csrf_protection' => true,
  27.             // // the name of the hidden HTML field that stores the token
  28.             // 'csrf_field_name' => '_token_forgot',
  29.         ]);
  30.         return $this->renderForm('security/login.html.twig', [
  31.             'last_username' => $lastUsername
  32.             'error' => $error,
  33.             'formForgot' => $form,
  34.         ]);
  35.     }
  36.     #[Route('/login/aspirante'name'app_login_aspirante'methods: ['GET''POST'])]
  37.     public function loginAspirante(AuthenticationUtils $authenticationUtils): Response
  38.     {
  39.         // get the login error if there is one
  40.         $error $authenticationUtils->getLastAuthenticationError();
  41.         // last username entered by the user
  42.         $lastUsername $authenticationUtils->getLastUsername();
  43.         $form $this->createForm(OlvidoPassType::class, null, [
  44.             'action' => $this->generateUrl('app_forgot'),
  45.             // // enable/disable CSRF protection for this form
  46.             // 'csrf_protection' => true,
  47.             // // the name of the hidden HTML field that stores the token
  48.             // 'csrf_field_name' => '_token_forgot',
  49.         ]);
  50.         return $this->renderForm('security/login.html.twig', [
  51.             'last_username' => $lastUsername
  52.             'error' => $error,
  53.             'formForgot' => $form,
  54.             'aspirante' => true,
  55.         ]);
  56.     }
  57.     #[Route('/logout'name'app_logout')]
  58.     public function logout()
  59.     {
  60.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  61.     }
  62.     #[Route('/portal'name'app_portal')]
  63.     public function portal()
  64.     {
  65.         if (in_array('ROLE_CONSEJO'$this->getUser()->getRoles()))
  66.             return $this->redirectToRoute('portal_consejo', [], Response::HTTP_SEE_OTHER);
  67.             // return new RedirectResponse($this->urlGenerator->generate('portal_consejo'));
  68.         elseif (in_array('ROLE_ASPIRANTE'$this->getUser()->getRoles()))
  69.             return $this->redirectToRoute('portal_aspirante', [], Response::HTTP_SEE_OTHER);
  70.             // return new RedirectResponse($this->urlGenerator->generate('portal_aspirante'));
  71.     }
  72.     #[Route('/forgot'name:'app_forgot'methods: ['POST'])]
  73.     public function forgot(
  74.         Request $request
  75.         SsoPersonaRepository $ssoPersonaRepository
  76.     ): Response
  77.     {
  78.         
  79.         $form $this->createForm(OlvidoPassType::class, null, [
  80.             'action' => $this->generateUrl('app_forgot'),
  81.             // // enable/disable CSRF protection for this form
  82.             // 'csrf_protection' => true,
  83.             // // the name of the hidden HTML field that stores the token
  84.             // 'csrf_field_name' => '_token_forgot',
  85.         ]);
  86.         
  87.         $form->handleRequest($request);
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             $persona $ssoPersonaRepository->findOneBy([
  90.                 'dni' => $form->get('dni')->getData()
  91.             ]);
  92.             if ($persona)
  93.                 return $this->renderForm('security/login.html.twig', [
  94.                     'last_username' => "",
  95.                     'error' => "",
  96.                     'persona' => $persona,
  97.                     'formForgot' => $form,
  98.                 ]);
  99.         } 
  100.         $request->getSession()->getFlashBag()->add(
  101.             'error'// notice / warning / error
  102.             'No se encuentra un usuario registrado con el DNI ingresado.'
  103.         );
  104.         return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  105.     }
  106.     #[Route('/forgot_ini/{id}'name'app_forgot_ini'methods: ['POST'])]
  107.     public function forgotIni(
  108.         Request $request
  109.         SsoPersona $persona
  110.         PortalConsejoService $portalService,
  111.         AuditoriaService $auditoriaService
  112.     ): Response
  113.     {
  114.         $submittedToken $request->request->get('ini_forgot_token');
  115.         if ($this->isCsrfTokenValid('iniciar-forgot'$submittedToken)) {
  116.             $user $persona->getUsuariosPortal()[0];
  117.             if ($portalService->procesarCorreo(
  118.                 $request
  119.                 $user
  120.                 $user->getPersona()->getEmailPersonal(),
  121.                 'RECUPERO'
  122.                 '[Portal del Consejo] Recupero de contraseƱa',
  123.                 'mail/recuperoPassword.html.twig'
  124.                 )) {
  125.                     $auditoriaService->auditarEvento(Registro::EVENTO_PASSWORD_OLVIDO$user);
  126.                     $request->getSession()->getFlashBag()->add(
  127.                         'notice'// notice / warning / error
  128.                         'Fue enviado un correo para continuar con el recupero de la contraseƱa.'
  129.                     );
  130.                 }
  131.         }
  132.         return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  133.     }
  134.     #[Route('/recupero/{mail}/{token}'name'app_recupero'methods: ['GET''POST'])]
  135.     public function recupero(
  136.         Request $request,
  137.         PortalConsejoService $portalService,
  138.         String $mail,
  139.         String $token
  140.     ): Response
  141.     {
  142.         $resultado $portalService->procesarRecuperoPassword($request$mail$token);
  143.         if ($resultado['login'])
  144.             return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  145.         else 
  146.             return $this->renderForm('security/forgotPass.html.twig', [
  147.                 'formPassword' => $resultado['form'],
  148.             ]);
  149.     }
  150. }