vendor/contao/core-bundle/src/Resources/contao/modules/ModuleLogin.php line 105

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\TwoFactor\Event\TwoFactorAuthenticationEvent;
  13. use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvents;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. /**
  16.  * Front end module "login".
  17.  */
  18. class ModuleLogin extends Module
  19. {
  20.     /**
  21.      * Template
  22.      * @var string
  23.      */
  24.     protected $strTemplate 'mod_login';
  25.     /**
  26.      * Flash type
  27.      * @var string
  28.      */
  29.     protected $strFlashType 'contao.FE.error';
  30.     /**
  31.      * @var string
  32.      */
  33.     private $targetPath '';
  34.     /**
  35.      * Display a login form
  36.      *
  37.      * @return string
  38.      */
  39.     public function generate()
  40.     {
  41.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  42.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  43.         {
  44.             $objTemplate = new BackendTemplate('be_wildcard');
  45.             $objTemplate->wildcard '### ' $GLOBALS['TL_LANG']['FMD']['login'][0] . ' ###';
  46.             $objTemplate->title $this->headline;
  47.             $objTemplate->id $this->id;
  48.             $objTemplate->link $this->name;
  49.             $objTemplate->href StringUtil::specialcharsUrl(System::getContainer()->get('router')->generate('contao_backend', array('do'=>'themes''table'=>'tl_module''act'=>'edit''id'=>$this->id)));
  50.             return $objTemplate->parse();
  51.         }
  52.         // If the form was submitted and the credentials were wrong, take the target
  53.         // path from the submitted data as otherwise it would take the current page
  54.         if ($request && $request->isMethod('POST'))
  55.         {
  56.             $this->targetPath base64_decode($request->request->get('_target_path'));
  57.         }
  58.         elseif ($this->redirectBack && $request && $request->query->has('redirect'))
  59.         {
  60.             $uriSigner System::getContainer()->get('uri_signer');
  61.             // We cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
  62.             if ($uriSigner->check($request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo() . (null !== ($qs $request->server->get('QUERY_STRING')) ? '?' $qs '')))
  63.             {
  64.                 $this->targetPath $request->query->get('redirect');
  65.             }
  66.         }
  67.         return parent::generate();
  68.     }
  69.     /**
  70.      * Generate the module
  71.      */
  72.     protected function compile()
  73.     {
  74.         /** @var PageModel $objPage */
  75.         global $objPage;
  76.         $container System::getContainer();
  77.         $request $container->get('request_stack')->getCurrentRequest();
  78.         $exception null;
  79.         $lastUsername '';
  80.         $this->Template->requestToken $container->get('contao.csrf.token_manager')->getDefaultTokenValue();
  81.         // Only call the authentication utils if there is an active session to prevent starting an empty session
  82.         if ($request && $request->hasSession() && ($request->hasPreviousSession() || $request->getSession()->isStarted()))
  83.         {
  84.             $authUtils $container->get('security.authentication_utils');
  85.             $exception $authUtils->getLastAuthenticationError();
  86.             $lastUsername $authUtils->getLastUsername();
  87.         }
  88.         $authorizationChecker $container->get('security.authorization_checker');
  89.         if ($authorizationChecker->isGranted('ROLE_MEMBER'))
  90.         {
  91.             $this->import(FrontendUser::class, 'User');
  92.             $strRedirect Environment::get('base') . Environment::get('request');
  93.             // Redirect to last page visited
  94.             if ($this->redirectBack && $this->targetPath)
  95.             {
  96.                 $strRedirect $this->targetPath;
  97.             }
  98.             // Redirect home if the page is protected
  99.             elseif ($objPage->protected)
  100.             {
  101.                 $strRedirect Environment::get('base');
  102.             }
  103.             $this->Template->logout true;
  104.             $this->Template->formId 'tl_logout_' $this->id;
  105.             $this->Template->slabel StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['logout']);
  106.             $this->Template->loggedInAs sprintf($GLOBALS['TL_LANG']['MSC']['loggedInAs'], $this->User->username);
  107.             $this->Template->action $container->get('security.logout_url_generator')->getLogoutPath();
  108.             $this->Template->targetPath StringUtil::specialchars($strRedirect);
  109.             if ($this->User->lastLogin 0)
  110.             {
  111.                 $this->Template->lastLogin sprintf($GLOBALS['TL_LANG']['MSC']['lastLogin'][1], Date::parse($objPage->datimFormat$this->User->lastLogin));
  112.             }
  113.             return;
  114.         }
  115.         if ($exception instanceof LockedException)
  116.         {
  117.             $this->Template->hasError true;
  118.             $this->Template->message sprintf($GLOBALS['TL_LANG']['ERR']['accountLocked'], $exception->getLockedMinutes());
  119.         }
  120.         elseif ($exception instanceof InvalidTwoFactorCodeException)
  121.         {
  122.             $this->Template->hasError true;
  123.             $this->Template->message $GLOBALS['TL_LANG']['ERR']['invalidTwoFactor'];
  124.         }
  125.         elseif ($exception instanceof AuthenticationException)
  126.         {
  127.             $this->Template->hasError true;
  128.             $this->Template->message $GLOBALS['TL_LANG']['ERR']['invalidLogin'];
  129.         }
  130.         $blnRedirectBack false;
  131.         $strRedirect Environment::get('base') . Environment::get('request');
  132.         // Redirect to the last page visited
  133.         if ($this->redirectBack && $this->targetPath)
  134.         {
  135.             $blnRedirectBack true;
  136.             $strRedirect $this->targetPath;
  137.         }
  138.         // Redirect to the jumpTo page
  139.         elseif (($objTarget $this->objModel->getRelated('jumpTo')) instanceof PageModel)
  140.         {
  141.             /** @var PageModel $objTarget */
  142.             $strRedirect $objTarget->getAbsoluteUrl();
  143.         }
  144.         $this->Template->formId 'tl_login_' $this->id;
  145.         $this->Template->forceTargetPath = (int) $blnRedirectBack;
  146.         $this->Template->targetPath StringUtil::specialchars(base64_encode($strRedirect));
  147.         if ($authorizationChecker->isGranted('IS_AUTHENTICATED_2FA_IN_PROGRESS'))
  148.         {
  149.             // Dispatch 2FA form event to prepare 2FA providers
  150.             $token $container->get('security.token_storage')->getToken();
  151.             $event = new TwoFactorAuthenticationEvent($request$token);
  152.             $container->get('event_dispatcher')->dispatch($eventTwoFactorAuthenticationEvents::FORM);
  153.             $this->Template->twoFactorEnabled true;
  154.             $this->Template->authCode $GLOBALS['TL_LANG']['MSC']['twoFactorVerification'];
  155.             $this->Template->slabel StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['continue']);
  156.             $this->Template->cancel $GLOBALS['TL_LANG']['MSC']['cancelBT'];
  157.             $this->Template->twoFactorAuthentication $GLOBALS['TL_LANG']['MSC']['twoFactorAuthentication'];
  158.             return;
  159.         }
  160.         $this->Template->username $GLOBALS['TL_LANG']['MSC']['username'];
  161.         $this->Template->password $GLOBALS['TL_LANG']['MSC']['password'][0];
  162.         $this->Template->slabel StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['login']);
  163.         $this->Template->value Input::encodeInsertTags(StringUtil::specialchars($lastUsername));
  164.         $this->Template->autologin $this->autologin;
  165.         $this->Template->autoLabel $GLOBALS['TL_LANG']['MSC']['autologin'];
  166.     }
  167. }
  168. class_alias(ModuleLogin::class, 'ModuleLogin');