src/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Usuario;
  4. use App\Form\UsuarioEsqueciSenhaType;
  5. use App\Services\EmailAssincronoService;
  6. use DateTime;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class SecurityController extends AbstractController
  16. {
  17.     public function login(AuthenticationUtils $authenticationUtils): Response
  18.     {
  19.          if ($this->getUser()) {
  20.              return $this->redirectToRoute('index');
  21.          }
  22.         // get the login error if there is one
  23.         $error $authenticationUtils->getLastAuthenticationError();
  24.         // last username entered by the user
  25.         $lastUsername $authenticationUtils->getLastUsername();
  26.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  27.     }
  28.     public function esqueciMinhaSenha(
  29.         Request $request,
  30.         ManagerRegistry $doctrine,
  31.         EmailAssincronoService $emailServico,
  32.         TranslatorInterface $tradutor
  33.     ) {
  34.         $usuarioEntidade = new Usuario();
  35.         $form $this->createForm(
  36.             UsuarioEsqueciSenhaType::class,
  37.             $usuarioEntidade,
  38.             [
  39.                 'method' => 'post'
  40.             ]
  41.         );
  42.         $form->handleRequest($request);
  43.         if ($form->isSubmitted() && $form->isValid()) {
  44.             $usuarioRepositorio $doctrine->getRepository(Usuario::class);
  45.             $usuarioEntidade $usuarioRepositorio->findOneBy([
  46.                 'cpf' => $usuarioEntidade->getCpf()
  47.             ]);
  48.             $usuarioEntidade->setConfirmacaoEmailData(new DateTime());
  49.             $usuarioEntidade->setToken(bin2hex(random_bytes(90)));
  50.             $doctrine->getManager()->flush();
  51.             $link $this->generateUrl('usuario_cadastrar_senha', [
  52.                 'id' => $usuarioEntidade->getId(),
  53.                 'token' => $usuarioEntidade->getToken()
  54.             ], UrlGeneratorInterface::ABSOLUTE_URL);
  55.             $emailServico->emailEsqueciSenha($usuarioEntidade, [
  56.                 'link' => $link,
  57.                 'usuarioEntidade' => $usuarioEntidade
  58.             ]);
  59.             $this->addFlash(
  60.                 'success',
  61.                 $tradutor->trans(
  62.                     'mensagem.confirmacaoEnvioEmailRedefinicao',
  63.                     [
  64.                         '%tokenLifetime%' => $this->getParameter('tokenTempoValido')
  65.                     ]
  66.                 )
  67.             );
  68.         }
  69.         return $this->render('security/esqueci-senha.html.twig', [
  70.             'form' => $form->createView()
  71.         ]);
  72.     }
  73.     public function logout(): Response
  74.     {
  75.         return $this->redirectToRoute('app_index');
  76.     }
  77.     public function acessoNegado(): Response
  78.     {
  79.     }
  80. }