Skip to content

Commit 9b76f0a

Browse files
authored
ENGCOM-3056: Fix the issue with reset password when customer has address from not allowed country #18179
2 parents 19fd585 + 8dd0fbb commit 9b76f0a

File tree

2 files changed

+2171
-4
lines changed

2 files changed

+2171
-4
lines changed

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\Customer\Model\Customer as CustomerModel;
1919
use Magento\Customer\Model\Customer\CredentialsValidator;
2020
use Magento\Customer\Model\Metadata\Validator;
21+
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
2122
use Magento\Eav\Model\Validator\Attribute\Backend;
2223
use Magento\Framework\Api\ExtensibleDataObjectConverter;
2324
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -45,14 +46,13 @@
4546
use Magento\Framework\Phrase;
4647
use Magento\Framework\Reflection\DataObjectProcessor;
4748
use Magento\Framework\Registry;
49+
use Magento\Framework\Session\SaveHandlerInterface;
50+
use Magento\Framework\Session\SessionManagerInterface;
4851
use Magento\Framework\Stdlib\DateTime;
4952
use Magento\Framework\Stdlib\StringUtils as StringHelper;
5053
use Magento\Store\Model\ScopeInterface;
5154
use Magento\Store\Model\StoreManagerInterface;
5255
use Psr\Log\LoggerInterface as PsrLogger;
53-
use Magento\Framework\Session\SessionManagerInterface;
54-
use Magento\Framework\Session\SaveHandlerInterface;
55-
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
5656

5757
/**
5858
* Handle various customer account actions
@@ -333,6 +333,11 @@ class AccountManagement implements AccountManagementInterface
333333
*/
334334
private $searchCriteriaBuilder;
335335

336+
/**
337+
* @var AddressRegistry
338+
*/
339+
private $addressRegistry;
340+
336341
/**
337342
* @param CustomerFactory $customerFactory
338343
* @param ManagerInterface $eventManager
@@ -364,7 +369,9 @@ class AccountManagement implements AccountManagementInterface
364369
* @param SaveHandlerInterface|null $saveHandler
365370
* @param CollectionFactory|null $visitorCollectionFactory
366371
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
372+
* @param AddressRegistry|null $addressRegistry
367373
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
374+
* @SuppressWarnings(PHPMD.NPathComplexity)
368375
*/
369376
public function __construct(
370377
CustomerFactory $customerFactory,
@@ -396,7 +403,8 @@ public function __construct(
396403
SessionManagerInterface $sessionManager = null,
397404
SaveHandlerInterface $saveHandler = null,
398405
CollectionFactory $visitorCollectionFactory = null,
399-
SearchCriteriaBuilder $searchCriteriaBuilder = null
406+
SearchCriteriaBuilder $searchCriteriaBuilder = null,
407+
AddressRegistry $addressRegistry = null
400408
) {
401409
$this->customerFactory = $customerFactory;
402410
$this->eventManager = $eventManager;
@@ -434,6 +442,8 @@ public function __construct(
434442
?: ObjectManager::getInstance()->get(CollectionFactory::class);
435443
$this->searchCriteriaBuilder = $searchCriteriaBuilder
436444
?: ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
445+
$this->addressRegistry = $addressRegistry
446+
?: ObjectManager::getInstance()->get(AddressRegistry::class);
437447
}
438448

439449
/**
@@ -579,6 +589,9 @@ public function initiatePasswordReset($email, $template, $websiteId = null)
579589
// load customer by email
580590
$customer = $this->customerRepository->get($email, $websiteId);
581591

592+
// No need to validate customer address while saving customer reset password token
593+
$this->disableAddressValidation($customer);
594+
582595
$newPasswordToken = $this->mathRandom->getUniqueHash();
583596
$this->changeResetPasswordLinkToken($customer, $newPasswordToken);
584597

@@ -669,6 +682,10 @@ public function resetPassword($email, $resetToken, $newPassword)
669682
} else {
670683
$customer = $this->customerRepository->get($email);
671684
}
685+
686+
// No need to validate customer address while saving customer reset password token
687+
$this->disableAddressValidation($customer);
688+
672689
//Validate Token and new password strength
673690
$this->validateResetPasswordToken($customer->getId(), $resetToken);
674691
$this->credentialsValidator->checkPasswordDifferentFromEmail(
@@ -926,6 +943,8 @@ public function getDefaultShippingAddress($customerId)
926943
* @param string $redirectUrl
927944
* @param array $extensions
928945
* @return void
946+
* @throws LocalizedException
947+
* @throws NoSuchEntityException
929948
*/
930949
protected function sendEmailConfirmation(CustomerInterface $customer, $redirectUrl, $extensions = [])
931950
{
@@ -987,7 +1006,10 @@ public function changePasswordById($customerId, $currentPassword, $newPassword)
9871006
* @param string $newPassword
9881007
* @return bool true on success
9891008
* @throws InputException
1009+
* @throws InputMismatchException
9901010
* @throws InvalidEmailOrPasswordException
1011+
* @throws LocalizedException
1012+
* @throws NoSuchEntityException
9911013
* @throws UserLockedException
9921014
*/
9931015
private function changePasswordForCustomer($customer, $currentPassword, $newPassword)
@@ -1202,6 +1224,8 @@ protected function sendNewAccountEmail(
12021224
*
12031225
* @param CustomerInterface $customer
12041226
* @return $this
1227+
* @throws LocalizedException
1228+
* @throws NoSuchEntityException
12051229
* @deprecated 100.1.0
12061230
*/
12071231
protected function sendPasswordResetNotificationEmail($customer)
@@ -1264,6 +1288,7 @@ protected function getTemplateTypes()
12641288
* @param int|null $storeId
12651289
* @param string $email
12661290
* @return $this
1291+
* @throws MailException
12671292
* @deprecated 100.1.0
12681293
*/
12691294
protected function sendEmailTemplate(
@@ -1379,6 +1404,9 @@ public function isResetPasswordLinkTokenExpired($rpToken, $rpTokenCreatedAt)
13791404
* @param string $passwordLinkToken
13801405
* @return bool
13811406
* @throws InputException
1407+
* @throws InputMismatchException
1408+
* @throws LocalizedException
1409+
* @throws NoSuchEntityException
13821410
*/
13831411
public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13841412
{
@@ -1407,6 +1435,8 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
14071435
*
14081436
* @param CustomerInterface $customer
14091437
* @return $this
1438+
* @throws LocalizedException
1439+
* @throws NoSuchEntityException
14101440
* @deprecated 100.1.0
14111441
*/
14121442
public function sendPasswordReminderEmail($customer)
@@ -1434,6 +1464,8 @@ public function sendPasswordReminderEmail($customer)
14341464
*
14351465
* @param CustomerInterface $customer
14361466
* @return $this
1467+
* @throws LocalizedException
1468+
* @throws NoSuchEntityException
14371469
* @deprecated 100.1.0
14381470
*/
14391471
public function sendPasswordResetConfirmationEmail($customer)
@@ -1478,6 +1510,7 @@ protected function getAddressById(CustomerInterface $customer, $addressId)
14781510
*
14791511
* @param CustomerInterface $customer
14801512
* @return Data\CustomerSecure
1513+
* @throws NoSuchEntityException
14811514
* @deprecated 100.1.0
14821515
*/
14831516
protected function getFullCustomerObject($customer)
@@ -1505,6 +1538,20 @@ public function getPasswordHash($password)
15051538
return $this->encryptor->getHash($password);
15061539
}
15071540

1541+
/**
1542+
* Disable Customer Address Validation
1543+
*
1544+
* @param CustomerInterface $customer
1545+
* @throws NoSuchEntityException
1546+
*/
1547+
private function disableAddressValidation($customer)
1548+
{
1549+
foreach ($customer->getAddresses() as $address) {
1550+
$addressModel = $this->addressRegistry->retrieve($address->getId());
1551+
$addressModel->setShouldIgnoreValidation(true);
1552+
}
1553+
}
1554+
15081555
/**
15091556
* Get email notification
15101557
*

0 commit comments

Comments
 (0)