Расширения ORM / Extension

QueryCollectionExtensionInterface

QueryItemExtensionInterface

Настройки / Использование

services.yaml

parameters: services: App\Extension\CurrentUserExtension: tags: - { name: api_platform.doctrine.orm.query_extension.collection } - { name: api_platform.doctrine.orm.query_extension.item }
<?php namespace App\Extension; use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; use App\Entity\Post; use App\Entity\User; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; final class CurrentUserExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface { private $tokenStorage; private $authorizationChecker; const ENTITIES = [ Post::class, User::class ]; public function __construct(TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $checker) { $this->tokenStorage = $tokenStorage; $this->authorizationChecker = $checker; } /** * {@inheritdoc} */ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) { $this->addWhere($queryBuilder, $resourceClass); } /** * {@inheritdoc} */ public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []) { $this->addWhere($queryBuilder, $resourceClass); } /** * * @param QueryBuilder $queryBuilder * @param string $resourceClass */ private function addWhere(QueryBuilder $queryBuilder, string $resourceClass) { $user = $this->tokenStorage->getToken()->getUser(); if ($user instanceof User && in_array($resourceClass, self::ENTITIES) && !$this->authorizationChecker->isGranted('ROLE_ADMIN') ) { $rootAlias = $queryBuilder->getRootAliases()[0]; $queryBuilder->andWhere(sprintf('%s.user = :current_user', $rootAlias)); $queryBuilder->setParameter('current_user', $user->getId()); } } }

Symfony / API Platform