Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
9 / 27 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ApieUserProvider | |
33.33% |
9 / 27 |
|
25.00% |
1 / 4 |
46.85 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| refreshUser | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| supportsClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| loadUserByIdentifier | |
57.14% |
8 / 14 |
|
0.00% |
0 / 1 |
5.26 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\ApieBundle\Security; |
| 4 | |
| 5 | use Apie\Common\ApieFacade; |
| 6 | use Apie\Common\Interfaces\CheckLoginStatusInterface; |
| 7 | use Apie\Common\ValueObjects\DecryptedAuthenticatedUser; |
| 8 | use Apie\Core\Entities\EntityInterface; |
| 9 | use Apie\Core\Exceptions\EntityNotFoundException; |
| 10 | use Apie\Core\ValueObjects\Utils; |
| 11 | use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
| 12 | use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
| 13 | use Symfony\Component\Security\Core\User\UserInterface; |
| 14 | use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 15 | |
| 16 | /** |
| 17 | * @implements UserProviderInterface<ApieUserDecorator> |
| 18 | */ |
| 19 | class ApieUserProvider implements UserProviderInterface |
| 20 | { |
| 21 | public function __construct(private readonly ApieFacade $apieFacade) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @template T of EntityInterface |
| 27 | * @param ApieUserDecorator<T> $user |
| 28 | * @return ApieUserDecorator<T> |
| 29 | */ |
| 30 | public function refreshUser(UserInterface $user): ApieUserDecorator |
| 31 | { |
| 32 | if (!$user instanceof ApieUserDecorator) { |
| 33 | throw new UnsupportedUserException(get_debug_type($user) . ' is not supported'); |
| 34 | } |
| 35 | $entity = $user->getEntity(); |
| 36 | if ($entity instanceof CheckLoginStatusInterface) { |
| 37 | $user = $this->loadUserByIdentifier($user->getUserIdentifier()); |
| 38 | $entity = $user->getEntity(); |
| 39 | if ($entity instanceof CheckLoginStatusInterface && $entity->isDisabled()) { |
| 40 | throw new UserNotFoundException( |
| 41 | 'User . ' . Utils::toString($entity->getId()) . ' is disabled' |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | return $user; |
| 46 | } |
| 47 | |
| 48 | public function supportsClass(string $class): bool |
| 49 | { |
| 50 | return $class === ApieUserDecorator::class; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return ApieUserDecorator<EntityInterface> |
| 55 | */ |
| 56 | public function loadUserByIdentifier(string $identifier): ApieUserDecorator |
| 57 | { |
| 58 | $identifier = new DecryptedAuthenticatedUser($identifier); |
| 59 | $boundedContextId = $identifier->getBoundedContextId(); |
| 60 | try { |
| 61 | $entity = $this->apieFacade->find($identifier->getId(), $boundedContextId); |
| 62 | if ($entity instanceof CheckLoginStatusInterface && $entity->isDisabled()) { |
| 63 | throw new UserNotFoundException( |
| 64 | 'User . ' . Utils::toString($entity->getId()) . ' is disabled' |
| 65 | ); |
| 66 | } |
| 67 | } catch (EntityNotFoundException $notFound) { |
| 68 | throw new UserNotFoundException( |
| 69 | 'User ' . Utils::toString($identifier->getId()) . ' is removed, logging out', |
| 70 | 0, |
| 71 | $notFound |
| 72 | ); |
| 73 | } |
| 74 | return new ApieUserDecorator($identifier, $entity); |
| 75 | } |
| 76 | } |