src/ApiPlatform/Filter/CurrentUserFilter.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\ApiPlatform\Filter;
  3. use ApiPlatform\Api\FilterInterface;
  4. use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\BooleanFilterTrait;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
  7. use App\Entity\Agent;
  8. use App\Entity\Manager;
  9. use Doctrine\ORM\QueryBuilder;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  15. class CurrentUserFilter extends AbstractFilter implements FilterInterface
  16. {
  17.     use BooleanFilterTrait;
  18.     private const FIELD_NAME 'currentUser';
  19.     private TokenStorageInterface $tokenStorage;
  20.     public function __construct(
  21.         TokenStorageInterface $tokenStorage,
  22.         ManagerRegistry $managerRegistry,
  23.         ?RequestStack $requestStack null,
  24.         LoggerInterface $logger null,
  25.         array $properties null,
  26.         NameConverterInterface $nameConverter null
  27.     ) {
  28.         parent::__construct($managerRegistry$requestStack$logger$properties$nameConverter);
  29.         $this->tokenStorage $tokenStorage;
  30.     }
  31.     protected function filterProperty(
  32.         string $property,
  33.         $value,
  34.         QueryBuilder $queryBuilder,
  35.         QueryNameGeneratorInterface $queryNameGenerator,
  36.         string $resourceClass,
  37.         string $operationName null
  38.     ): void {
  39.         if (self::FIELD_NAME !== $property) {
  40.             return;
  41.         }
  42.         $value $this->normalizeValue($value$property);
  43.         if (null === $value || $value === false) {
  44.             return;
  45.         }
  46.         if ($this->tokenStorage->getToken() !== null && $this->tokenStorage->getToken()->getUser() instanceof Manager) {
  47.             return;
  48.         }
  49.         $valueParameter $queryNameGenerator->generateParameterName(self::FIELD_NAME);
  50.         $alias $queryBuilder->getRootAliases()[0];
  51.         $queryBuilder->andWhere(sprintf(
  52.             '%s.id != :%s ',
  53.             $alias,
  54.             $valueParameter,
  55.         ))->setParameter($valueParameter$this->getUser()->getId());
  56.     }
  57.     public function getDescription(string $resourceClass): array
  58.     {
  59.         return [
  60.             'currentUser' => [
  61.                 'property' => null,
  62.                 'type' => 'boolean',
  63.                 'required' => false,
  64.                 'swagger' => [
  65.                     'description' => 'Filter current user from agents list.',
  66.                     'name' => 'Custom name to use in the Swagger documentation',
  67.                     'type' => 'Will appear below the name in the Swagger documentation',
  68.                 ],
  69.             ]
  70.         ];
  71.     }
  72.     private function getUser(): ?Agent
  73.     {
  74.         if (!$token $this->tokenStorage->getToken()) {
  75.             return null;
  76.         }
  77.         $user $token->getUser();
  78.         if (!$user instanceof Agent) {
  79.             return null;
  80.         }
  81.         return $user;
  82.     }
  83. }