src/Controller/SecurityController.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Controller;
  11. use App\Constants\PageTypeConstant;
  12. use FOS\UserBundle\Controller\SecurityController as BaseController;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. /**
  19.  * Controller managing security.
  20.  *
  21.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  22.  * @author Christophe Coevoet <stof@notk.org>
  23.  */
  24. class SecurityController extends BaseController
  25. {
  26.     private $authenticationUtils;
  27.     private $tokenManager;
  28.     public function __construct(AuthenticationUtils $authenticationUtils, ?CsrfTokenManagerInterface $tokenManager null)
  29.     {
  30.         $this->authenticationUtils $authenticationUtils;
  31.         $this->tokenManager $tokenManager;
  32.     }
  33.     /**
  34.      * @Route("/login", name="login_locale", methods={"get"})
  35.      *
  36.      * @return mixed
  37.      */
  38.     public function loginAction(): Response
  39.     {
  40.         if ($this->get('security.authorization_checker')->isGranted('ROLE_FRONT_USER')) {
  41.             // rout for redirect if user logged in
  42.             return $this->redirectToRoute('index_page');
  43.         }
  44.         $error $this->authenticationUtils->getLastAuthenticationError();
  45.         $lastUsername $this->authenticationUtils->getLastUsername();
  46.         $csrfToken $this->tokenManager
  47.             $this->tokenManager->getToken('authenticate')->getValue()
  48.             : null;
  49.         return $this->renderLogin([
  50.             'page' => PageTypeConstant::LOGIN,
  51.             'last_username' => $lastUsername,
  52.             'error' => $error,
  53.             'csrf_token' => $csrfToken,
  54.         ]);
  55.     }
  56.     public function checkAction()
  57.     {
  58.         throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  59.     }
  60.     public function logoutAction()
  61.     {
  62.         throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  63.     }
  64.     /**
  65.      * Renders the login template with the given parameters. Overwrite this function in
  66.      * an extended controller to provide additional data for the login template.
  67.      */
  68.     protected function renderLogin(array $data): Response
  69.     {
  70.         return $this->render('pages/login/login.html.twig'$data);
  71.     }
  72. }