vendor/pimcore/customer-management-framework-bundle/src/Security/UserProvider/CustomerObjectUserProvider.php line 105

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 Commercial License (PCL)
  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 PCL
  13.  */
  14. namespace CustomerManagementFrameworkBundle\Security\UserProvider;
  15. use CustomerManagementFrameworkBundle\CustomerProvider\CustomerProviderInterface;
  16. use CustomerManagementFrameworkBundle\Model\CustomerInterface;
  17. use Pimcore\Model\DataObject\AbstractObject;
  18. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  19. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. use Symfony\Component\Security\Core\User\UserProviderInterface;
  22. class CustomerObjectUserProvider implements UserProviderInterface
  23. {
  24.     /**
  25.      * @var CustomerProviderInterface
  26.      */
  27.     private $customerProvider;
  28.     /**
  29.      * @var string
  30.      */
  31.     protected $usernameField 'email';
  32.     public function __construct(
  33.         CustomerProviderInterface $customerProvider,
  34.         string $usernameField 'email'
  35.     ) {
  36.         $this->customerProvider $customerProvider;
  37.         $this->usernameField $usernameField;
  38.     }
  39.     /**
  40.      * @inheritdoc
  41.      *
  42.      * @return UserInterface
  43.      */
  44.     public function loadUserByIdentifier(string $identifier)//: UserInterface
  45.     {
  46.         $list $this->customerProvider->getList();
  47.         $list->setCondition(sprintf('%s = ?'$this->usernameField), $identifier);
  48.         $this->customerProvider->addActiveCondition($list);
  49.         /** @var CustomerInterface|false $customer */
  50.         $customer $list->current();
  51.         if (!$customer instanceof UserInterface) {
  52.             throw new UserNotFoundException(sprintf('Customer "%s" was not found'$identifier));
  53.         }
  54.         return $customer;
  55.     }
  56.     /**
  57.      * @deprecated use loadUserByIdentifier() instead.
  58.      *
  59.      * @inheritdoc
  60.      */
  61.     public function loadUserByUsername($username)
  62.     {
  63.         return $this->loadUserByIdentifier($username);
  64.     }
  65.     /**
  66.      * @inheritdoc
  67.      *
  68.      * @return UserInterface
  69.      */
  70.     public function refreshUser(UserInterface $user)
  71.     {
  72.         $class $this->customerProvider->getCustomerClassName();
  73.         if (!$user instanceof $class || !$user instanceof AbstractObject) {
  74.             throw new UnsupportedUserException();
  75.         }
  76.         $customer $this->customerProvider->getById($user->getId(), true);
  77.         if (!$customer instanceof UserInterface) {
  78.             throw new UserNotFoundException(sprintf('Customer "%s" was not found'$user->getId()));
  79.         }
  80.         return $customer;
  81.     }
  82.     /**
  83.      * @inheritdoc
  84.      *
  85.      * @return bool
  86.      */
  87.     public function supportsClass($class)
  88.     {
  89.         return $class === $this->customerProvider->getCustomerClassName();
  90.     }
  91. }