Skip to content

Commit 0cf59de

Browse files
committed
Added user permission check
1 parent 42b2358 commit 0cf59de

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddress.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1314
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
1516
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1617
use Magento\Quote\Api\CartRepositoryInterface;
1718
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
19+
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;
1820
use Magento\QuoteGraphQl\Model\Resolver\Address\AddressDataProvider;
1921

2022
/**
@@ -27,6 +29,11 @@ class CartAddress implements ResolverInterface
2729
*/
2830
private $addressDataProvider;
2931

32+
/**
33+
* @var IsCartMutationAllowedForCurrentUser
34+
*/
35+
private $isCartMutationAllowedForCurrentUser;
36+
3037
/**
3138
* @var CartRepositoryInterface
3239
*/
@@ -43,36 +50,49 @@ class CartAddress implements ResolverInterface
4350
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
4451
* @param CartRepositoryInterface $cartRepository
4552
* @param AddressDataProvider $addressDataProvider
53+
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
4654
*/
4755
public function __construct(
4856
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
4957
CartRepositoryInterface $cartRepository,
50-
AddressDataProvider $addressDataProvider
58+
AddressDataProvider $addressDataProvider,
59+
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
5160
) {
5261
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
5362
$this->cartRepository = $cartRepository;
5463
$this->addressDataProvider = $addressDataProvider;
64+
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
5565
}
5666

5767
/**
5868
* @inheritdoc
5969
*/
6070
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
6171
{
72+
/* The cart_id is used instead of the model because some parent resolvers do not work
73+
with cart model */
6274
if (!isset($value['cart_id'])) {
63-
// TODO: consider the possibility to pass quote model instead od quote ID
6475
throw new LocalizedException(__('"cart_id" value should be specified'));
6576
}
6677

78+
$maskedCartId = $value['cart_id'];
79+
6780
try {
68-
$quoteId = $this->maskedQuoteIdToQuoteId->execute($value['cart_id']);
81+
$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
6982
} catch (NoSuchEntityException $exception) {
7083
throw new GraphQlNoSuchEntityException(
71-
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $value['cart_id']])
84+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
7285
);
7386
}
7487

75-
// TODO: should we check customer permissions here as well?
88+
if (false === $this->isCartMutationAllowedForCurrentUser->execute($quoteId)) {
89+
throw new GraphQlAuthorizationException(
90+
__(
91+
'The current user cannot perform operations on cart "%masked_cart_id"',
92+
['masked_cart_id' => $maskedCartId]
93+
)
94+
);
95+
}
7696

7797
try {
7898
$quote = $this->cartRepository->get($quoteId);

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingMethod/SetShippingMethodsOnCart.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ class SetShippingMethodsOnCart implements ResolverInterface
7272
* @param ArrayManager $arrayManager
7373
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
7474
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
75+
* @param ShippingInformationManagementInterface $shippingInformationManagement
76+
* @param QuoteAddressFactory $quoteAddressFactory
77+
* @param QuoteAddressResource $quoteAddressResource
78+
* @param ShippingInformationFactory $shippingInformationFactory
7579
*/
7680
public function __construct(
7781
ArrayManager $arrayManager,
7882
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
7983
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser,
8084
ShippingInformationManagementInterface $shippingInformationManagement,
81-
QuoteAddressFactory $quoteAddressFacrory,
85+
QuoteAddressFactory $quoteAddressFactory,
8286
QuoteAddressResource $quoteAddressResource,
8387
ShippingInformationFactory $shippingInformationFactory
8488
) {
@@ -88,7 +92,7 @@ public function __construct(
8892
$this->shippingInformationManagement = $shippingInformationManagement;
8993

9094
$this->quoteAddressResource = $quoteAddressResource;
91-
$this->quoteAddressFactory = $quoteAddressFacrory;
95+
$this->quoteAddressFactory = $quoteAddressFactory;
9296
$this->shippingInformationFactory = $shippingInformationFactory;
9397
}
9498

0 commit comments

Comments
 (0)