<?php
// src/Security/UserProviderrovider.php
namespace App\Security;
use App\Entity\User;
use App\Exception\Api\UserNotEnabledException;
use App\Exception\Api\UserNotValidatedException;
use App\Repository\UserRepository;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* UserProvider.
*/
class UserApiProvider extends UserProvider
{
private UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function loadUserByIdentifier(string $identifier): UserInterface
{
$user = $this->userRepository->loadUserByIdentifier($identifier);
if (null === $user) {
throw new UserNotFoundException();
}
if (!$user->getEnabled()) {
throw new UserNotEnabledException();
}
if (!$user->isValidated()) {
throw new UserNotValidatedException();
}
return $user;
}
/**
* Refreshes the user after being reloaded from the session.
*
* When a user is logged in, at the beginning of each request, the
* User object is loaded from the session and then this method is
* called. Your job is to make sure the user's data is still fresh by,
* for example, re-querying for fresh User data.
*
* If your firewall is "stateless: true" (for a pure API), this
* method is not called.
*
* @return UserInterface
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
}
return $this->userRepository->refreshUser($user);
}
/**
* Tells Symfony to use this provider for this User class.
*/
public function supportsClass(string $class)
{
return User::class === $class || is_subclass_of($class, User::class);
}
/**
* Upgrades the encoded password of a user, typically for using a better hash algorithm.
*/
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
{
$this->userRepository->upgradePassword($user, $newEncodedPassword);
}
/** @deprecated since 5.3 use loadUserByIdentifier */
public function loadUserByUsername(string $username)
{
return $this->loadUserByIdentifier($username);
}
}