hydra-sql/src/Security/SQLLoginUserProvider.php

50 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Security;
use App\Entity\User;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
class SQLLoginUserProvider implements UserProviderInterface
{
protected RequestStack $requestStack;
public function __construct(RequestStack $requestStack, SessionInterface $session)
{
$this->requestStack = $requestStack;
$this->session = $session;
}
public function loadUserByIdentifier(string $identifier, ?User $user): ?UserInterface
{
if ($user->getUserIdentifier() === $identifier) {
return $user;
}
return null;
}
public function loadUserByUsername(string $username): ?UserInterface
{
return $this->loadUserByIdentifier($username, null);
}
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
}
return $this->loadUserByIdentifier($user->getUserIdentifier(), $user);
}
public function supportsClass(string $class)
{
return User::class === $class || is_subclass_of($class, User::class);
}
}