<?php
namespace App\Controller\Publico;
use App\Entity\Ajustes\Direccion;
use App\Entity\Ajustes\Usuario;
use App\Form\Publico\Registro\RegistroUsuarioType;
use App\Repository\Ajustes\UsuarioRepository;
use App\Security\AppAuthenticator;
use App\Service\EmailService;
use Doctrine\ORM\EntityManagerInterface;
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 Symfony\Contracts\Translation\TranslatorInterface;
use Sonata\SeoBundle\Seo\SeoPageInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
class SecurityController extends AbstractController
{
public const BASEDIR = '/pages/publico/security';
private $seo;
protected $trans;
private $repo;
private $hash;
private $em;
private $authUser;
private $authLogin;
public function __construct(TranslatorInterface $tr, SeoPageInterface $seo, UsuarioRepository $repo, UserPasswordHasherInterface $hash, EntityManagerInterface $em, UserAuthenticatorInterface $authUser, AppAuthenticator $authLogin){
$this->trans = $tr;
$this->seo = $seo;
$this->repo = $repo;
$this->hash = $hash;
$this->em = $em;
$this->authUser = $authUser;
$this->authLogin = $authLogin;
}
/**
* @Route("/acceder", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('index');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last email entered by the user
$lastEmail = $authenticationUtils->getLastUsername();
//SEO
$titulo =$this->trans->trans('security.login.seo.title', [], 'security');
$descripcion =$this->trans->trans('security.login.seo.description', [], 'security');
$this->seo
->setTitle($titulo)
->addMeta('name', 'description', $descripcion)
->addMeta('property', 'og:title', $titulo)
->addMeta('property', 'og:description', $descripcion)
;
return $this->render(self::BASEDIR.'/login.html.twig', ['last_email' => $lastEmail, 'error' => $error]);
}
/**
* @Route("/salir", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/registro", name="app_register")
*/
public function register(Request $request, EmailService $servEmails): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('index');
}
$direccion = (new Direccion())->setDirEnabled(true);
$usuario = (new Usuario())
->setRoles(['ROLE_CLIENTE'])
->addDireccione($direccion)
;
$form = $this->createForm(RegistroUsuarioType::class, $usuario);
$form->handleRequest($request);
if ($form->isSubmitted()){
if($form->isValid()){
$usuario->setPassword(
$this->hash->hashPassword(
$usuario,
$usuario->getPassword()
)
);
$usuario
->setLastLoginIp($request->getClientIp())
;
$this->em->persist($usuario);
$this->em->persist($direccion->setDirUser($usuario));
$this->em->flush();
$servEmails->enviarEmailValidacion('/emails/email_verification.html.twig', $usuario);
return $this->authUser->authenticateUser($usuario, $this->authLogin, $request);
}else{
$this->addFlash('danger', $this->trans->trans('security.register.form.error', [], 'security'));
}
}else{
$this->addFlash('info', $this->trans->trans('security.register.form.info', [], 'security'));
}
//SEO
$titulo =$this->trans->trans('security.register.seo.title', [], 'security');
$descripcion =$this->trans->trans('security.register.seo.description', [], 'security');
$this->seo
->setTitle($titulo)
->addMeta('name', 'description', $descripcion)
->addMeta('property', 'og:title', $titulo)
->addMeta('property', 'og:description', $descripcion)
;
return $this->render(self::BASEDIR. '/registro_usuario.html.twig', [
'form' => $form->createView()
]);
}
}