2022-12-09 17:31:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Security;
|
|
|
|
|
|
|
|
use App\Entity\User;
|
2025-07-08 17:09:44 +02:00
|
|
|
use App\Service\SQLLoginService;
|
|
|
|
use App\SQLLogin\SQLLoginRequest;
|
2022-12-09 17:31:07 +01:00
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
|
2025-07-08 17:09:44 +02:00
|
|
|
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
2022-12-09 17:31:07 +01:00
|
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
|
|
|
2022-12-14 16:38:46 +01:00
|
|
|
class SQLLoginUserProvider implements UserProviderInterface
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
2025-07-08 17:09:44 +02:00
|
|
|
public function __construct(
|
|
|
|
private readonly RequestStack $requestStack,
|
|
|
|
private readonly SQLLoginService $sqlLoginService,
|
|
|
|
private readonly SQLLoginRequest $sqlLoginRequest
|
|
|
|
){
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
2025-07-08 17:09:44 +02:00
|
|
|
public function loadUserByIdentifier(string $identifier): UserInterface
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
2025-07-08 17:09:44 +02:00
|
|
|
$user = $this->sqlLoginService->fetchPasswordAndDatas($identifier);
|
2022-12-09 17:31:07 +01:00
|
|
|
|
2025-07-08 17:09:44 +02:00
|
|
|
$attributes = $user;
|
|
|
|
unset($attributes[$this->sqlLoginRequest->getPasswordColumnName()]);
|
2022-12-09 17:31:07 +01:00
|
|
|
|
2025-07-08 17:09:44 +02:00
|
|
|
if (empty($user[$this->sqlLoginRequest->getLoginColumnName()]) || empty($user[$this->sqlLoginRequest->getPasswordColumnName()])) {
|
|
|
|
throw new UserNotFoundException('email or password not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new User($user[$this->sqlLoginRequest->getLoginColumnName()], $user[$this->sqlLoginRequest->getPasswordColumnName()], $attributes);
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
2025-07-08 17:09:44 +02:00
|
|
|
public function refreshUser(UserInterface $user): UserInterface
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
|
|
|
if (!$user instanceof User) {
|
|
|
|
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
|
|
|
|
}
|
|
|
|
|
2025-07-08 17:09:44 +02:00
|
|
|
return $this->loadUserByIdentifier($user->getUserIdentifier());
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
2024-09-24 11:47:52 +02:00
|
|
|
public function supportsClass(string $class): bool
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
2025-07-08 17:09:44 +02:00
|
|
|
return User::class === $class || \is_subclass_of($class, User::class);
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
}
|