From 3735e912b66fe91acf545c7db597b2a4cda44edf Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 16 Mar 2019 13:21:52 +0200 Subject: [PATCH] magento/graphql-ce#271: [My Account] Add support of Customer attributes - Customer attributes validation added for create customer action --- .../Model/Customer/CreateCustomerAccount.php | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php index b7b66df042467..685332e638d81 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php @@ -45,25 +45,33 @@ class CreateCustomerAccount */ private $changeSubscriptionStatus; + /** + * @var GetAllowedCustomerAttributes + */ + private $getAllowedCustomerAttributes; + /** * @param DataObjectHelper $dataObjectHelper * @param CustomerInterfaceFactory $customerFactory * @param StoreManagerInterface $storeManager * @param AccountManagementInterface $accountManagement * @param ChangeSubscriptionStatus $changeSubscriptionStatus + * @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes */ public function __construct( DataObjectHelper $dataObjectHelper, CustomerInterfaceFactory $customerFactory, StoreManagerInterface $storeManager, AccountManagementInterface $accountManagement, - ChangeSubscriptionStatus $changeSubscriptionStatus + ChangeSubscriptionStatus $changeSubscriptionStatus, + GetAllowedCustomerAttributes $getAllowedCustomerAttributes ) { $this->dataObjectHelper = $dataObjectHelper; $this->customerFactory = $customerFactory; $this->accountManagement = $accountManagement; $this->storeManager = $storeManager; $this->changeSubscriptionStatus = $changeSubscriptionStatus; + $this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; } /** @@ -96,6 +104,7 @@ public function execute(array $data): CustomerInterface */ private function createAccount(array $data): CustomerInterface { + $this->validateData($data); $customerDataObject = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray( $customerDataObject, @@ -109,4 +118,29 @@ private function createAccount(array $data): CustomerInterface $password = array_key_exists('password', $data) ? $data['password'] : null; return $this->accountManagement->createAccount($customerDataObject, $password); } + + /** + * @param array $customerData + * @return void + * @throws GraphQlInputException + */ + public function validateData(array $customerData): void + { + $attributes = $this->getAllowedCustomerAttributes->execute(); + $errorInput = []; + + foreach ($attributes as $attributeName => $attributeInfo) { + if ($attributeInfo->getIsRequired() + && (isset($customerData[$attributeName]) && empty($customerData[$attributeName])) + ) { + $errorInput[] = $attributeName; + } + } + + if ($errorInput) { + throw new GraphQlInputException( + __('Required parameters are missing: %1', [implode(', ', $errorInput)]) + ); + } + } }