vendor/pimcore/pimcore/bundles/AdminBundle/Controller/Admin/LoginController.php line 71

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\AdminBundle\Controller\Admin;
  15. use Pimcore\Bundle\AdminBundle\Controller\AdminController;
  16. use Pimcore\Bundle\AdminBundle\Controller\BruteforceProtectedControllerInterface;
  17. use Pimcore\Bundle\AdminBundle\EventListener\CsrfProtectionListener;
  18. use Pimcore\Bundle\AdminBundle\Security\BruteforceProtectionHandler;
  19. use Pimcore\Config;
  20. use Pimcore\Controller\Configuration\TemplatePhp;
  21. use Pimcore\Controller\EventedControllerInterface;
  22. use Pimcore\Event\Admin\Login\LostPasswordEvent;
  23. use Pimcore\Event\AdminEvents;
  24. use Pimcore\Logger;
  25. use Pimcore\Model\User;
  26. use Pimcore\Templating\Model\ViewModel;
  27. use Pimcore\Tool;
  28. use Pimcore\Tool\Authentication;
  29. use Symfony\Component\HttpFoundation\RedirectResponse;
  30. use Symfony\Component\HttpFoundation\Request;
  31. use Symfony\Component\HttpFoundation\Response;
  32. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  33. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  34. use Symfony\Component\Routing\Annotation\Route;
  35. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  36. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  37. use Symfony\Component\Security\Core\Security;
  38. use Symfony\Component\Security\Core\User\UserInterface;
  39. class LoginController extends AdminController implements BruteforceProtectedControllerInterfaceEventedControllerInterface
  40. {
  41.     public function onKernelController(FilterControllerEvent $event)
  42.     {
  43.         // use browser language for login page if possible
  44.         $locale 'en';
  45.         $availableLocales Tool\Admin::getLanguages();
  46.         foreach ($event->getRequest()->getLanguages() as $userLocale) {
  47.             if (in_array($userLocale$availableLocales)) {
  48.                 $locale $userLocale;
  49.                 break;
  50.             }
  51.         }
  52.         $this->get('translator')->setLocale($locale);
  53.     }
  54.     public function onKernelResponse(FilterResponseEvent $event)
  55.     {
  56.         $event->getResponse()->headers->set('X-Frame-Options''deny'true);
  57.     }
  58.     /**
  59.      * @Route("/login", name="pimcore_admin_login")
  60.      * @Route("/login/", name="pimcore_admin_login_fallback")
  61.      *
  62.      * @TemplatePhp()
  63.      */
  64.     public function loginAction(Request $requestCsrfProtectionListener $csrfProtectionListenerConfig $config)
  65.     {
  66.         if ($request->get('_route') === 'pimcore_admin_login_fallback') {
  67.             return $this->redirectToRoute('pimcore_admin_login'$request->query->all(), Response::HTTP_MOVED_PERMANENTLY);
  68.         }
  69.         $csrfProtectionListener->regenerateCsrfToken();
  70.         $user $this->getAdminUser();
  71.         if ($user instanceof UserInterface) {
  72.             return $this->redirectToRoute('pimcore_admin_index');
  73.         }
  74.         $view $this->buildLoginPageViewModel($config);
  75.         if ($request->get('auth_failed')) {
  76.             $view->error 'error_auth_failed';
  77.         }
  78.         if ($request->get('session_expired')) {
  79.             $view->error 'error_session_expired';
  80.         }
  81.         return $view;
  82.     }
  83.     /**
  84.      * @Route("/logout", name="pimcore_admin_logout")
  85.      */
  86.     public function logoutAction()
  87.     {
  88.         // this route will never be matched, but will be handled by the logout handler
  89.     }
  90.     /**
  91.      * Dummy route used to check authentication
  92.      *
  93.      * @Route("/login/login", name="pimcore_admin_login_check")
  94.      *
  95.      * @see AdminAuthenticator for the security implementation
  96.      */
  97.     public function loginCheckAction()
  98.     {
  99.         // just in case the authenticator didn't redirect
  100.         return new RedirectResponse($this->generateUrl('pimcore_admin_login'));
  101.     }
  102.     /**
  103.      * @Route("/login/lostpassword")
  104.      * @TemplatePhp()
  105.      */
  106.     public function lostpasswordAction(Request $requestBruteforceProtectionHandler $bruteforceProtectionHandlerCsrfProtectionListener $csrfProtectionListenerConfig $config)
  107.     {
  108.         $view $this->buildLoginPageViewModel($config);
  109.         $error null;
  110.         if ($request->getMethod() === 'POST' && $username $request->get('username')) {
  111.             $user User::getByName($username);
  112.             if ($user instanceof User) {
  113.                 if (!$user->isActive()) {
  114.                     $error 'user inactive';
  115.                 }
  116.                 if (!$user->getEmail()) {
  117.                     $error 'user has no email address';
  118.                 }
  119.                 if (!$user->getPassword()) {
  120.                     $error 'user has no password';
  121.                 }
  122.             } else {
  123.                 $error 'user unknown';
  124.             }
  125.             if (!$error && $user instanceof User) {
  126.                 $token Authentication::generateToken($user->getName());
  127.                 $loginUrl $this->generateUrl('pimcore_admin_login_check', [
  128.                     'token' => $token,
  129.                     'reset' => 'true'
  130.                 ], UrlGeneratorInterface::ABSOLUTE_URL);
  131.                 try {
  132.                     $event = new LostPasswordEvent($user$loginUrl);
  133.                     $this->get('event_dispatcher')->dispatch(AdminEvents::LOGIN_LOSTPASSWORD$event);
  134.                     // only send mail if it wasn't prevented in event
  135.                     if ($event->getSendMail()) {
  136.                         $mail Tool::getMail([$user->getEmail()], 'Pimcore lost password service');
  137.                         $mail->setIgnoreDebugMode(true);
  138.                         $mail->setBodyText("Login to pimcore and change your password using the following link. This temporary login link will expire in 24 hours: \r\n\r\n" $loginUrl);
  139.                         $mail->send();
  140.                     }
  141.                     // directly return event response
  142.                     if ($event->hasResponse()) {
  143.                         return $event->getResponse();
  144.                     }
  145.                 } catch (\Exception $e) {
  146.                     $error 'could not send email';
  147.                 }
  148.             }
  149.             if ($error) {
  150.                 Logger::error('Lost password service: ' $error);
  151.                 $bruteforceProtectionHandler->addEntry($request->get('username'), $request);
  152.             }
  153.         }
  154.         $csrfProtectionListener->regenerateCsrfToken();
  155.         return $view;
  156.     }
  157.     /**
  158.      * @Route("/login/deeplink")
  159.      * @TemplatePhp()
  160.      */
  161.     public function deeplinkAction(Request $request)
  162.     {
  163.         // check for deeplink
  164.         $queryString $_SERVER['QUERY_STRING'];
  165.         if (preg_match('/(document|asset|object)_([0-9]+)_([a-z]+)/'$queryString$deeplink)) {
  166.             $deeplink $deeplink[0];
  167.             $perspective strip_tags($request->get('perspective'));
  168.             if (strpos($queryString'token')) {
  169.                 $url $this->generateUrl('pimcore_admin_login', [
  170.                     'deeplink' => $deeplink,
  171.                     'perspective' => $perspective
  172.                 ]);
  173.                 $url .= '&' $queryString;
  174.                 return $this->redirect($url);
  175.             } elseif ($queryString) {
  176.                 return new ViewModel([
  177.                     'tab' => $deeplink,
  178.                     'perspective' => $perspective
  179.                 ]);
  180.             }
  181.         }
  182.     }
  183.     /**
  184.      * @return ViewModel
  185.      */
  186.     protected function buildLoginPageViewModel(Config $config)
  187.     {
  188.         $bundleManager $this->get('pimcore.extension.bundle_manager');
  189.         $view = new ViewModel([
  190.             'config' => $config,
  191.             'pluginCssPaths' => $bundleManager->getCssPaths()
  192.         ]);
  193.         return $view;
  194.     }
  195.     /**
  196.      * @Route("/login/2fa", name="pimcore_admin_2fa")
  197.      * @TemplatePhp()
  198.      */
  199.     public function twoFactorAuthenticationAction(Request $requestBruteforceProtectionHandler $bruteforceProtectionHandlerConfig $config)
  200.     {
  201.         $view $this->buildLoginPageViewModel($config);
  202.         if ($request->hasSession()) {
  203.             // we have to call the check here manually, because BruteforceProtectionListener uses the 'username' from the request
  204.             $bruteforceProtectionHandler->checkProtection($this->getAdminUser()->getName(), $request);
  205.             $session $request->getSession();
  206.             $authException $session->get(Security::AUTHENTICATION_ERROR);
  207.             if ($authException instanceof AuthenticationException) {
  208.                 $session->remove(Security::AUTHENTICATION_ERROR);
  209.                 $view->error $authException->getMessage();
  210.                 $bruteforceProtectionHandler->addEntry($this->getAdminUser()->getName(), $request);
  211.             }
  212.         } else {
  213.             $view->error 'No session available, it either timed out or cookies are not enabled.';
  214.         }
  215.         return $view;
  216.     }
  217.     /**
  218.      * @Route("/login/2fa-verify", name="pimcore_admin_2fa-verify")
  219.      *
  220.      * @param Request $request
  221.      */
  222.     public function twoFactorAuthenticationVerifyAction(Request $request)
  223.     {
  224.     }
  225. }