Skip to content

Commit 044985d

Browse files
committed
GraphQL-375: 1. Customer can get shipping/billing address data of any other customer
1 parent 6170993 commit 044985d

File tree

7 files changed

+453
-281
lines changed

7 files changed

+453
-281
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: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ class SetBillingAddressOnCart
2727
*/
2828
private $billingAddressManagement;
2929

30-
/**
31-
* @var AddressRepositoryInterface
32-
*/
33-
private $addressRepository;
34-
3530
/**
3631
* @var Address
3732
*/
@@ -42,26 +37,35 @@ class SetBillingAddressOnCart
4237
*/
4338
private $checkCustomerAccount;
4439

40+
/**
41+
* @var GetCustomerAddress
42+
*/
43+
private $getCustomerAddress;
44+
4545
/**
4646
* @param BillingAddressManagementInterface $billingAddressManagement
4747
* @param AddressRepositoryInterface $addressRepository
4848
* @param Address $addressModel
4949
* @param CheckCustomerAccount $checkCustomerAccount
50+
* @param GetCustomerAddress $getCustomerAddress
5051
*/
5152
public function __construct(
5253
BillingAddressManagementInterface $billingAddressManagement,
5354
AddressRepositoryInterface $addressRepository,
5455
Address $addressModel,
55-
CheckCustomerAccount $checkCustomerAccount
56+
CheckCustomerAccount $checkCustomerAccount,
57+
GetCustomerAddress $getCustomerAddress
5658
) {
5759
$this->billingAddressManagement = $billingAddressManagement;
5860
$this->addressRepository = $addressRepository;
5961
$this->addressModel = $addressModel;
6062
$this->checkCustomerAccount = $checkCustomerAccount;
63+
$this->getCustomerAddress = $getCustomerAddress;
6164
}
6265

6366
/**
6467
* @inheritdoc
68+
*
6569
* @param ContextInterface $context
6670
* @param CartInterface $cart
6771
* @param array $billingAddress
@@ -99,19 +103,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
99103
$billingAddress = $this->addressModel->addData($addressInput);
100104
} else {
101105
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
102-
103-
/** @var AddressInterface $customerAddress */
104-
$customerAddress = $this->addressRepository->getById($customerAddressId);
105-
106-
if ((int)$customerAddress->getCustomerId() !== $context->getUserId()) {
107-
throw new GraphQlAuthorizationException(
108-
__(
109-
'The current user cannot use address with ID "%customer_address_id"',
110-
['customer_address_id' => $customerAddressId]
111-
)
112-
);
113-
}
114-
106+
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
115107
$billingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
116108
}
117109

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
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\GraphQlAuthorizationException;
1312
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1413
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1514
use Magento\Quote\Api\Data\CartInterface;
1615
use Magento\Quote\Model\Quote\Address;
1716
use Magento\Quote\Model\ShippingAddressManagementInterface;
18-
use Magento\Customer\Api\AddressRepositoryInterface;
1917

2018
/**
2119
* Set single shipping address for a specified shopping cart
@@ -27,11 +25,6 @@ class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
2725
*/
2826
private $shippingAddressManagement;
2927

30-
/**
31-
* @var AddressRepositoryInterface
32-
*/
33-
private $addressRepository;
34-
3528
/**
3629
* @var Address
3730
*/
@@ -42,26 +35,32 @@ class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
4235
*/
4336
private $checkCustomerAccount;
4437

38+
/**
39+
* @var GetCustomerAddress
40+
*/
41+
private $getCustomerAddress;
42+
4543
/**
4644
* @param ShippingAddressManagementInterface $shippingAddressManagement
47-
* @param AddressRepositoryInterface $addressRepository
4845
* @param Address $addressModel
4946
* @param CheckCustomerAccount $checkCustomerAccount
47+
* @param GetCustomerAddress $getCustomerAddress
5048
*/
5149
public function __construct(
5250
ShippingAddressManagementInterface $shippingAddressManagement,
53-
AddressRepositoryInterface $addressRepository,
5451
Address $addressModel,
55-
CheckCustomerAccount $checkCustomerAccount
52+
CheckCustomerAccount $checkCustomerAccount,
53+
GetCustomerAddress $getCustomerAddress
5654
) {
5755
$this->shippingAddressManagement = $shippingAddressManagement;
58-
$this->addressRepository = $addressRepository;
5956
$this->addressModel = $addressModel;
6057
$this->checkCustomerAccount = $checkCustomerAccount;
58+
$this->getCustomerAddress = $getCustomerAddress;
6159
}
6260

6361
/**
6462
* @inheritdoc
63+
*
6564
* @param ContextInterface $context
6665
* @param CartInterface $cart
6766
* @param array $shippingAddresses
@@ -98,19 +97,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
9897
$shippingAddress = $this->addressModel->addData($addressInput);
9998
} else {
10099
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
101-
102-
/** @var AddressInterface $customerAddress */
103-
$customerAddress = $this->addressRepository->getById($customerAddressId);
104-
105-
if ((int)$customerAddress->getCustomerId() !== $context->getUserId()) {
106-
throw new GraphQlAuthorizationException(
107-
__(
108-
'The current user cannot use address with ID "%customer_address_id"',
109-
['customer_address_id' => $customerAddressId]
110-
)
111-
);
112-
}
113-
100+
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
114101
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
115102
}
116103

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

Lines changed: 1 addition & 9 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;
@@ -81,6 +72,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
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
}

0 commit comments

Comments
 (0)