Skip to content

Commit 317c02a

Browse files
committed
Fix the reset password issue when customer has address from not allowed country #18170
1 parent 6b109f4 commit 317c02a

File tree

1 file changed

+66
-13
lines changed

1 file changed

+66
-13
lines changed

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

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Customer\Model\Customer as CustomerModel;
1818
use Magento\Customer\Model\Customer\CredentialsValidator;
1919
use Magento\Customer\Model\Metadata\Validator;
20+
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
2021
use Magento\Eav\Model\Validator\Attribute\Backend;
2122
use Magento\Framework\Api\ExtensibleDataObjectConverter;
2223
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -44,14 +45,13 @@
4445
use Magento\Framework\Phrase;
4546
use Magento\Framework\Reflection\DataObjectProcessor;
4647
use Magento\Framework\Registry;
48+
use Magento\Framework\Session\SaveHandlerInterface;
49+
use Magento\Framework\Session\SessionManagerInterface;
4750
use Magento\Framework\Stdlib\DateTime;
4851
use Magento\Framework\Stdlib\StringUtils as StringHelper;
4952
use Magento\Store\Model\ScopeInterface;
5053
use Magento\Store\Model\StoreManagerInterface;
5154
use Psr\Log\LoggerInterface as PsrLogger;
52-
use Magento\Framework\Session\SessionManagerInterface;
53-
use Magento\Framework\Session\SaveHandlerInterface;
54-
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
5555

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

335+
/**
336+
* @var AddressRegistry
337+
*/
338+
private $addressRegistry;
339+
335340
/**
336341
* @param CustomerFactory $customerFactory
337342
* @param ManagerInterface $eventManager
@@ -359,12 +364,13 @@ class AccountManagement implements AccountManagementInterface
359364
* @param CredentialsValidator|null $credentialsValidator
360365
* @param DateTimeFactory|null $dateTimeFactory
361366
* @param AccountConfirmation|null $accountConfirmation
362-
* @param DateTimeFactory $dateTimeFactory
363367
* @param SessionManagerInterface|null $sessionManager
364368
* @param SaveHandlerInterface|null $saveHandler
365369
* @param CollectionFactory|null $visitorCollectionFactory
366370
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
371+
* @param AddressRegistry|null $addressRegistry
367372
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
373+
* @SuppressWarnings(PHPMD.NPathComplexity)
368374
*/
369375
public function __construct(
370376
CustomerFactory $customerFactory,
@@ -396,7 +402,8 @@ public function __construct(
396402
SessionManagerInterface $sessionManager = null,
397403
SaveHandlerInterface $saveHandler = null,
398404
CollectionFactory $visitorCollectionFactory = null,
399-
SearchCriteriaBuilder $searchCriteriaBuilder = null
405+
SearchCriteriaBuilder $searchCriteriaBuilder = null,
406+
AddressRegistry $addressRegistry = null
400407
) {
401408
$this->customerFactory = $customerFactory;
402409
$this->eventManager = $eventManager;
@@ -434,6 +441,8 @@ public function __construct(
434441
?: ObjectManager::getInstance()->get(CollectionFactory::class);
435442
$this->searchCriteriaBuilder = $searchCriteriaBuilder
436443
?: ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
444+
$this->addressRegistry = $addressRegistry
445+
?: ObjectManager::getInstance()->get(AddressRegistry::class);
437446
}
438447

439448
/**
@@ -499,8 +508,11 @@ public function activateById($customerId, $confirmationKey)
499508
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
500509
* @param string $confirmationKey
501510
* @return \Magento\Customer\Api\Data\CustomerInterface
502-
* @throws \Magento\Framework\Exception\State\InvalidTransitionException
503-
* @throws \Magento\Framework\Exception\State\InputMismatchException
511+
* @throws InputException
512+
* @throws InputMismatchException
513+
* @throws InvalidTransitionException
514+
* @throws LocalizedException
515+
* @throws NoSuchEntityException
504516
*/
505517
private function activateCustomer($customer, $confirmationKey)
506518
{
@@ -574,6 +586,9 @@ public function initiatePasswordReset($email, $template, $websiteId = null)
574586
// load customer by email
575587
$customer = $this->customerRepository->get($email, $websiteId);
576588

589+
// No need to validate customer address while saving customer reset password token
590+
$this->disableAddressValidation($customer);
591+
577592
$newPasswordToken = $this->mathRandom->getUniqueHash();
578593
$this->changeResetPasswordLinkToken($customer, $newPasswordToken);
579594

@@ -611,10 +626,10 @@ public function initiatePasswordReset($email, $template, $websiteId = null)
611626
* Match a customer by their RP token.
612627
*
613628
* @param string $rpToken
629+
* @return CustomerInterface
614630
* @throws ExpiredException
631+
* @throws LocalizedException
615632
* @throws NoSuchEntityException
616-
*
617-
* @return CustomerInterface
618633
*/
619634
private function matchCustomerByRpToken(string $rpToken): CustomerInterface
620635
{
@@ -657,6 +672,10 @@ public function resetPassword($email, $resetToken, $newPassword)
657672
} else {
658673
$customer = $this->customerRepository->get($email);
659674
}
675+
676+
// No need to validate customer address while saving customer reset password token
677+
$this->disableAddressValidation($customer);
678+
660679
//Validate Token and new password strength
661680
$this->validateResetPasswordToken($customer->getId(), $resetToken);
662681
$this->credentialsValidator->checkPasswordDifferentFromEmail(
@@ -900,6 +919,8 @@ public function getDefaultShippingAddress($customerId)
900919
* @param CustomerInterface $customer
901920
* @param string $redirectUrl
902921
* @return void
922+
* @throws LocalizedException
923+
* @throws NoSuchEntityException
903924
*/
904925
protected function sendEmailConfirmation(CustomerInterface $customer, $redirectUrl)
905926
{
@@ -954,7 +975,10 @@ public function changePasswordById($customerId, $currentPassword, $newPassword)
954975
* @param string $newPassword
955976
* @return bool true on success
956977
* @throws InputException
978+
* @throws InputMismatchException
957979
* @throws InvalidEmailOrPasswordException
980+
* @throws LocalizedException
981+
* @throws NoSuchEntityException
958982
* @throws UserLockedException
959983
*/
960984
private function changePasswordForCustomer($customer, $currentPassword, $newPassword)
@@ -1063,10 +1087,11 @@ public function isCustomerInStore($customerWebsiteId, $storeId)
10631087
* @param int $customerId
10641088
* @param string $resetPasswordLinkToken
10651089
* @return bool
1066-
* @throws \Magento\Framework\Exception\State\InputMismatchException If token is mismatched
1067-
* @throws \Magento\Framework\Exception\State\ExpiredException If token is expired
1068-
* @throws \Magento\Framework\Exception\InputException If token or customer id is invalid
1069-
* @throws \Magento\Framework\Exception\NoSuchEntityException If customer doesn't exist
1090+
* @throws ExpiredException
1091+
* @throws InputException
1092+
* @throws InputMismatchException
1093+
* @throws LocalizedException
1094+
* @throws NoSuchEntityException
10701095
*/
10711096
private function validateResetPasswordToken($customerId, $resetPasswordLinkToken)
10721097
{
@@ -1156,6 +1181,8 @@ protected function sendNewAccountEmail(
11561181
*
11571182
* @param CustomerInterface $customer
11581183
* @return $this
1184+
* @throws LocalizedException
1185+
* @throws NoSuchEntityException
11591186
* @deprecated 100.1.0
11601187
*/
11611188
protected function sendPasswordResetNotificationEmail($customer)
@@ -1169,6 +1196,7 @@ protected function sendPasswordResetNotificationEmail($customer)
11691196
* @param CustomerInterface $customer
11701197
* @param int|string|null $defaultStoreId
11711198
* @return int
1199+
* @throws LocalizedException
11721200
* @deprecated 100.1.0
11731201
*/
11741202
protected function getWebsiteStoreId($customer, $defaultStoreId = null)
@@ -1182,6 +1210,8 @@ protected function getWebsiteStoreId($customer, $defaultStoreId = null)
11821210
}
11831211

11841212
/**
1213+
* Get email template types
1214+
*
11851215
* @return array
11861216
* @deprecated 100.1.0
11871217
*/
@@ -1215,6 +1245,7 @@ protected function getTemplateTypes()
12151245
* @param int|null $storeId
12161246
* @param string $email
12171247
* @return $this
1248+
* @throws MailException
12181249
* @deprecated 100.1.0
12191250
*/
12201251
protected function sendEmailTemplate(
@@ -1321,6 +1352,9 @@ public function isResetPasswordLinkTokenExpired($rpToken, $rpTokenCreatedAt)
13211352
* @param string $passwordLinkToken
13221353
* @return bool
13231354
* @throws InputException
1355+
* @throws InputMismatchException
1356+
* @throws LocalizedException
1357+
* @throws NoSuchEntityException
13241358
*/
13251359
public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13261360
{
@@ -1348,6 +1382,8 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13481382
*
13491383
* @param CustomerInterface $customer
13501384
* @return $this
1385+
* @throws LocalizedException
1386+
* @throws NoSuchEntityException
13511387
* @deprecated 100.1.0
13521388
*/
13531389
public function sendPasswordReminderEmail($customer)
@@ -1375,6 +1411,8 @@ public function sendPasswordReminderEmail($customer)
13751411
*
13761412
* @param CustomerInterface $customer
13771413
* @return $this
1414+
* @throws LocalizedException
1415+
* @throws NoSuchEntityException
13781416
* @deprecated 100.1.0
13791417
*/
13801418
public function sendPasswordResetConfirmationEmail($customer)
@@ -1419,6 +1457,7 @@ protected function getAddressById(CustomerInterface $customer, $addressId)
14191457
*
14201458
* @param CustomerInterface $customer
14211459
* @return Data\CustomerSecure
1460+
* @throws NoSuchEntityException
14221461
* @deprecated 100.1.0
14231462
*/
14241463
protected function getFullCustomerObject($customer)
@@ -1444,6 +1483,20 @@ public function getPasswordHash($password)
14441483
return $this->encryptor->getHash($password);
14451484
}
14461485

1486+
/**
1487+
* Disable Customer Address Validation
1488+
*
1489+
* @param CustomerInterface $customer
1490+
* @throws NoSuchEntityException
1491+
*/
1492+
private function disableAddressValidation($customer)
1493+
{
1494+
foreach ($customer->getAddresses() as $address) {
1495+
$addressModel = $this->addressRegistry->retrieve($address->getId());
1496+
$addressModel->setShouldIgnoreValidation(true);
1497+
}
1498+
}
1499+
14471500
/**
14481501
* Get email notification
14491502
*

0 commit comments

Comments
 (0)