Skip to content

Commit de8d0a9

Browse files
committed
MAGETWO-91513: Password reset email cannot be sent if the customer does not have customer attribute set that is changed to required after original account creation
- Skipping address validation for reset password flow
1 parent b831a20 commit de8d0a9

File tree

4 files changed

+63
-32
lines changed

4 files changed

+63
-32
lines changed

app/code/Magento/Customer/Model/AccountManagement.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13841384
$customerSecure->setRpTokenCreatedAt(
13851385
$this->dateTimeFactory->create()->format(DateTime::DATETIME_PHP_FORMAT)
13861386
);
1387+
$this->setIgnoreValidationFlag($customer);
13871388
$this->customerRepository->save($customer);
13881389
}
13891390
return true;
@@ -1537,4 +1538,15 @@ private function destroyCustomerSessions($customerId)
15371538
$this->saveHandler->destroy($sessionId);
15381539
}
15391540
}
1541+
1542+
/**
1543+
* Set ignore_validation_flag for reset password flow to skip unnecessary address and customer validation
1544+
*
1545+
* @param Customer $customer
1546+
* @return void
1547+
*/
1548+
private function setIgnoreValidationFlag($customer)
1549+
{
1550+
$customer->setData('ignore_validation_flag', true);
1551+
}
15401552
}

app/code/Magento/Customer/Model/ResourceModel/Customer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ protected function _beforeSave(\Magento\Framework\DataObject $customer)
151151
$customer->setConfirmation(null);
152152
}
153153

154-
$this->_validate($customer);
154+
if (!$customer->getData('ignore_validation_flag')) {
155+
$this->_validate($customer);
156+
}
155157

156158
return $this;
157159
}

app/code/Magento/Customer/Model/ResourceModel/Customer/Relation.php

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,43 @@ public function processRelation(\Magento\Framework\Model\AbstractModel $customer
2323
$defaultBillingId = $customer->getData('default_billing');
2424
$defaultShippingId = $customer->getData('default_shipping');
2525

26-
/** @var \Magento\Customer\Model\Address $address */
27-
foreach ($customer->getAddresses() as $address) {
28-
if ($address->getData('_deleted')) {
29-
if ($address->getId() == $defaultBillingId) {
30-
$customer->setData('default_billing', null);
31-
}
26+
if (!$customer->getData('ignore_validation_flag')) {
27+
/** @var \Magento\Customer\Model\Address $address */
28+
foreach ($customer->getAddresses() as $address) {
29+
if ($address->getData('_deleted')) {
30+
if ($address->getId() == $defaultBillingId) {
31+
$customer->setData('default_billing', null);
32+
}
3233

33-
if ($address->getId() == $defaultShippingId) {
34-
$customer->setData('default_shipping', null);
35-
}
34+
if ($address->getId() == $defaultShippingId) {
35+
$customer->setData('default_shipping', null);
36+
}
3637

37-
$removedAddressId = $address->getId();
38-
$address->delete();
38+
$removedAddressId = $address->getId();
39+
$address->delete();
3940

40-
// Remove deleted address from customer address collection
41-
$customer->getAddressesCollection()->removeItemByKey($removedAddressId);
42-
} else {
43-
$address->setParentId(
44-
$customer->getId()
45-
)->setStoreId(
46-
$customer->getStoreId()
47-
)->setIsCustomerSaveTransaction(
48-
true
49-
)->save();
41+
// Remove deleted address from customer address collection
42+
$customer->getAddressesCollection()->removeItemByKey($removedAddressId);
43+
} else {
44+
$address->setParentId(
45+
$customer->getId()
46+
)->setStoreId(
47+
$customer->getStoreId()
48+
)->setIsCustomerSaveTransaction(
49+
true
50+
)->save();
5051

51-
if (($address->getIsPrimaryBilling() ||
52-
$address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId
53-
) {
54-
$customer->setData('default_billing', $address->getId());
55-
}
52+
if (($address->getIsPrimaryBilling() ||
53+
$address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId
54+
) {
55+
$customer->setData('default_billing', $address->getId());
56+
}
5657

57-
if (($address->getIsPrimaryShipping() ||
58-
$address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId
59-
) {
60-
$customer->setData('default_shipping', $address->getId());
58+
if (($address->getIsPrimaryShipping() ||
59+
$address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId
60+
) {
61+
$customer->setData('default_shipping', $address->getId());
62+
}
6163
}
6264
}
6365
}

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
212212
) {
213213
$customerModel->setDefaultShipping($prevCustomerDataArr['default_shipping']);
214214
}
215+
$this->setValidationFlag($customerArr, $customerModel);
215216
$customerModel->save();
216217
$this->customerRegistry->push($customerModel);
217218
$customerId = $customerModel->getId();
@@ -221,7 +222,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
221222
) {
222223
$customer->setAddresses($delegatedNewOperation->getCustomer()->getAddresses());
223224
}
224-
if ($customer->getAddresses() !== null) {
225+
if ($customer->getAddresses() !== null && !$customerModel->getData('ignore_validation_flag')) {
225226
if ($customer->getId()) {
226227
$existingAddresses = $this->getById($customer->getId())->getAddresses();
227228
$getIdFunc = function ($address) {
@@ -389,4 +390,18 @@ protected function addFilterGroupToCollection(
389390
$collection->addFieldToFilter($fields);
390391
}
391392
}
393+
394+
/**
395+
* Set ignore_validation_flag to skip model validation
396+
*
397+
* @param array $customerArray
398+
* @param Customer $customerModel
399+
* @return void
400+
*/
401+
private function setValidationFlag($customerArray, $customerModel)
402+
{
403+
if (isset($customerArray['ignore_validation_flag'])) {
404+
$customerModel->setData('ignore_validation_flag', true);
405+
}
406+
}
392407
}

0 commit comments

Comments
 (0)