Skip to content

Commit 9041bdb

Browse files
committed
GraphQL: Add missing module depenency and add input checker on address update
1 parent 47adffd commit 9041bdb

File tree

3 files changed

+72
-51
lines changed

3 files changed

+72
-51
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/Address.php

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11-
use Magento\Customer\Api\Data\CustomerInterface;
1211
use Magento\Customer\Api\CustomerRepositoryInterface;
1312
use Magento\Customer\Api\AddressRepositoryInterface;
13+
use Magento\Customer\Api\AddressMetadataManagementInterface;
1414
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1515
use Magento\Customer\Api\Data\AddressInterface;
1616
use Magento\Framework\Api\DataObjectHelper;
@@ -19,30 +19,23 @@
1919
use Magento\Framework\GraphQl\Query\ResolverInterface;
2020
use Magento\CustomerGraphQl\Model\Resolver\Address\AddressDataProvider;
2121
use Magento\Eav\Model\Config;
22+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
23+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
24+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
25+
use Magento\Framework\Exception\NoSuchEntityException;
2226

2327
/**
2428
* Customers Address, used for GraphQL request processing.
2529
*/
2630
class Address implements ResolverInterface
2731
{
28-
/**
29-
* Input data key
30-
*/
31-
const CUSTOM_ATTRIBUTE_KEY = 'custom_attributes';
32-
const EXTENSION_ATTRIBUTE_KEY = 'extension_attributes';
33-
3432
/**
3533
* Mutation Address type
3634
*/
3735
const MUTATION_ADDRESS_CREATE = 'customerAddressCreate';
3836
const MUTATION_ADDRESS_UPDATE = 'customerAddressUpdate';
3937
const MUTATION_ADDRESS_DELETE = 'customerAddressDelete';
4038

41-
/**
42-
* @var CustomerRepositoryInterface
43-
*/
44-
private $customerRepositoryInterface;
45-
4639
/**
4740
* @var AddressRepositoryInterface
4841
*/
@@ -64,7 +57,7 @@ class Address implements ResolverInterface
6457
private $addressDataProvider;
6558

6659
/**
67-
* @var \Magento\Framework\Api\DataObjectHelper
60+
* @var DataObjectHelper
6861
*/
6962
private $dataObjectHelper;
7063

@@ -74,29 +67,26 @@ class Address implements ResolverInterface
7467
private $addressAttributes;
7568

7669
/**
77-
* @param CustomerRepositoryInterface $customerRepositoryInterface
7870
* @param AddressRepositoryInterface $addressRepositoryInterface
7971
* @param AddressInterfaceFactory $addressInterfaceFactory
8072
* @param Config $eavConfig
8173
* @param AddressDataProvider $addressDataProvider
8274
* @param DataObjectHelper $dataObjectHelper
8375
*/
8476
public function __construct(
85-
CustomerRepositoryInterface $customerRepositoryInterface,
8677
AddressRepositoryInterface $addressRepositoryInterface,
8778
AddressInterfaceFactory $addressInterfaceFactory,
8879
Config $eavConfig,
8980
AddressDataProvider $addressDataProvider,
9081
DataObjectHelper $dataObjectHelper
9182
) {
92-
$this->customerRepositoryInterface = $customerRepositoryInterface;
9383
$this->addressRepositoryInterface = $addressRepositoryInterface;
9484
$this->addressInterfaceFactory = $addressInterfaceFactory;
9585
$this->eavConfig = $eavConfig;
9686
$this->addressDataProvider = $addressDataProvider;
9787
$this->dataObjectHelper = $dataObjectHelper;
9888
$this->addressAttributes = $this->eavConfig->getEntityAttributes(
99-
\Magento\Customer\Api\AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
89+
AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
10090
);
10191
}
10292

@@ -112,40 +102,58 @@ public function resolve(
112102
) {
113103
/** @var \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context */
114104
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
115-
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
105+
throw new GraphQlAuthorizationException(
116106
__(
117107
'Current customer does not have access to the resource "%1"',
118-
[\Magento\Customer\Model\Customer::ENTITY]
108+
[AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS]
119109
)
120110
);
121111
}
122-
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
123-
$customer = $this->customerRepositoryInterface->getById($context->getUserId());
112+
$customerId = $context->getUserId();
124113
switch ($field->getName()) {
125114
case self::MUTATION_ADDRESS_CREATE:
126115
return $this->addressDataProvider->processCustomerAddress(
127-
$this->processCustomerAddressCreate($customer, $args['input'])
116+
$this->processCustomerAddressCreate($customerId, $args['input'])
128117
);
129118
case self::MUTATION_ADDRESS_UPDATE:
130119
return $this->addressDataProvider->processCustomerAddress(
131-
$this->processCustomerAddressUpdate($customer, $args['id'], $args['input'])
120+
$this->processCustomerAddressUpdate($customerId, $args['id'], $args['input'])
132121
);
133122
case self::MUTATION_ADDRESS_DELETE:
134-
return $this->processCustomerAddressDelete($customer, $args['id']);
123+
return $this->processCustomerAddressDelete($customerId, $args['id']);
135124
default:
136125
return [];
137126
}
138127
}
139128

140129
/**
141-
* Get input address attribute errors
130+
* Get new address attribute input errors
131+
*
132+
* @param array $addressInput
133+
* @return bool|string
134+
*/
135+
private function getNewAddressInputError(array $addressInput)
136+
{
137+
foreach ($this->addressAttributes as $attributeName => $attributeInfo) {
138+
if ($attributeInfo->getIsRequired()
139+
&& (!isset($addressInput[$attributeName]) || empty($addressInput[$attributeName]))) {
140+
return $attributeName;
141+
}
142+
}
143+
return false;
144+
}
145+
146+
/**
147+
* Get update address attribute input errors
148+
*
142149
* @param array $addressInput
143150
* @return bool|string
144151
*/
145-
private function getAddressInputError(array $addressInput)
152+
private function getUpdateAddressInputError(array $addressInput)
146153
{
147154
foreach ($this->addressAttributes as $attributeName => $attributeInfo) {
148-
if ($attributeInfo->getIsRequired() && !isset($addressInput[$attributeName])) {
155+
if ($attributeInfo->getIsRequired()
156+
&& (isset($addressInput[$attributeName]) && empty($addressInput[$attributeName]))) {
149157
return $attributeName;
150158
}
151159
}
@@ -154,6 +162,7 @@ private function getAddressInputError(array $addressInput)
154162

155163
/**
156164
* Add $addressInput array information to a $address object
165+
*
157166
* @param AddressInterface $address
158167
* @param array $addressInput
159168
* @return AddressInterface
@@ -170,16 +179,17 @@ private function fillAddress(AddressInterface $address, array $addressInput) : A
170179

171180
/**
172181
* Process customer address create
173-
* @param CustomerInterface $customer
182+
*
183+
* @param int $customerId
174184
* @param array $addressInput
175185
* @return AddressInterface
176-
* @throws \Magento\Framework\GraphQl\Exception\GraphQlInputException
186+
* @throws GraphQlInputException
177187
*/
178-
private function processCustomerAddressCreate(CustomerInterface $customer, array $addressInput) : AddressInterface
188+
private function processCustomerAddressCreate($customerId, array $addressInput) : AddressInterface
179189
{
180-
$errorInput = $this->getAddressInputError($addressInput);
190+
$errorInput = $this->getNewAddressInputError($addressInput);
181191
if ($errorInput) {
182-
throw new \Magento\Framework\GraphQl\Exception\GraphQlInputException(
192+
throw new GraphQlInputException(
183193
__('Required parameter %1 is missing', [$errorInput])
184194
);
185195
}
@@ -188,69 +198,78 @@ private function processCustomerAddressCreate(CustomerInterface $customer, array
188198
$this->addressInterfaceFactory->create(),
189199
$addressInput
190200
);
191-
$newAddress->setCustomerId($customer->getId());
201+
$newAddress->setCustomerId($customerId);
192202
return $this->addressRepositoryInterface->save($newAddress);
193203
}
194204

195205
/**
196206
* Process customer address update
197-
* @param CustomerInterface $customer
207+
*
208+
* @param int $customerId
198209
* @param int $addressId
199210
* @param array $addressInput
200211
* @return AddressInterface
201-
* @throws \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException
202-
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
212+
* @throws GraphQlAuthorizationException
213+
* @throws GraphQlNoSuchEntityException
214+
* @throws GraphQlInputException
203215
*/
204-
private function processCustomerAddressUpdate(CustomerInterface $customer, $addressId, array $addressInput)
216+
private function processCustomerAddressUpdate($customerId, $addressId, array $addressInput)
205217
{
206218
try {
207219
/** @var AddressInterface $address */
208220
$address = $this->addressRepositoryInterface->getById($addressId);
209-
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
210-
throw new \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException(
221+
} catch (NoSuchEntityException $exception) {
222+
throw new GraphQlNoSuchEntityException(
211223
__('Address id %1 does not exist.', [$addressId])
212224
);
213225
}
214-
if ($address->getCustomerId() != $customer->getId()) {
215-
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
226+
if ($address->getCustomerId() != $customerId) {
227+
throw new GraphQlAuthorizationException(
216228
__('Current customer does not have permission to update address id %1', [$addressId])
217229
);
218230
}
231+
$errorInput = $this->getUpdateAddressInputError($addressInput);
232+
if ($errorInput) {
233+
throw new GraphQlInputException(
234+
__('Required parameter %1 is missing', [$errorInput])
235+
);
236+
}
219237
return $this->addressRepositoryInterface->save(
220238
$this->fillAddress($address, $addressInput)
221239
);
222240
}
223241

224242
/**
225243
* Process customer address delete
226-
* @param CustomerInterface $customer
244+
*
245+
* @param int $customerId
227246
* @param int $addressId
228247
* @return bool
229-
* @throws \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException
230-
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
248+
* @throws GraphQlAuthorizationException
249+
* @throws GraphQlNoSuchEntityException
231250
*/
232-
private function processCustomerAddressDelete(CustomerInterface $customer, $addressId)
251+
private function processCustomerAddressDelete($customerId, $addressId)
233252
{
234253
try {
235254
/** @var AddressInterface $address */
236255
$address = $this->addressRepositoryInterface->getById($addressId);
237-
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
238-
throw new \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException(
256+
} catch (NoSuchEntityException $exception) {
257+
throw new GraphQlNoSuchEntityException(
239258
__('Address id %1 does not exist.', [$addressId])
240259
);
241260
}
242-
if ($address->getCustomerId() != $customer->getId()) {
243-
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
261+
if ($customerId != $address->getCustomerId()) {
262+
throw new GraphQlAuthorizationException(
244263
__('Current customer does not have permission to delete address id %1', [$addressId])
245264
);
246265
}
247266
if ($address->isDefaultBilling()) {
248-
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
267+
throw new GraphQlAuthorizationException(
249268
__('Customer Address %1 is set as default billing address and can not be deleted', [$addressId])
250269
);
251270
}
252271
if ($address->isDefaultShipping()) {
253-
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
272+
throw new GraphQlAuthorizationException(
254273
__('Customer Address %1 is set as default shipping address and can not be deleted', [$addressId])
255274
);
256275
}

app/code/Magento/CustomerGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"magento/module-customer": "*",
88
"magento/module-authorization": "*",
99
"magento/module-integration": "*",
10+
"magento/module-eav": "*",
1011
"magento/framework": "*"
1112
},
1213
"suggest": {

app/code/Magento/CustomerGraphQl/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<module name="Magento_Customer"/>
1212
<module name="Magento_Authorization"/>
1313
<module name="Magento_GraphQl"/>
14+
<module name="Magento_Eav"/>
1415
</sequence>
1516
</module>
1617
</config>

0 commit comments

Comments
 (0)