src/Controller/SecurityController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  10. /**
  11.  * Controller used to manage the application security.
  12.  * See https://symfony.com/doc/current/cookbook/security/form_login_setup.html.
  13.  *
  14.  */
  15. class SecurityController extends AbstractController
  16. {
  17.     use TargetPathTrait;
  18.     /**
  19.      * @Route("/login", name="security_login")
  20.      */
  21.     public function login(Request $requestSecurity $securityAuthenticationUtils $authenticationUtils): Response
  22.     {
  23.         // if user is already logged in, don't display the login page again
  24.         if ($security->isGranted('ROLE_USER')) {
  25.             return $this->redirectToRoute('admin_search');
  26.         }
  27.         // this statement solves an edge-case: if you change the locale in the login
  28.         // page, after a successful login you are redirected to a page in the previous
  29.         // locale. This code regenerates the referrer URL whenever the login page is
  30.         // browsed, to ensure that its locale is always the current one.
  31.         $this->saveTargetPath($request->getSession(), 'main'$this->generateUrl('admin_search'));
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         $lastUsername $authenticationUtils->getLastUsername();
  34.         return $this->render('@EasyAdmin/page/login.html.twig', [
  35.             // parameters usually defined in Symfony login forms
  36.             'error' => $error,
  37.             'last_username' => $lastUsername,
  38.             // OPTIONAL parameters to customize the login form:
  39.             // the translation_domain to use (define this option only if you are
  40.             // rendering the login template in a regular Symfony controller; when
  41.             // rendering it from an EasyAdmin Dashboard this is automatically set to
  42.             // the same domain as the rest of the Dashboard)
  43.             'translation_domain' => 'admin',
  44.             // the title visible above the login form (define this option only if you are
  45.             // rendering the login template in a regular Symfony controller; when rendering
  46.             // it from an EasyAdmin Dashboard this is automatically set as the Dashboard title)
  47.             'page_title' => 'Farelogix LHG',
  48.             // the string used to generate the CSRF token. If you don't define
  49.             // this parameter, the login form won't include a CSRF token
  50.             'csrf_token_intention' => 'authenticate',
  51.             // the URL users are redirected to after the login (default: '/admin')
  52.             'target_path' => $this->generateUrl('admin'),
  53.             // the label displayed for the username form field (the |trans filter is applied to it)
  54.             'username_label' => 'Your username',
  55.             // the label displayed for the password form field (the |trans filter is applied to it)
  56.             'password_label' => 'Your password',
  57.             // the label displayed for the Sign In form button (the |trans filter is applied to it)
  58.             'sign_in_label' => 'Log in',
  59.             // the 'name' HTML attribute of the <input> used for the username field (default: '_username')
  60.             'username_parameter' => '_username',
  61.             // the 'name' HTML attribute of the <input> used for the password field (default: '_password')
  62.             'password_parameter' => '_password',
  63.             // whether to enable or not the "forgot password?" link (default: false)
  64.             'forgot_password_enabled' => false,
  65.             // the path (i.e. a relative or absolute URL) to visit when clicking the "forgot password?" link (default: '#')
  66.             //'forgot_password_path' => $this->generateUrl('...', ['...' => '...']),
  67.             // the label displayed for the "forgot password?" link (the |trans filter is applied to it)
  68.             'forgot_password_label' => 'Forgot your password?',
  69.             // whether to enable or not the "remember me" checkbox (default: false)
  70.             'remember_me_enabled' => true,
  71.             // remember me name form field (default: '_remember_me')
  72.             'remember_me_parameter' => '_remember_me',
  73.             // whether to check by default the "remember me" checkbox (default: false)
  74.             'remember_me_checked' => true,
  75.             // the label displayed for the remember me checkbox (the |trans filter is applied to it)
  76.             'remember_me_label' => 'Remember me',
  77.         ]);
  78.        /* return $this->render('security/login.html.twig', [
  79.             // last username entered by the user (if any)
  80.             'last_username' => $authenticationUtils->getLastUsername(),
  81.             // last authentication error (if any)
  82.             'error' => $authenticationUtils->getLastAuthenticationError(),
  83.         ]);*/
  84.     }
  85.     /**
  86.      * This is the route the user can use to logout.
  87.      *
  88.      * But, this will never be executed. Symfony will intercept this first
  89.      * and handle the logout automatically. See logout in config/packages/security.yaml
  90.      *
  91.      * @Route("/logout", name="security_logout")
  92.      */
  93.     public function logout(): void
  94.     {
  95.         throw new \Exception('This should never be reached!');
  96.     }
  97. }