<?php
namespace App\Controller\Admin;
use Doctrine\ORM\EntityManagerInterface;
use Evo\Infrastructure\MappingORM\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
private TokenStorageInterface $token;
private EntityManagerInterface $em;
private SessionInterface $session;
private ParameterBagInterface $params;
public function __construct(
TokenStorageInterface $tokenStorage,
EntityManagerInterface $em,
ParameterBagInterface $params,
SessionInterface $session
) {
$this->token = $tokenStorage;
$this->em = $em;
$this->session = $session;
$this->params = $params;
}
/**
* @Route("/login", name="app_admin_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->token->getToken()->getUser() instanceof User) {
return $this->redirectToRoute('app_admin_dashboard_index');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/logout", name="app_admin_logout", methods={"GET"})
*/
public function logout(): void
{
}
/**
* @Route("/auth/confirmation", name="app_admin_auth_confirmation", methods={"GET","POST"})
*
* @return RedirectResponse|Response
*/
public function authConfirmation(Request $request)
{
$email = null;
$currentUser = $this->session->get('current_user');
if (!isset($currentUser)) {
/** @var User $user */
$user = $this->getUser();
if ($user) {
$email = $user->getEmail();
$this->session->set('current_user', serialize($user));
}
}
$this->container->get('security.token_storage')->getToken()->setAuthenticated(false);
if (null !== $request->request->get('code_confirmation')) {
$value = (int) $request->request->get('code_confirmation');
if (isset($currentUser)) {
$user = unserialize($currentUser);
$user = $this->em->getRepository(User::class)->find($user->getId());
if ($value === $user->getCodeConfirmation()) {
unset($currentUser);
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->container->get('security.token_storage')->setToken($token);
$this->container->get('session')->set('_security_main', serialize($token));
$expired = (int) $this->params->get('session_max_idle_time');
setcookie('auto_logout', 0, ['expires' => time() + $expired, 'path' => '/']);
return $this->redirectToRoute('app_admin_dashboard_index');
}
return $this->render('security/auth_confirmation.html.twig', [
'error' => 'Code invalide',
'userEmail' => $email,
]);
}
}
return $this->render('security/auth_confirmation.html.twig', [
'error' => null,
'userEmail' => $email,
]);
}
}