src/Controller/Admin/SecurityController.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Evo\Infrastructure\MappingORM\User;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. class SecurityController extends AbstractController
  16. {
  17.     private TokenStorageInterface $token;
  18.     private EntityManagerInterface $em;
  19.     private SessionInterface $session;
  20.     private ParameterBagInterface $params;
  21.     public function __construct(
  22.         TokenStorageInterface $tokenStorage,
  23.         EntityManagerInterface $em,
  24.         ParameterBagInterface $params,
  25.         SessionInterface $session
  26.     ) {
  27.         $this->token $tokenStorage;
  28.         $this->em $em;
  29.         $this->session $session;
  30.         $this->params $params;
  31.     }
  32.     /**
  33.      * @Route("/login", name="app_admin_login")
  34.      */
  35.     public function login(AuthenticationUtils $authenticationUtils): Response
  36.     {
  37.         if ($this->token->getToken()->getUser() instanceof User) {
  38.             return $this->redirectToRoute('app_admin_dashboard_index');
  39.         }
  40.         $error $authenticationUtils->getLastAuthenticationError();
  41.         $lastUsername $authenticationUtils->getLastUsername();
  42.         return $this->render('security/login.html.twig', [
  43.             'last_username' => $lastUsername,
  44.             'error' => $error,
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/logout", name="app_admin_logout", methods={"GET"})
  49.      */
  50.     public function logout(): void
  51.     {
  52.     }
  53.     /**
  54.      * @Route("/auth/confirmation", name="app_admin_auth_confirmation", methods={"GET","POST"})
  55.      *
  56.      * @return RedirectResponse|Response
  57.      */
  58.     public function authConfirmation(Request $request)
  59.     {
  60.         $email null;
  61.         $currentUser $this->session->get('current_user');
  62.         if (!isset($currentUser)) {
  63.             /** @var User $user */
  64.             $user $this->getUser();
  65.             if ($user) {
  66.                 $email $user->getEmail();
  67.                 $this->session->set('current_user'serialize($user));
  68.             }
  69.         }
  70.         $this->container->get('security.token_storage')->getToken()->setAuthenticated(false);
  71.         if (null !== $request->request->get('code_confirmation')) {
  72.             $value = (int) $request->request->get('code_confirmation');
  73.             if (isset($currentUser)) {
  74.                 $user unserialize($currentUser);
  75.                 $user $this->em->getRepository(User::class)->find($user->getId());
  76.                 if ($value === $user->getCodeConfirmation()) {
  77.                     unset($currentUser);
  78.                     $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  79.                     $this->container->get('security.token_storage')->setToken($token);
  80.                     $this->container->get('session')->set('_security_main'serialize($token));
  81.                     $expired = (int) $this->params->get('session_max_idle_time');
  82.                     setcookie('auto_logout'0, ['expires' => time() + $expired'path' => '/']);
  83.                     return $this->redirectToRoute('app_admin_dashboard_index');
  84.                 }
  85.                 return $this->render('security/auth_confirmation.html.twig', [
  86.                     'error' => 'Code invalide',
  87.                     'userEmail' => $email,
  88.                 ]);
  89.             }
  90.         }
  91.         return $this->render('security/auth_confirmation.html.twig', [
  92.             'error' => null,
  93.             'userEmail' => $email,
  94.         ]);
  95.     }
  96. }