Skip to content

Commit a79f94e

Browse files
ENGCOM-4295: Fix for getting shipping/billing address data of any other customer #376
- Merge Pull Request magento/graphql-ce#376 from magento/graphql-ce:375-unauthorized-quote-address-fix - Merged commits: 1. 5af7a1b 2. 6170993 3. 044985d 4. 1c9fad2 5. 9491c01 6. 1301a42 7. fa5613b 8. dde488f
2 parents 4fb0f1a + dde488f commit a79f94e

File tree

8 files changed

+467
-585
lines changed

8 files changed

+467
-585
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
16+
17+
/**
18+
* Get customer address. Throws exception if customer is not owner of address
19+
*/
20+
class GetCustomerAddress
21+
{
22+
/**
23+
* @var AddressRepositoryInterface
24+
*/
25+
private $addressRepository;
26+
27+
/**
28+
* @param AddressRepositoryInterface $addressRepository
29+
*/
30+
public function __construct(AddressRepositoryInterface $addressRepository)
31+
{
32+
$this->addressRepository = $addressRepository;
33+
}
34+
35+
/**
36+
* Get customer address. Throws exception if customer is not owner of address
37+
*
38+
* @param int $addressId
39+
* @param int $customerId
40+
* @return AddressInterface
41+
* @throws GraphQlAuthorizationException
42+
* @throws GraphQlNoSuchEntityException
43+
* @throws LocalizedException
44+
*/
45+
public function execute(int $addressId, int $customerId): AddressInterface
46+
{
47+
try {
48+
$customerAddress = $this->addressRepository->getById($addressId);
49+
} catch (NoSuchEntityException $e) {
50+
throw new GraphQlNoSuchEntityException(
51+
__('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
52+
);
53+
}
54+
55+
if ((int)$customerAddress->getCustomerId() !== $customerId) {
56+
throw new GraphQlAuthorizationException(
57+
__(
58+
'The current user cannot use address with ID "%address_id"',
59+
['address_id' => $addressId]
60+
)
61+
);
62+
}
63+
return $customerAddress;
64+
}
65+
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Customer\Api\Data\AddressInterface;
1110
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
1211
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1312
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -26,11 +25,6 @@ class SetBillingAddressOnCart
2625
*/
2726
private $billingAddressManagement;
2827

29-
/**
30-
* @var AddressRepositoryInterface
31-
*/
32-
private $addressRepository;
33-
3428
/**
3529
* @var Address
3630
*/
@@ -41,26 +35,40 @@ class SetBillingAddressOnCart
4135
*/
4236
private $checkCustomerAccount;
4337

38+
/**
39+
* @var GetCustomerAddress
40+
*/
41+
private $getCustomerAddress;
42+
4443
/**
4544
* @param BillingAddressManagementInterface $billingAddressManagement
4645
* @param AddressRepositoryInterface $addressRepository
4746
* @param Address $addressModel
4847
* @param CheckCustomerAccount $checkCustomerAccount
48+
* @param GetCustomerAddress $getCustomerAddress
4949
*/
5050
public function __construct(
5151
BillingAddressManagementInterface $billingAddressManagement,
5252
AddressRepositoryInterface $addressRepository,
5353
Address $addressModel,
54-
CheckCustomerAccount $checkCustomerAccount
54+
CheckCustomerAccount $checkCustomerAccount,
55+
GetCustomerAddress $getCustomerAddress
5556
) {
5657
$this->billingAddressManagement = $billingAddressManagement;
5758
$this->addressRepository = $addressRepository;
5859
$this->addressModel = $addressModel;
5960
$this->checkCustomerAccount = $checkCustomerAccount;
61+
$this->getCustomerAddress = $getCustomerAddress;
6062
}
6163

6264
/**
63-
* @inheritdoc
65+
* Set billing address for a specified shopping cart
66+
*
67+
* @param ContextInterface $context
68+
* @param CartInterface $cart
69+
* @param array $billingAddress
70+
* @return void
71+
* @throws GraphQlInputException
6472
*/
6573
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
6674
{
@@ -88,9 +96,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
8896
$billingAddress = $this->addressModel->addData($addressInput);
8997
} else {
9098
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
91-
92-
/** @var AddressInterface $customerAddress */
93-
$customerAddress = $this->addressRepository->getById($customerAddressId);
99+
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
94100
$billingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
95101
}
96102

app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Customer\Api\Data\AddressInterface;
1110
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
1211
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1312
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1413
use Magento\Quote\Api\Data\CartInterface;
1514
use Magento\Quote\Model\Quote\Address;
1615
use Magento\Quote\Model\ShippingAddressManagementInterface;
17-
use Magento\Customer\Api\AddressRepositoryInterface;
1816

1917
/**
2018
* Set single shipping address for a specified shopping cart
@@ -26,11 +24,6 @@ class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
2624
*/
2725
private $shippingAddressManagement;
2826

29-
/**
30-
* @var AddressRepositoryInterface
31-
*/
32-
private $addressRepository;
33-
3427
/**
3528
* @var Address
3629
*/
@@ -41,26 +34,36 @@ class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
4134
*/
4235
private $checkCustomerAccount;
4336

37+
/**
38+
* @var GetCustomerAddress
39+
*/
40+
private $getCustomerAddress;
41+
4442
/**
4543
* @param ShippingAddressManagementInterface $shippingAddressManagement
46-
* @param AddressRepositoryInterface $addressRepository
4744
* @param Address $addressModel
4845
* @param CheckCustomerAccount $checkCustomerAccount
46+
* @param GetCustomerAddress $getCustomerAddress
4947
*/
5048
public function __construct(
5149
ShippingAddressManagementInterface $shippingAddressManagement,
52-
AddressRepositoryInterface $addressRepository,
5350
Address $addressModel,
54-
CheckCustomerAccount $checkCustomerAccount
51+
CheckCustomerAccount $checkCustomerAccount,
52+
GetCustomerAddress $getCustomerAddress
5553
) {
5654
$this->shippingAddressManagement = $shippingAddressManagement;
57-
$this->addressRepository = $addressRepository;
5855
$this->addressModel = $addressModel;
5956
$this->checkCustomerAccount = $checkCustomerAccount;
57+
$this->getCustomerAddress = $getCustomerAddress;
6058
}
6159

6260
/**
6361
* @inheritdoc
62+
*
63+
* @param ContextInterface $context
64+
* @param CartInterface $cart
65+
* @param array $shippingAddresses
66+
* @throws GraphQlInputException
6467
*/
6568
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void
6669
{
@@ -87,9 +90,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
8790
$shippingAddress = $this->addressModel->addData($addressInput);
8891
} else {
8992
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
90-
91-
/** @var AddressInterface $customerAddress */
92-
$customerAddress = $this->addressRepository->getById($customerAddressId);
93+
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
9394
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
9495
}
9596

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Framework\Stdlib\ArrayManager;
15-
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1615
use Magento\Quote\Model\ShippingAddressManagementInterface;
1716
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1817
use Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface;
@@ -24,11 +23,6 @@
2423
*/
2524
class SetShippingAddressesOnCart implements ResolverInterface
2625
{
27-
/**
28-
* @var MaskedQuoteIdToQuoteIdInterface
29-
*/
30-
private $maskedQuoteIdToQuoteId;
31-
3226
/**
3327
* @var ShippingAddressManagementInterface
3428
*/
@@ -50,20 +44,17 @@ class SetShippingAddressesOnCart implements ResolverInterface
5044
private $setShippingAddressesOnCart;
5145

5246
/**
53-
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
5447
* @param ShippingAddressManagementInterface $shippingAddressManagement
5548
* @param GetCartForUser $getCartForUser
5649
* @param ArrayManager $arrayManager
5750
* @param SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
5851
*/
5952
public function __construct(
60-
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
6153
ShippingAddressManagementInterface $shippingAddressManagement,
6254
GetCartForUser $getCartForUser,
6355
ArrayManager $arrayManager,
6456
SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
6557
) {
66-
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
6758
$this->shippingAddressManagement = $shippingAddressManagement;
6859
$this->getCartForUser = $getCartForUser;
6960
$this->arrayManager = $arrayManager;
@@ -76,16 +67,16 @@ public function __construct(
7667
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
7768
{
7869
$shippingAddresses = $this->arrayManager->get('input/shipping_addresses', $args);
79-
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);
70+
$maskedCartId = (string) $this->arrayManager->get('input/cart_id', $args);
8071

8172
if (!$maskedCartId) {
8273
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
8374
}
75+
8476
if (!$shippingAddresses) {
8577
throw new GraphQlInputException(__('Required parameter "shipping_addresses" is missing'));
8678
}
8779

88-
$maskedCartId = $args['input']['cart_id'];
8980
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
9081

9182
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ input SetShippingMethodsOnCartInput {
6565

6666
input ShippingMethodForAddressInput {
6767
cart_address_id: Int!
68-
shipping_carrier_code: String!
69-
shipping_method_code: String!
68+
carrier_code: String!
69+
method_code: String!
7070
}
7171

7272
type SetBillingAddressOnCartOutput {
@@ -117,7 +117,7 @@ type CartAddress {
117117
country: CartAddressCountry
118118
telephone: String
119119
address_type: AdressTypeEnum
120-
selected_shipping_method: ShippingMethod
120+
selected_shipping_method: SelectedShippingMethod
121121
available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAdress\\AvailableShippingMethods")
122122
items_weight: Float
123123
customer_notes: String
@@ -139,15 +139,8 @@ type CartAddressCountry {
139139
label: String
140140
}
141141

142-
type ShippingMethod {
143-
code: String
144-
label: String
145-
free_shipping: Boolean!
146-
error_message: String
142+
type SelectedShippingMethod {
147143
amount: Float!
148-
base_amount: Float!
149-
amount_incl_tax: Float!
150-
base_amount_incl_tax: Float!
151144
}
152145

153146
type AvailableShippingMethod {

0 commit comments

Comments
 (0)