src/Controller/Seguridades/AuthController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Seguridades;
  3. use App\Controller\InformacionGeneralController;
  4. use App\Entity\Seguridades\{IntentosAccesoUsuario};
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Symfony\Component\HttpFoundation\{Request};
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Service\{AdminServiceCacheServiceEmailServiceLoginService};
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Exception;
  12. use Symfony\Component\Form\Extension\Core\Type\{SubmitTypeTextType};
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. class AuthController extends AbstractController {
  15.     /* Template login */
  16.         public function index(AuthenticationUtils $authenticationUtilsEntityManagerInterface $emCacheService $cacheLoginService $login) {
  17.             $informacionGeneral = new InformacionGeneralController($em);
  18.             $message '';
  19.             $error $authenticationUtils->getLastAuthenticationError();
  20.             $lastUsername $authenticationUtils->getLastUsername();
  21.             if ($error) :
  22.                 $user $login->checkUser($lastUsername$em);
  23.                 if ($user) :
  24.                     $cache->add('lastUsername'$lastUsername);
  25.                     if ($user['active']) :
  26.                         if (!$user['bloqueado']) :
  27.                             $message $login->checkAttemps($user$em);
  28.                             if (!$message) :
  29.                                 return $this->redirectToRoute('bloqueado');
  30.                             endif;
  31.                         else :
  32.                             return $this->redirectToRoute('usuarioBloqueado');
  33.                         endif;
  34.                     else :
  35.                         $cache->add('inactivo'true);
  36.                         return $this->redirectToRoute('inactivo');
  37.                     endif;
  38.                 else :
  39.                     $message 'Usuario/Contraseña Incorrectos';
  40.                 endif;
  41.             endif;
  42.             return $this->render('Seguridades/auth/login/login.html.twig', [
  43.                 'error' => $error,
  44.                 'last_username' => $lastUsername,
  45.                 'message' => $message,
  46.                 'informacionGeneral' => $informacionGeneral->getData()
  47.             ]);
  48.         }
  49.         public function accessControl(UserInterface $userEntityManagerInterface $emCacheService $cacheAdminService $adminServiceLoginService $loginSer) {
  50.             $intentosAcceso $em->getRepository(IntentosAcceso::class)->findOneBy(['idUsuario' => $user->getId()]);
  51.             if ($intentosAcceso) : $em->remove($intentosAcceso);
  52.                 $em->flush();
  53.             endif;
  54.             if (!$user->getActivo()) :
  55.                 $cache->add('lastUsername'$user->getUsername());
  56.                 $cache->add('inactivo'true);
  57.                 return $this->redirectToRoute('inactivo');
  58.             endif;
  59.             if ($user->getBloqueado()) :
  60.                 $cache->add('lastUsername'$user->getUsername());
  61.                 return $this->redirectToRoute('bloqueado');
  62.             endif;
  63.             if ($adminService->checkPassChange($user)) : return $this->redirectToRoute('change_pass');
  64.             endif;
  65.             $loginSer->addAccess($user'home'$em);
  66.             return $this->redirectToRoute('home');
  67.         }
  68.     /* Template recuperacion contraseña */
  69.         public function recuperacion(Request $requestLoginService $loginEntityManagerInterface $emEmailService $emailService) {
  70.             $informacionGeneral = new InformacionGeneralController($em);
  71.             $form $this->createRecForm();
  72.             $form->handleRequest($request);
  73.             $user $message '';
  74.             if ($form->isSubmitted()) :
  75.                 $usuario $form->get('usuario')->getData();
  76.                 $user $login->getUserLog($usuario$em);
  77.                 if ($user && $user->getActivo()) :
  78.                     $datosUser $this->getDataUser($user);
  79.                     $pass $login->setPasswordUser($user$em);
  80.                     $emailService->SendEmailRecuperarContrasenia($datosUser$pass$em);
  81.                     return $this->render('Seguridades/auth/recuperar_password.html.twig',[
  82.                         'informacionGeneral' => $informacionGeneral->getData()
  83.                     ]);
  84.                 else :
  85.                     $message "El Usuario no existe o se encuentra Inactivo, porfavor contactarse con el Administrador";
  86.                 endif;
  87.             endif;
  88.             return $this->render('Seguridades/auth/recuperar.html.twig', [
  89.                 'informacionGeneral' => $informacionGeneral->getData(),
  90.                 'form' => $form->createView(),
  91.                 'user' => $user,
  92.                 'message' => $message
  93.             ]);
  94.         }
  95.         private function createRecForm() {
  96.             return $this->createFormBuilder($user = new Usuario)
  97.                 ->add('usuario'TextType::class, array(
  98.                     'attr' => [
  99.                         'class' => 'form-control'
  100.                     ],
  101.                     'mapped' => false
  102.                 ))
  103.                 ->add('submit'SubmitType::class, array(
  104.                     'attr' => [
  105.                         'class' => 'btn btn-primary'
  106.                     ],
  107.                     'label' => 'Recuperar Cuenta'
  108.                 ))
  109.                 ->getForm();
  110.         }
  111.     /* Template bloqueado */
  112.         public function bloqueado(LoginService $loginCacheService $cacheEntityManagerInterface $emEmailService $emailService) {
  113.             $user $cache->get('lastUsername');
  114.             $informacionGeneral = new InformacionGeneralController($em);
  115.             if ($user) :
  116.                 $cache->delete('lastUsername');
  117.                 $user $em->getRepository(Usuario::class)->findOneBy(['usuario' => $user]);
  118.                 $dataPerson $this->getDataUser($user);
  119.                 $emailService->sendEmailBloqueo($dataPerson$em);
  120.                 $login->setStateUser($user$em);
  121.                 $correo $user->getIdPersona()->getMailPersonal();
  122.                 $correo substr($correo05);
  123.                 $correo .= 'XXXXXXX';
  124.                 $this->removeAttempsUser($em$user);
  125.                 return $this->render('Seguridades/auth/comprobar.html.twig', [
  126.                     'informacionGeneral' => $informacionGeneral->getData(),
  127.                     'nombres' => $dataPerson['nombres'],
  128.                     'apellidos' => $dataPerson['apellidos'],
  129.                     'correo' => $correo
  130.                 ]);
  131.             else :
  132.                 return $this->redirectToRoute('login');
  133.             endif;
  134.         }
  135.         private function removeAttempsUser(EntityManagerInterface $emUsuario $user) {
  136.             $attemp $em->getRepository(IntentosAcceso::class)->findOneBy(['idUsuario' => $user->getId()]);
  137.             $em->remove($attemp);
  138.             $em->flush();
  139.         }
  140.         private function getDataUser(Usuario $user) {
  141.             return [
  142.                 'mail' => $user->getIdPersona()->getMailPersonal(),
  143.                 'usuario' => $user->getUsuario(),
  144.                 'nombres' => $user->getIdPersona()->getNombres(),
  145.                 'apellidos' => $user->getIdPersona()->getApellidos(),
  146.             ];
  147.         }
  148.     /* Template usuario bloqueado */
  149.         public function usuarioBloqueado(CacheService $cacheEntityManagerInterface $em) {
  150.             $informacionGeneral = new InformacionGeneralController($em);
  151.             if ($cache->get('lastUsername')) :
  152.                 $cache->delete('lastUsername');
  153.                 return $this->render('Seguridades/auth/bloqueado.html.twig', [
  154.                     'informacionGeneral' => $informacionGeneral->getData(),
  155.                 ]);
  156.             else :
  157.                 return $this->redirectToRoute('login');
  158.             endif;
  159.         }
  160.     /* Template inactivo */
  161.         public function inactivo(CacheService $cacheEntityManagerInterface $em) {
  162.             $informacionGeneral = new InformacionGeneralController($em);
  163.             if ($cache->get('inactivo') && $cache->get('lastUsername')) :
  164.                 $cache->delete('lastUsername');
  165.                 $cache->delete('inactivo');
  166.                 return $this->render('Seguridades/auth/inactivo.html.twig', [
  167.                     'informacionGeneral' => $informacionGeneral->getData(),
  168.                 ]);
  169.             else :
  170.                 return $this->redirectToRoute('login');
  171.             endif;
  172.         }
  173.     /* Template cambiar contraseña */
  174.         public function changePass(AdminService $adminServiceEntityManagerInterface $em) {
  175.             $informacionGeneral = new InformacionGeneralController($em);
  176.             $form $adminService->formSetPass();
  177.             return $this->render('Seguridades/usuario/changePass/setPass.html.twig', [
  178.                 'form' => $form->createView(),
  179.                 'informacionGeneral' => $informacionGeneral->getData(),
  180.             ]);
  181.         }
  182.     /* Peticiones */
  183.         /**
  184.          * @Route("/setPass", name="setPass")
  185.          */
  186.         public function setPass(Request $requestEntityManagerInterface $emUserInterface $user) {
  187.             $loginService = new LoginService();
  188.             $pass $request->request->get('pass');
  189.             try {
  190.                 $us $em->getRepository(Usuario::class)->find($user->getId());
  191.                 $newPass $loginService->encode($pass);
  192.                 $us->setPassword($newPass);
  193.                 $us->setNuevaClave(false);
  194.                 $em->persist($us);
  195.                 $em->flush();
  196.                 return $this->json(true);
  197.             } catch (Exception $e) {
  198.                 return $this->json(false);
  199.             }
  200.         }
  201.         /**
  202.          * @Route("/verifyPass", name="verifyPass")
  203.          */
  204.         public function verifyPass(Request $requestUserInterface $user) {
  205.             $passAc $user->getPassword();
  206.             $passNw $request->request->get('passNw');
  207.             return $this->json(password_verify($passNw$passAc));
  208.         }
  209. }