<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eccube\Security\Core\User;
use Eccube\Entity\Customer;
use Eccube\Entity\Master\CustomerStatus;
use Eccube\Repository\CustomerRepository;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
class CustomerProvider implements UserProviderInterface
{
/**
* @var CustomerRepository
*/
protected $customerRepository;
public function __construct(CustomerRepository $customerRepository)
{
$this->customerRepository = $customerRepository;
}
/**
* @return UserInterface
*
* @throws UserNotFoundException
*
* @deprecated since Symfony 5.3, use loadUserByIdentifier() instead
*/
public function loadUserByUsername($username): Customer
{
$Customer = $this->customerRepository->findOneBy([
'email' => $username,
'Status' => CustomerStatus::REGULAR,
]);
if (null === $Customer) {
throw new UserNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
return $Customer;
}
/**
* Refreshes the user.
*
* It is up to the implementation to decide if the user data should be
* totally reloaded (e.g. from the database), or if the UserInterface
* object can just be merged into some internal array of users / identity
* map.
*
* @return UserInterface
*
* @throws UnsupportedUserException if the user is not supported
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof Customer) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
return $this->loadUserByUsername($user->getUsername());
}
/**
* Whether this provider supports the given user class.
*
* @param string $class
*
* @return bool
*/
public function supportsClass($class)
{
return Customer::class === $class || is_subclass_of($class, Customer::class);
}
}