Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/src/UserBundle/Security/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/UserBundle/Security/Echasl23UserProvider.php

<?php

namespace App\UserBundle\Security;

use App\UserBundle\Entity\UserOld;
use App\UserBundle\Repository\UserOldRepository;
use App\UserBundle\Service\Echasl23UserService;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

/**
 * Loads echasl23 users for authentication; inactive accounts are treated as not found.
 */
class Echasl23UserProvider implements UserProviderInterface, PasswordUpgraderInterface
{
    public function __construct(
        private readonly UserOldRepository $userOldRepository,
        private readonly Echasl23UserService $echasl23UserService,
    ) {
    }

    public function loadUserByIdentifier(string $identifier): UserInterface
    {
        $user = $this->userOldRepository->findOneBy(['username' => $identifier]);

        if (!$user instanceof UserOld || !$user->isActivated()) {
            throw new UserNotFoundException(sprintf('User "%s" not found or inactive.', $identifier));
        }

        $user->setResolvedRoles($this->echasl23UserService->resolveRoles($user));

        return $user;
    }

    public function refreshUser(UserInterface $user): UserInterface
    {
        if (!$user instanceof UserOld) {
            throw new UnsupportedUserException(sprintf('Invalid user class "%s".', $user::class));
        }

        return $this->loadUserByIdentifier($user->getUserIdentifier());
    }

    public function supportsClass(string $class): bool
    {
        return UserOld::class === $class || is_subclass_of($class, UserOld::class);
    }

    public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
    {
        if (!$user instanceof UserOld) {
            throw new UnsupportedUserException(sprintf('Invalid user class "%s".', $user::class));
        }

        $user->setPassword($newHashedPassword);
        $this->userOldRepository->save($user);
    }
}