|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Paweł Jędrzejewski |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\Bundle\ApiBundle\Doctrine\QueryItemExtension; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; |
| 17 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
| 18 | +use Doctrine\ORM\QueryBuilder; |
| 19 | +use Sylius\Bundle\ApiBundle\Context\UserContextInterface; |
| 20 | +use Sylius\Component\Core\Model\AdminUserInterface; |
| 21 | +use Sylius\Component\Core\Model\OrderInterface; |
| 22 | +use Sylius\Component\Core\Model\ShopUserInterface; |
| 23 | +use Symfony\Component\HttpFoundation\Request; |
| 24 | +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
| 25 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 26 | + |
| 27 | +final class OrderDeleteMethodExtension implements QueryItemExtensionInterface |
| 28 | +{ |
| 29 | + /** @var UserContextInterface */ |
| 30 | + private $userContext; |
| 31 | + |
| 32 | + public function __construct(UserContextInterface $userContext) |
| 33 | + { |
| 34 | + $this->userContext = $userContext; |
| 35 | + } |
| 36 | + |
| 37 | + public function applyToItem( |
| 38 | + QueryBuilder $queryBuilder, |
| 39 | + QueryNameGeneratorInterface $queryNameGenerator, |
| 40 | + string $resourceClass, |
| 41 | + array $identifiers, |
| 42 | + string $operationName = null, |
| 43 | + array $context = [] |
| 44 | + ) { |
| 45 | + $operationName = strtoupper($operationName); |
| 46 | + |
| 47 | + if (!is_a($resourceClass, OrderInterface::class, true)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if ($operationName !== Request::METHOD_DELETE) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $rootAlias = $queryBuilder->getRootAliases()[0]; |
| 56 | + $user = $this->userContext->getUser(); |
| 57 | + |
| 58 | + $this->applyToItemForDeleteMethod($user, $queryBuilder, $operationName, $rootAlias); |
| 59 | + } |
| 60 | + |
| 61 | + private function applyToItemForDeleteMethod( |
| 62 | + ?UserInterface $user, |
| 63 | + QueryBuilder $queryBuilder, |
| 64 | + string $operationName, |
| 65 | + string $rootAlias |
| 66 | + ): void { |
| 67 | + if ($user === null) { |
| 68 | + $queryBuilder |
| 69 | + ->andWhere(sprintf('%s.customer IS NULL', $rootAlias)) |
| 70 | + ->andWhere(sprintf('%s.state = :state', $rootAlias)) |
| 71 | + ->setParameter('state', OrderInterface::STATE_CART) |
| 72 | + ; |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if ($user instanceof ShopUserInterface && in_array('ROLE_API_ACCESS', $user->getRoles(), true)) { |
| 78 | + $queryBuilder |
| 79 | + ->andWhere(sprintf('%s.customer = :customer', $rootAlias)) |
| 80 | + ->setParameter('customer', $user->getCustomer()->getId()) |
| 81 | + ->andWhere(sprintf('%s.state = :state', $rootAlias)) |
| 82 | + ->setParameter('state', OrderInterface::STATE_CART) |
| 83 | + ; |
| 84 | + |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if ($user instanceof AdminUserInterface && in_array('ROLE_API_ACCESS', $user->getRoles(), true)) { |
| 89 | + $queryBuilder |
| 90 | + ->andWhere(sprintf('%s.state = :state', $rootAlias)) |
| 91 | + ->setParameter('state', OrderInterface::STATE_CART) |
| 92 | + ; |
| 93 | + |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + throw new AccessDeniedHttpException('Requested method is not allowed.'); |
| 98 | + } |
| 99 | +} |
0 commit comments