<?php
namespace App\Controller\Seguridades;
use App\Controller\InformacionGeneralController;
use App\Entity\Seguridades\{IntentosAcceso, Usuario};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\{Request};
use Symfony\Component\Routing\Annotation\Route;
use App\Service\{AdminService, CacheService, EmailService, LoginService};
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Form\Extension\Core\Type\{SubmitType, TextType};
use Symfony\Component\Security\Core\User\UserInterface;
class AuthController extends AbstractController {
/* Template login */
public function index(AuthenticationUtils $authenticationUtils, EntityManagerInterface $em, CacheService $cache, LoginService $login) {
$informacionGeneral = new InformacionGeneralController($em);
$message = '';
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
if ($error) :
$user = $login->checkUser($lastUsername, $em);
if ($user) :
$cache->add('lastUsername', $lastUsername);
if ($user['active']) :
if (!$user['bloqueado']) :
$message = $login->checkAttemps($user, $em);
if (!$message) :
return $this->redirectToRoute('bloqueado');
endif;
else :
return $this->redirectToRoute('usuarioBloqueado');
endif;
else :
$cache->add('inactivo', true);
return $this->redirectToRoute('inactivo');
endif;
else :
$message = 'Usuario/Contraseña Incorrectos';
endif;
endif;
return $this->render('Seguridades/auth/login/login.html.twig', [
'error' => $error,
'last_username' => $lastUsername,
'message' => $message,
'informacionGeneral' => $informacionGeneral->getData()
]);
}
public function accessControl(UserInterface $user, EntityManagerInterface $em, CacheService $cache, AdminService $adminService, LoginService $loginSer) {
$intentosAcceso = $em->getRepository(IntentosAcceso::class)->findOneBy(['idUsuario' => $user->getId()]);
if ($intentosAcceso) : $em->remove($intentosAcceso);
$em->flush();
endif;
if (!$user->getActivo()) :
$cache->add('lastUsername', $user->getUsername());
$cache->add('inactivo', true);
return $this->redirectToRoute('inactivo');
endif;
if ($user->getBloqueado()) :
$cache->add('lastUsername', $user->getUsername());
return $this->redirectToRoute('bloqueado');
endif;
if ($adminService->checkPassChange($user)) : return $this->redirectToRoute('change_pass');
endif;
$loginSer->addAccess($user, 'home', $em);
return $this->redirectToRoute('home');
}
/* Template recuperacion contraseña */
public function recuperacion(Request $request, LoginService $login, EntityManagerInterface $em, EmailService $emailService) {
$informacionGeneral = new InformacionGeneralController($em);
$form = $this->createRecForm();
$form->handleRequest($request);
$user = $message = '';
if ($form->isSubmitted()) :
$usuario = $form->get('usuario')->getData();
$user = $login->getUserLog($usuario, $em);
if ($user && $user->getActivo()) :
$datosUser = $this->getDataUser($user);
$pass = $login->setPasswordUser($user, $em);
$emailService->SendEmailRecuperarContrasenia($datosUser, $pass, $em);
return $this->render('Seguridades/auth/recuperar_password.html.twig',[
'informacionGeneral' => $informacionGeneral->getData()
]);
else :
$message = "El Usuario no existe o se encuentra Inactivo, porfavor contactarse con el Administrador";
endif;
endif;
return $this->render('Seguridades/auth/recuperar.html.twig', [
'informacionGeneral' => $informacionGeneral->getData(),
'form' => $form->createView(),
'user' => $user,
'message' => $message
]);
}
private function createRecForm() {
return $this->createFormBuilder($user = new Usuario)
->add('usuario', TextType::class, array(
'attr' => [
'class' => 'form-control'
],
'mapped' => false
))
->add('submit', SubmitType::class, array(
'attr' => [
'class' => 'btn btn-primary'
],
'label' => 'Recuperar Cuenta'
))
->getForm();
}
/* Template bloqueado */
public function bloqueado(LoginService $login, CacheService $cache, EntityManagerInterface $em, EmailService $emailService) {
$user = $cache->get('lastUsername');
$informacionGeneral = new InformacionGeneralController($em);
if ($user) :
$cache->delete('lastUsername');
$user = $em->getRepository(Usuario::class)->findOneBy(['usuario' => $user]);
$dataPerson = $this->getDataUser($user);
$emailService->sendEmailBloqueo($dataPerson, $em);
$login->setStateUser($user, $em);
$correo = $user->getIdPersona()->getMailPersonal();
$correo = substr($correo, 0, 5);
$correo .= 'XXXXXXX';
$this->removeAttempsUser($em, $user);
return $this->render('Seguridades/auth/comprobar.html.twig', [
'informacionGeneral' => $informacionGeneral->getData(),
'nombres' => $dataPerson['nombres'],
'apellidos' => $dataPerson['apellidos'],
'correo' => $correo
]);
else :
return $this->redirectToRoute('login');
endif;
}
private function removeAttempsUser(EntityManagerInterface $em, Usuario $user) {
$attemp = $em->getRepository(IntentosAcceso::class)->findOneBy(['idUsuario' => $user->getId()]);
$em->remove($attemp);
$em->flush();
}
private function getDataUser(Usuario $user) {
return [
'mail' => $user->getIdPersona()->getMailPersonal(),
'usuario' => $user->getUsuario(),
'nombres' => $user->getIdPersona()->getNombres(),
'apellidos' => $user->getIdPersona()->getApellidos(),
];
}
/* Template usuario bloqueado */
public function usuarioBloqueado(CacheService $cache, EntityManagerInterface $em) {
$informacionGeneral = new InformacionGeneralController($em);
if ($cache->get('lastUsername')) :
$cache->delete('lastUsername');
return $this->render('Seguridades/auth/bloqueado.html.twig', [
'informacionGeneral' => $informacionGeneral->getData(),
]);
else :
return $this->redirectToRoute('login');
endif;
}
/* Template inactivo */
public function inactivo(CacheService $cache, EntityManagerInterface $em) {
$informacionGeneral = new InformacionGeneralController($em);
if ($cache->get('inactivo') && $cache->get('lastUsername')) :
$cache->delete('lastUsername');
$cache->delete('inactivo');
return $this->render('Seguridades/auth/inactivo.html.twig', [
'informacionGeneral' => $informacionGeneral->getData(),
]);
else :
return $this->redirectToRoute('login');
endif;
}
/* Template cambiar contraseña */
public function changePass(AdminService $adminService, EntityManagerInterface $em) {
$informacionGeneral = new InformacionGeneralController($em);
$form = $adminService->formSetPass();
return $this->render('Seguridades/usuario/changePass/setPass.html.twig', [
'form' => $form->createView(),
'informacionGeneral' => $informacionGeneral->getData(),
]);
}
/* Peticiones */
/**
* @Route("/setPass", name="setPass")
*/
public function setPass(Request $request, EntityManagerInterface $em, UserInterface $user) {
$loginService = new LoginService();
$pass = $request->request->get('pass');
try {
$us = $em->getRepository(Usuario::class)->find($user->getId());
$newPass = $loginService->encode($pass);
$us->setPassword($newPass);
$us->setNuevaClave(false);
$em->persist($us);
$em->flush();
return $this->json(true);
} catch (Exception $e) {
return $this->json(false);
}
}
/**
* @Route("/verifyPass", name="verifyPass")
*/
public function verifyPass(Request $request, UserInterface $user) {
$passAc = $user->getPassword();
$passNw = $request->request->get('passNw');
return $this->json(password_verify($passNw, $passAc));
}
}