Skip to content

Commit dba9800

Browse files
committed
magento/graphql-ce#271: [My Account] Add support of Customer attributes
- Customer attributes validation added
1 parent 7b1cd79 commit dba9800

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Customer\Api\CustomerMetadataManagementInterface;
11+
use Magento\Eav\Model\Config;
12+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
13+
14+
/**
15+
* Get allowed address attributes
16+
*/
17+
class GetAllowedCustomerAttributes
18+
{
19+
/**
20+
* @var Config
21+
*/
22+
private $eavConfig;
23+
24+
/**
25+
* @param Config $eavConfig
26+
*/
27+
public function __construct(Config $eavConfig)
28+
{
29+
$this->eavConfig = $eavConfig;
30+
}
31+
32+
/**
33+
* Get allowed address attributes
34+
*
35+
* @return AbstractAttribute[]
36+
*/
37+
public function execute(): array
38+
{
39+
$attributes = $this->eavConfig->getEntityAttributes(
40+
CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER
41+
);
42+
foreach ($attributes as $attributeCode => $attribute) {
43+
if (false === $attribute->getIsVisibleOnFront()) {
44+
unset($attributes[$attributeCode]);
45+
}
46+
}
47+
return $attributes;
48+
}
49+
}

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php

+34-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class UpdateCustomerAccount
4444
*/
4545
private $changeSubscriptionStatus;
4646

47+
/**
48+
* @var GetAllowedCustomerAttributes
49+
*/
50+
private $getAllowedCustomerAttributes;
51+
4752
/**
4853
* @var array
4954
*/
@@ -55,6 +60,7 @@ class UpdateCustomerAccount
5560
* @param CheckCustomerPassword $checkCustomerPassword
5661
* @param DataObjectHelper $dataObjectHelper
5762
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
63+
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
5864
* @param array $restrictedKeys
5965
*/
6066
public function __construct(
@@ -63,6 +69,7 @@ public function __construct(
6369
CheckCustomerPassword $checkCustomerPassword,
6470
DataObjectHelper $dataObjectHelper,
6571
ChangeSubscriptionStatus $changeSubscriptionStatus,
72+
GetAllowedCustomerAttributes $getAllowedCustomerAttributes,
6673
array $restrictedKeys = []
6774
) {
6875
$this->saveCustomer = $saveCustomer;
@@ -71,6 +78,7 @@ public function __construct(
7178
$this->dataObjectHelper = $dataObjectHelper;
7279
$this->restrictedKeys = $restrictedKeys;
7380
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
81+
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
7482
}
7583

7684
/**
@@ -93,7 +101,7 @@ public function execute(CustomerInterface $customer, array $data): void
93101
$this->checkCustomerPassword->execute($data['password'], (int)$customer->getId());
94102
$customer->setEmail($data['email']);
95103
}
96-
104+
$this->validateData($data);
97105
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
98106
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);
99107

@@ -105,4 +113,29 @@ public function execute(CustomerInterface $customer, array $data): void
105113
$this->changeSubscriptionStatus->execute((int)$customer->getId(), (bool)$data['is_subscribed']);
106114
}
107115
}
116+
117+
/**
118+
* @param array $customerData
119+
* @return void
120+
* @throws GraphQlInputException
121+
*/
122+
public function validateData(array $customerData): void
123+
{
124+
$attributes = $this->getAllowedCustomerAttributes->execute();
125+
$errorInput = [];
126+
127+
foreach ($attributes as $attributeName => $attributeInfo) {
128+
if ($attributeInfo->getIsRequired()
129+
&& (isset($customerData[$attributeName]) && empty($customerData[$attributeName]))
130+
) {
131+
$errorInput[] = $attributeName;
132+
}
133+
}
134+
135+
if ($errorInput) {
136+
throw new GraphQlInputException(
137+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
138+
);
139+
}
140+
}
108141
}

0 commit comments

Comments
 (0)