Skip to content

Commit 5c0bcfd

Browse files
committed
Refactored flow for new authorization check logic
1 parent 1c470a0 commit 5c0bcfd

File tree

2 files changed

+19
-82
lines changed

2 files changed

+19
-82
lines changed

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

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@
88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

1010
use Magento\Framework\Exception\LocalizedException;
11-
use Magento\Framework\Exception\NoSuchEntityException;
1211
use Magento\Framework\GraphQl\Config\Element\Field;
13-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
14-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1512
use Magento\Framework\GraphQl\Query\ResolverInterface;
1613
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1714
use Magento\Quote\Api\CartRepositoryInterface;
1815
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
19-
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;
2016
use Magento\QuoteGraphQl\Model\Resolver\Address\AddressDataProvider;
2117

2218
/**
@@ -29,11 +25,6 @@ class CartAddress implements ResolverInterface
2925
*/
3026
private $addressDataProvider;
3127

32-
/**
33-
* @var IsCartMutationAllowedForCurrentUser
34-
*/
35-
private $isCartMutationAllowedForCurrentUser;
36-
3728
/**
3829
* @var CartRepositoryInterface
3930
*/
@@ -50,58 +41,28 @@ class CartAddress implements ResolverInterface
5041
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
5142
* @param CartRepositoryInterface $cartRepository
5243
* @param AddressDataProvider $addressDataProvider
53-
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
5444
*/
5545
public function __construct(
5646
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
5747
CartRepositoryInterface $cartRepository,
58-
AddressDataProvider $addressDataProvider,
59-
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
48+
AddressDataProvider $addressDataProvider
6049
) {
6150
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
6251
$this->cartRepository = $cartRepository;
6352
$this->addressDataProvider = $addressDataProvider;
64-
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
6553
}
6654

6755
/**
6856
* @inheritdoc
6957
*/
7058
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
7159
{
72-
/* The cart_id is used instead of the model because some parent resolvers do not work
73-
with cart model */
74-
if (!isset($value['cart_id'])) {
75-
throw new LocalizedException(__('"cart_id" value should be specified'));
60+
if (!isset($value['model'])) {
61+
throw new LocalizedException(__('"model" value should be specified'));
7662
}
7763

78-
$maskedCartId = $value['cart_id'];
79-
80-
try {
81-
$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
82-
} catch (NoSuchEntityException $exception) {
83-
throw new GraphQlNoSuchEntityException(
84-
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
85-
);
86-
}
87-
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-
}
96-
97-
try {
98-
$quote = $this->cartRepository->get($quoteId);
99-
} catch (NoSuchEntityException $exception) {
100-
throw new GraphQlNoSuchEntityException(
101-
__('Could not find a cart with ID "%quote_id"', ['quote_id' => $quoteId])
102-
);
103-
}
64+
$cart = $value['model'];
10465

105-
return $this->addressDataProvider->getCartAddresses($quote);
66+
return $this->addressDataProvider->getCartAddresses($cart);
10667
}
10768
}

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

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212
use Magento\Framework\Exception\InputException;
1313
use Magento\Framework\Exception\NoSuchEntityException;
1414
use Magento\Framework\Exception\StateException;
15-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1615
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1716
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1817
use Magento\Framework\GraphQl\Query\ResolverInterface;
1918
use Magento\Framework\GraphQl\Config\Element\Field;
2019
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2120
use Magento\Framework\Stdlib\ArrayManager;
22-
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
23-
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;
2421
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
2522
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
2623
use Magento\Checkout\Model\ShippingInformationFactory;
24+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
2725

2826
/**
2927
* Class SetShippingMethodsOnCart
@@ -47,20 +45,15 @@ class SetShippingMethodsOnCart implements ResolverInterface
4745
*/
4846
private $quoteAddressResource;
4947

50-
/**
51-
* @var MaskedQuoteIdToQuoteIdInterface
52-
*/
53-
private $maskedQuoteIdToQuoteId;
54-
5548
/**
5649
* @var ArrayManager
5750
*/
5851
private $arrayManager;
5952

6053
/**
61-
* @var IsCartMutationAllowedForCurrentUser
54+
* @var GetCartForUser
6255
*/
63-
private $isCartMutationAllowedForCurrentUser;
56+
private $getCartForUser;
6457

6558
/**
6659
* @var ShippingInformationManagementInterface
@@ -70,27 +63,23 @@ class SetShippingMethodsOnCart implements ResolverInterface
7063
/**
7164
* SetShippingMethodsOnCart constructor.
7265
* @param ArrayManager $arrayManager
73-
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
74-
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
66+
* @param GetCartForUser $getCartForUser
7567
* @param ShippingInformationManagementInterface $shippingInformationManagement
7668
* @param QuoteAddressFactory $quoteAddressFactory
7769
* @param QuoteAddressResource $quoteAddressResource
7870
* @param ShippingInformationFactory $shippingInformationFactory
7971
*/
8072
public function __construct(
8173
ArrayManager $arrayManager,
82-
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
83-
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser,
74+
GetCartForUser $getCartForUser,
8475
ShippingInformationManagementInterface $shippingInformationManagement,
8576
QuoteAddressFactory $quoteAddressFactory,
8677
QuoteAddressResource $quoteAddressResource,
8778
ShippingInformationFactory $shippingInformationFactory
8879
) {
8980
$this->arrayManager = $arrayManager;
90-
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
91-
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
81+
$this->getCartForUser = $getCartForUser;
9282
$this->shippingInformationManagement = $shippingInformationManagement;
93-
9483
$this->quoteAddressResource = $quoteAddressResource;
9584
$this->quoteAddressFactory = $quoteAddressFactory;
9685
$this->shippingInformationFactory = $shippingInformationFactory;
@@ -111,34 +100,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
111100
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
112101
}
113102

114-
$shippingMethod = reset($shippingMethods); // TODO: provide implementation for multishipping
103+
$shippingMethod = reset($shippingMethods);
115104

116105
if (!$shippingMethod['cart_address_id']) {
117106
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
118107
}
119-
if (!$shippingMethod['shipping_carrier_code']) { // FIXME: check the E_WARNING here
108+
if (!$shippingMethod['shipping_carrier_code']) {
120109
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
121110
}
122-
if (!$shippingMethod['shipping_method_code']) { // FIXME: check the E_WARNING here
111+
if (!$shippingMethod['shipping_method_code']) {
123112
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
124113
}
125114

126-
try {
127-
$cartId = $this->maskedQuoteIdToQuoteId->execute((string) $maskedCartId);
128-
} catch (NoSuchEntityException $exception) {
129-
throw new GraphQlNoSuchEntityException(
130-
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
131-
);
132-
}
133-
134-
if (false === $this->isCartMutationAllowedForCurrentUser->execute($cartId)) {
135-
throw new GraphQlAuthorizationException(
136-
__(
137-
'The current user cannot perform operations on cart "%masked_cart_id"',
138-
['masked_cart_id' => $maskedCartId]
139-
)
140-
);
141-
}
115+
$userId = $context->getUserId();
116+
$cart = $this->getCartForUser->execute((string) $maskedCartId, $userId);
142117

143118
$quoteAddress = $this->quoteAddressFactory->create();
144119
$this->quoteAddressResource->load($quoteAddress, $shippingMethod['cart_address_id']);
@@ -153,7 +128,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
153128
$shippingInformation->setShippingMethodCode($shippingMethod['shipping_method_code']);
154129

155130
try {
156-
$this->shippingInformationManagement->saveAddressInformation($cartId, $shippingInformation);
131+
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
157132
} catch (NoSuchEntityException $exception) {
158133
throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
159134
} catch (StateException $exception) {
@@ -164,7 +139,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
164139

165140
return [
166141
'cart' => [
167-
'cart_id' => $maskedCartId
142+
'cart_id' => $maskedCartId,
143+
'model' => $cart
168144
]
169145
];
170146
}

0 commit comments

Comments
 (0)