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

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