vendor/contao/core-bundle/src/Resources/contao/controllers/BackendIndex.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Contao\CoreBundle\Security\Exception\LockedException;
  11. use Scheb\TwoFactorBundle\Security\Authentication\Exception\InvalidTwoFactorCodeException;
  12. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorToken;
  13. use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvent;
  14. use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvents;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\UriSigner;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  19. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  20. /**
  21.  * Handle back end logins and logouts.
  22.  */
  23. class BackendIndex extends Backend
  24. {
  25.     /**
  26.      * Initialize the controller
  27.      *
  28.      * 1. Import the user
  29.      * 2. Call the parent constructor
  30.      * 3. Login the user
  31.      * 4. Load the language files
  32.      * DO NOT CHANGE THIS ORDER!
  33.      */
  34.     public function __construct()
  35.     {
  36.         $this->import(BackendUser::class, 'User');
  37.         parent::__construct();
  38.         System::loadLanguageFile('default');
  39.         System::loadLanguageFile('tl_user');
  40.     }
  41.     /**
  42.      * Run the controller and parse the login template
  43.      *
  44.      * @return Response
  45.      */
  46.     public function run()
  47.     {
  48.         $container System::getContainer();
  49.         $exception $container->get('security.authentication_utils')->getLastAuthenticationError();
  50.         if ($exception instanceof LockedException)
  51.         {
  52.             Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['accountLocked'], $exception->getLockedMinutes()));
  53.         }
  54.         elseif ($exception instanceof InvalidTwoFactorCodeException)
  55.         {
  56.             Message::addError($GLOBALS['TL_LANG']['ERR']['invalidTwoFactor']);
  57.         }
  58.         elseif ($exception instanceof AuthenticationException)
  59.         {
  60.             Message::addError($GLOBALS['TL_LANG']['ERR']['invalidLogin']);
  61.         }
  62.         $router $container->get('router');
  63.         $targetPath $router->generate('contao_backend', array(), UrlGeneratorInterface::ABSOLUTE_URL);
  64.         $request $container->get('request_stack')->getCurrentRequest();
  65.         if ($request && $request->query->has('redirect'))
  66.         {
  67.             /** @var UriSigner $uriSigner */
  68.             $uriSigner $container->get('uri_signer');
  69.             // We cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
  70.             if ($uriSigner->check($request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo() . (null !== ($qs $request->server->get('QUERY_STRING')) ? '?' $qs '')))
  71.             {
  72.                 $targetPath $request->query->get('redirect');
  73.             }
  74.         }
  75.         $objTemplate = new BackendTemplate('be_login');
  76.         $objTemplate->headline $GLOBALS['TL_LANG']['MSC']['loginBT'];
  77.         /** @var TokenInterface $token */
  78.         $token $container->get('security.token_storage')->getToken();
  79.         if ($token instanceof TwoFactorToken)
  80.         {
  81.             // Dispatch 2FA form event to prepare 2FA providers
  82.             $event = new TwoFactorAuthenticationEvent($request$token);
  83.             $container->get('event_dispatcher')->dispatch($eventTwoFactorAuthenticationEvents::FORM);
  84.             $objTemplate = new BackendTemplate('be_login_two_factor');
  85.             $objTemplate->headline $GLOBALS['TL_LANG']['MSC']['twoFactorAuthentication'];
  86.             $objTemplate->authCode $GLOBALS['TL_LANG']['MSC']['twoFactorVerification'];
  87.             $objTemplate->cancel $GLOBALS['TL_LANG']['MSC']['cancelBT'];
  88.         }
  89.         $objTemplate->theme Backend::getTheme();
  90.         $objTemplate->messages Message::generate();
  91.         $objTemplate->base Environment::get('base');
  92.         $objTemplate->language $GLOBALS['TL_LANGUAGE'];
  93.         $objTemplate->languages System::getContainer()->get('contao.intl.locales')->getEnabledLocales(nulltrue); // backwards compatibility
  94.         $objTemplate->host Backend::getDecodedHostname();
  95.         $objTemplate->charset System::getContainer()->getParameter('kernel.charset');
  96.         $objTemplate->userLanguage $GLOBALS['TL_LANG']['tl_user']['language'][0];
  97.         $objTemplate->curUsername Input::post('username') ?: '';
  98.         $objTemplate->loginButton StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['continue']);
  99.         $objTemplate->username $GLOBALS['TL_LANG']['tl_user']['username'][0];
  100.         $objTemplate->password $GLOBALS['TL_LANG']['MSC']['password'][0];
  101.         $objTemplate->feLink $GLOBALS['TL_LANG']['MSC']['feLink'];
  102.         $objTemplate->default $GLOBALS['TL_LANG']['MSC']['default'];
  103.         $objTemplate->jsDisabled $GLOBALS['TL_LANG']['MSC']['jsDisabled'];
  104.         $objTemplate->targetPath StringUtil::specialchars(base64_encode($targetPath));
  105.         return $objTemplate->getResponse();
  106.     }
  107. }
  108. class_alias(BackendIndex::class, 'BackendIndex');