<?php
namespace App\Controller;
use App\Entity\Usuario;
use App\Form\UsuarioEsqueciSenhaType;
use App\Services\EmailAssincronoService;
use DateTime;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends AbstractController
{
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('index');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
public function esqueciMinhaSenha(
Request $request,
ManagerRegistry $doctrine,
EmailAssincronoService $emailServico,
TranslatorInterface $tradutor
) {
$usuarioEntidade = new Usuario();
$form = $this->createForm(
UsuarioEsqueciSenhaType::class,
$usuarioEntidade,
[
'method' => 'post'
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$usuarioRepositorio = $doctrine->getRepository(Usuario::class);
$usuarioEntidade = $usuarioRepositorio->findOneBy([
'cpf' => $usuarioEntidade->getCpf()
]);
$usuarioEntidade->setConfirmacaoEmailData(new DateTime());
$usuarioEntidade->setToken(bin2hex(random_bytes(90)));
$doctrine->getManager()->flush();
$link = $this->generateUrl('usuario_cadastrar_senha', [
'id' => $usuarioEntidade->getId(),
'token' => $usuarioEntidade->getToken()
], UrlGeneratorInterface::ABSOLUTE_URL);
$emailServico->emailEsqueciSenha($usuarioEntidade, [
'link' => $link,
'usuarioEntidade' => $usuarioEntidade
]);
$this->addFlash(
'success',
$tradutor->trans(
'mensagem.confirmacaoEnvioEmailRedefinicao',
[
'%tokenLifetime%' => $this->getParameter('tokenTempoValido')
]
)
);
}
return $this->render('security/esqueci-senha.html.twig', [
'form' => $form->createView()
]);
}
public function logout(): Response
{
return $this->redirectToRoute('app_index');
}
public function acessoNegado(): Response
{
}
}