vendor/pimcore/customer-management-framework-bundle/src/Security/Authentication/LoginManager.php line 116

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace CustomerManagementFrameworkBundle\Security\Authentication;
  16. use Pimcore\Http\RequestHelper;
  17. use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
  18. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  19. use Symfony\Component\DependencyInjection\ContainerInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  24. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  25. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  26. use Symfony\Component\Security\Core\User\UserInterface;
  27. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  28. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  29. /**
  30.  * Handles manual user logins (e.g. from registration). The logic implemented here basically
  31.  * resembles to LoginManager in FOSUserBundle, but resolves firewall config, user checkers and
  32.  * remember me services dynamically from the firewall config instead of wiring it toghether via
  33.  * compiler passes as we don't have a config defining which firewall to use in CMF.
  34.  */
  35. class LoginManager implements LoginManagerInterface
  36. {
  37.     /**
  38.      * @var RequestHelper
  39.      */
  40.     private $requestHelper;
  41.     /**
  42.      * @var FirewallMap
  43.      */
  44.     private $firewallMap;
  45.     /**
  46.      * @var SessionAuthenticationStrategyInterface
  47.      */
  48.     private $sessionStrategy;
  49.     /**
  50.      * @var TokenStorageInterface
  51.      */
  52.     private $tokenStorage;
  53.     /**
  54.      * @var UserCheckerInterface
  55.      */
  56.     private $defaultUserChecker;
  57.     /**
  58.      * @var ContainerInterface
  59.      */
  60.     private $container;
  61.     public function __construct(
  62.         RequestHelper $requestHelper,
  63.         FirewallMap $firewallMap,
  64.         SessionAuthenticationStrategyInterface $sessionStrategy,
  65.         TokenStorageInterface $tokenStorage,
  66.         UserCheckerInterface $defaultUserChecker,
  67.         ContainerInterface $container
  68.     ) {
  69.         $this->firewallMap $firewallMap;
  70.         $this->requestHelper $requestHelper;
  71.         $this->sessionStrategy $sessionStrategy;
  72.         $this->tokenStorage $tokenStorage;
  73.         $this->defaultUserChecker $defaultUserChecker;
  74.         $this->container $container;
  75.     }
  76.     /**
  77.      * @inheritdoc
  78.      */
  79.     public function login(UserInterface $userRequest $request nullResponse $response null)
  80.     {
  81.         if (null === $request) {
  82.             $request $this->requestHelper->getCurrentRequest();
  83.         }
  84.         $firewallConfig $this->firewallMap->getFirewallConfig($request);
  85.         $userChecker $this->getUserChecker($firewallConfig);
  86.         $rememberMeService $this->getRememberMeService($firewallConfig);
  87.         $userChecker->checkPreAuth($user);
  88.         $token $this->createToken($firewallConfig->getName(), $user);
  89.         $this->sessionStrategy->onAuthentication($request$token);
  90.         if (null !== $response && null !== $rememberMeService) {
  91.             $rememberMeService->loginSuccess($request$response$token);
  92.         }
  93.         $this->tokenStorage->setToken($token);
  94.     }
  95.     private function createToken(string $firewallNameUserInterface $user): TokenInterface
  96.     {
  97.         return new UsernamePasswordToken($usernull$firewallName$user->getRoles());
  98.     }
  99.     private function getUserChecker(FirewallConfig $config): UserCheckerInterface
  100.     {
  101.         if ($this->container->has($config->getUserChecker())) {
  102.             /** @var UserCheckerInterface $userChecker */
  103.             $userChecker $this->container->get($config->getUserChecker());
  104.             return $userChecker;
  105.         }
  106.         return $this->defaultUserChecker;
  107.     }
  108.     /**
  109.      * @param FirewallConfig $config
  110.      *
  111.      * @return RememberMeServicesInterface|null
  112.      */
  113.     private function getRememberMeService(FirewallConfig $config)
  114.     {
  115.         $definitions = [
  116.             'security.authentication.rememberme.services.persistent.' $config->getName(),
  117.             'security.authentication.rememberme.services.simplehash.' $config->getName()
  118.         ];
  119.         foreach ($definitions as $definition) {
  120.             if ($this->container->has($definition)) {
  121.                 /** @var RememberMeServicesInterface $rememberMeService */
  122.                 $rememberMeService $this->container->get($definition);
  123.                 return $rememberMeService;
  124.             }
  125.         }
  126.         return null;
  127.     }
  128. }