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/MemberPortalUserProvider.php

<?php

namespace App\UserBundle\Security;

use App\UserBundle\Entity\UserLogin;
use App\UserBundle\Repository\UserLoginRepository;
use App\UserBundle\Service\MemberPortalUserService;
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 user_login accounts; requires a linked member firm in allowed status.
 */
class MemberPortalUserProvider implements UserProviderInterface, PasswordUpgraderInterface
{
    public function __construct(
        private readonly UserLoginRepository $userLoginRepository,
        private readonly MemberPortalUserService $memberPortalUserService,
    ) {
    }

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

        if (!$user instanceof UserLogin) {
            throw new UserNotFoundException(sprintf('Member portal user "%s" not found.', $identifier));
        }

        $context = $this->memberPortalUserService->buildLoginContext($user);
        if (!$context->hasMemberFirm()) {
            throw new UserNotFoundException(
                sprintf(
                    'Member portal user "%s" is not linked to a current CESA member firm.',
                    $identifier,
                ),
            );
        }

        $this->memberPortalUserService->applyLoginContext($user, $context);

        return $user;
    }

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

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

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

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

        $user->setPassword($newHashedPassword);
        $this->userLoginRepository->getEntityManager()->flush();
    }
}