-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 1.13: [CS][DX] Refactor [CS][DX] Refactor Apply Review fixes fix the build fallback order item test add contract tests Filter order items collection for visitors and shop users Add voter for the adjustments subresources Resolve createdByGuest on order fixtures
- Loading branch information
Showing
21 changed files
with
843 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...lius/Bundle/ApiBundle/Doctrine/QueryCollectionExtension/OrderItemsByShopUserExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Doctrine\QueryCollectionExtension; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
|
||
final class OrderItemsByShopUserExtension implements ContextAwareQueryCollectionExtensionInterface | ||
{ | ||
public function __construct(private UserContextInterface $userContext) | ||
{ | ||
} | ||
|
||
/** @param array<string, mixed> $context */ | ||
public function applyToCollection( | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
string $resourceClass, | ||
?string $operationName = null, | ||
array $context = [], | ||
): void { | ||
if (!is_a($resourceClass, OrderItemInterface::class, true)) { | ||
return; | ||
} | ||
|
||
$user = $this->userContext->getUser(); | ||
if (!$user instanceof ShopUserInterface) { | ||
return; | ||
} | ||
|
||
$rootAlias = $queryBuilder->getRootAliases()[0]; | ||
$orderParameterName = $queryNameGenerator->generateParameterName('order'); | ||
$customerJoinParameterName = $queryNameGenerator->generateJoinAlias('customer_join'); | ||
$customerParameterName = $queryNameGenerator->generateParameterName('customer'); | ||
|
||
$queryBuilder | ||
->leftJoin(sprintf('%s.order', $rootAlias), $orderParameterName) | ||
->leftJoin(sprintf('%s.customer', $orderParameterName), $customerJoinParameterName) | ||
->andWhere(sprintf('%s = :%s', $customerJoinParameterName, $customerParameterName)) | ||
->setParameter($customerParameterName, $user->getCustomer()->getId()) | ||
->addOrderBy(sprintf('%s.id', $rootAlias), 'ASC') | ||
; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ylius/Bundle/ApiBundle/Doctrine/QueryCollectionExtension/OrderItemsByVisitorExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Doctrine\QueryCollectionExtension; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
|
||
final class OrderItemsByVisitorExtension implements ContextAwareQueryCollectionExtensionInterface | ||
{ | ||
public function __construct(private UserContextInterface $userContext) | ||
{ | ||
} | ||
|
||
/** @param array<string, mixed> $context */ | ||
public function applyToCollection( | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
string $resourceClass, | ||
?string $operationName = null, | ||
array $context = [], | ||
): void { | ||
if (!is_a($resourceClass, OrderItemInterface::class, true)) { | ||
return; | ||
} | ||
|
||
$user = $this->userContext->getUser(); | ||
if ($user !== null) { | ||
return; | ||
} | ||
|
||
$rootAlias = $queryBuilder->getRootAliases()[0]; | ||
$orderParameterName = $queryNameGenerator->generateJoinAlias('order'); | ||
$customerParameterName = $queryNameGenerator->generateJoinAlias('customer'); | ||
$userParameterName = $queryNameGenerator->generateJoinAlias('user'); | ||
|
||
$queryBuilder | ||
->leftJoin(sprintf('%s.order', $rootAlias), $orderParameterName) | ||
->leftJoin(sprintf('%s.customer', $orderParameterName), $customerParameterName) | ||
->leftJoin(sprintf('%s.user', $customerParameterName), $userParameterName) | ||
->andWhere( | ||
$queryBuilder->expr()->andX( | ||
$queryBuilder->expr()->isNull($userParameterName), | ||
$queryBuilder->expr()->eq(sprintf('%s.createdByGuest', $orderParameterName), ':createdByGuest'), | ||
), | ||
) | ||
->setParameter('createdByGuest', true) | ||
->addOrderBy(sprintf('%s.id', $rootAlias), 'ASC') | ||
; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Sylius/Bundle/ApiBundle/Provider/AdjustmentOrderProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Provider; | ||
|
||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\OrderItemUnitInterface; | ||
use Sylius\Component\Order\Model\OrderInterface; | ||
|
||
/** @experimental */ | ||
final class AdjustmentOrderProvider implements AdjustmentOrderProviderInterface | ||
{ | ||
public function provide(AdjustmentInterface $adjustment): ?OrderInterface | ||
{ | ||
switch ($adjustment) { | ||
case $adjustment->getAdjustable() instanceof OrderInterface: | ||
return $adjustment->getOrder(); | ||
case $adjustment->getAdjustable() instanceof OrderItemInterface: | ||
return $adjustment->getOrderItem()->getOrder(); | ||
case $adjustment->getAdjustable() instanceof OrderItemUnitInterface: | ||
return $adjustment->getOrderItemUnit()->getOrderItem()->getOrder(); | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Sylius/Bundle/ApiBundle/Provider/AdjustmentOrderProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Provider; | ||
|
||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Order\Model\OrderInterface; | ||
|
||
/** @experimental */ | ||
interface AdjustmentOrderProviderInterface | ||
{ | ||
public function provide(AdjustmentInterface $adjustment): ?OrderInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Sylius/Bundle/ApiBundle/Resources/config/services/security.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
This file is part of the Sylius package. | ||
(c) Sylius Sp. z o.o. | ||
For the full copyright and license information, please view the LICENSE | ||
file that was distributed with this source code. | ||
--> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" | ||
> | ||
<services> | ||
<service id="sylius_api.security.voter.order" class="Sylius\Bundle\ApiBundle\Security\OrderAdjustmentsVoter"> | ||
<argument type="service" id="Sylius\Bundle\ApiBundle\Provider\AdjustmentOrderProviderInterface" /> | ||
<tag name="security.voter" /> | ||
</service> | ||
</services> | ||
</container> |
65 changes: 65 additions & 0 deletions
65
src/Sylius/Bundle/ApiBundle/Security/OrderAdjustmentsVoter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Security; | ||
|
||
use Doctrine\Common\Collections\Collection; | ||
use Sylius\Bundle\ApiBundle\Provider\AdjustmentOrderProviderInterface; | ||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
|
||
final class OrderAdjustmentsVoter extends Voter | ||
{ | ||
public function __construct(private AdjustmentOrderProviderInterface $adjustmentOrderProvider) | ||
{ | ||
} | ||
|
||
public const SYLIUS_ORDER_ADJUSTMENT = 'SYLIUS_ORDER_ADJUSTMENT'; | ||
|
||
protected function supports(string $attribute, mixed $subject): bool | ||
{ | ||
return $subject instanceof Collection; | ||
} | ||
|
||
public function supportsAttribute(string $attribute): bool | ||
{ | ||
return self::SYLIUS_ORDER_ADJUSTMENT === $attribute; | ||
} | ||
|
||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool | ||
{ | ||
$user = $token->getUser(); | ||
|
||
if ($subject === [] || $subject->isEmpty() || !$subject->first() instanceof AdjustmentInterface) { | ||
return true; | ||
} | ||
|
||
/** @var AdjustmentInterface $subjectItem */ | ||
foreach ($subject as $subjectItem) { | ||
if ($this->adjustmentOrderProvider->provide($subjectItem)) { | ||
/** @var OrderInterface $order */ | ||
$order = $this->adjustmentOrderProvider->provide($subjectItem); | ||
|
||
if (!$order->isCreatedByGuest() || $order->getUser()) { | ||
return $order->getUser() === $user; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.