src/Controller/Publico/SecurityController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Publico;
  3. use App\Entity\Ajustes\Direccion;
  4. use App\Entity\Ajustes\Usuario;
  5. use App\Form\Publico\Registro\RegistroUsuarioType;
  6. use App\Repository\Ajustes\UsuarioRepository;
  7. use App\Security\AppAuthenticator;
  8. use App\Service\EmailService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Sonata\SeoBundle\Seo\SeoPageInterface;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  19. class SecurityController extends AbstractController
  20. {
  21.     public const BASEDIR '/pages/publico/security';
  22.     private $seo;
  23.     protected $trans;
  24.     private $repo;
  25.     private $hash;
  26.     private $em;
  27.     private $authUser;
  28.     private $authLogin;
  29.     public function __construct(TranslatorInterface $trSeoPageInterface $seoUsuarioRepository $repoUserPasswordHasherInterface $hashEntityManagerInterface $emUserAuthenticatorInterface $authUserAppAuthenticator $authLogin){
  30.         $this->trans $tr;     
  31.         $this->seo   $seo;
  32.         $this->repo   $repo;
  33.         $this->hash   $hash;
  34.         $this->em   $em;
  35.         $this->authUser   $authUser;
  36.         $this->authLogin   $authLogin;
  37.     }
  38.     /**
  39.      * @Route("/acceder", name="app_login")
  40.      */
  41.     public function login(AuthenticationUtils $authenticationUtilsRequest $request): Response
  42.     {
  43.         if ($this->getUser()) {
  44.             return $this->redirectToRoute('index');
  45.         }
  46.         
  47.         // get the login error if there is one
  48.         $error $authenticationUtils->getLastAuthenticationError();
  49.         // last email entered by the user
  50.         $lastEmail $authenticationUtils->getLastUsername();
  51.         //SEO
  52.         $titulo =$this->trans->trans('security.login.seo.title', [], 'security');
  53.         $descripcion =$this->trans->trans('security.login.seo.description', [], 'security');
  54.         $this->seo 
  55.             ->setTitle($titulo)
  56.             ->addMeta('name''description'$descripcion)
  57.             ->addMeta('property''og:title'$titulo)
  58.             ->addMeta('property''og:description'$descripcion)
  59.         ;
  60.         return $this->render(self::BASEDIR.'/login.html.twig', ['last_email' => $lastEmail'error' => $error]);
  61.     }
  62.     /**
  63.      * @Route("/salir", name="app_logout")
  64.      */
  65.     public function logout(): void
  66.     {
  67.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  68.     }
  69.     /**
  70.      * @Route("/registro", name="app_register")
  71.      */
  72.     public function register(Request $requestEmailService $servEmails): Response
  73.     {
  74.         if ($this->getUser()) {
  75.             return $this->redirectToRoute('index');
  76.         }
  77.         
  78.         $direccion = (new Direccion())->setDirEnabled(true);
  79.         $usuario = (new Usuario())
  80.             ->setRoles(['ROLE_CLIENTE'])
  81.             ->addDireccione($direccion)
  82.         ;
  83.         $form $this->createForm(RegistroUsuarioType::class, $usuario);
  84.         $form->handleRequest($request);
  85.         if ($form->isSubmitted()){
  86.             if($form->isValid()){
  87.                 $usuario->setPassword(
  88.                     $this->hash->hashPassword(
  89.                         $usuario,
  90.                         $usuario->getPassword()
  91.                     )
  92.                 );
  93.                 $usuario
  94.                     ->setLastLoginIp($request->getClientIp())
  95.                 ;
  96.                 $this->em->persist($usuario);
  97.                 $this->em->persist($direccion->setDirUser($usuario));
  98.                 $this->em->flush();
  99.                 $servEmails->enviarEmailValidacion('/emails/email_verification.html.twig'$usuario);
  100.                 return $this->authUser->authenticateUser($usuario$this->authLogin$request);
  101.             }else{
  102.                 $this->addFlash('danger'$this->trans->trans('security.register.form.error', [], 'security'));
  103.             }
  104.         }else{
  105.             $this->addFlash('info'$this->trans->trans('security.register.form.info', [], 'security'));
  106.         }
  107.         //SEO
  108.         $titulo =$this->trans->trans('security.register.seo.title', [], 'security');
  109.         $descripcion =$this->trans->trans('security.register.seo.description', [], 'security');
  110.         $this->seo 
  111.             ->setTitle($titulo)
  112.             ->addMeta('name''description'$descripcion)
  113.             ->addMeta('property''og:title'$titulo)
  114.             ->addMeta('property''og:description'$descripcion)
  115.         ;
  116.         return $this->render(self::BASEDIR'/registro_usuario.html.twig', [
  117.             'form' => $form->createView()
  118.         ]);
  119.     }
  120. }