Skip to content

Commit 7a974b0

Browse files
ENGCOM-3812: [Backport] Fix the issue with reset password when customer has address from not allowed country #19964
- Merge Pull Request #19964 from dmytro-ch/magento2:fix/2.2-issue-18170 - Merged commits: 1. 317c02a
2 parents 523a83e + 317c02a commit 7a974b0

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(
@@ -906,6 +925,8 @@ public function getDefaultShippingAddress($customerId)
906925
* @param string $redirectUrl
907926
* @param array $extensions
908927
* @return void
928+
* @throws LocalizedException
929+
* @throws NoSuchEntityException
909930
*/
910931
protected function sendEmailConfirmation(CustomerInterface $customer, $redirectUrl, $extensions = [])
911932
{
@@ -967,7 +988,10 @@ public function changePasswordById($customerId, $currentPassword, $newPassword)
967988
* @param string $newPassword
968989
* @return bool true on success
969990
* @throws InputException
991+
* @throws InputMismatchException
970992
* @throws InvalidEmailOrPasswordException
993+
* @throws LocalizedException
994+
* @throws NoSuchEntityException
971995
* @throws UserLockedException
972996
*/
973997
private function changePasswordForCustomer($customer, $currentPassword, $newPassword)
@@ -1076,10 +1100,11 @@ public function isCustomerInStore($customerWebsiteId, $storeId)
10761100
* @param int $customerId
10771101
* @param string $resetPasswordLinkToken
10781102
* @return bool
1079-
* @throws \Magento\Framework\Exception\State\InputMismatchException If token is mismatched
1080-
* @throws \Magento\Framework\Exception\State\ExpiredException If token is expired
1081-
* @throws \Magento\Framework\Exception\InputException If token or customer id is invalid
1082-
* @throws \Magento\Framework\Exception\NoSuchEntityException If customer doesn't exist
1103+
* @throws ExpiredException
1104+
* @throws InputException
1105+
* @throws InputMismatchException
1106+
* @throws LocalizedException
1107+
* @throws NoSuchEntityException
10831108
*/
10841109
private function validateResetPasswordToken($customerId, $resetPasswordLinkToken)
10851110
{
@@ -1169,6 +1194,8 @@ protected function sendNewAccountEmail(
11691194
*
11701195
* @param CustomerInterface $customer
11711196
* @return $this
1197+
* @throws LocalizedException
1198+
* @throws NoSuchEntityException
11721199
* @deprecated 100.1.0
11731200
*/
11741201
protected function sendPasswordResetNotificationEmail($customer)
@@ -1182,6 +1209,7 @@ protected function sendPasswordResetNotificationEmail($customer)
11821209
* @param CustomerInterface $customer
11831210
* @param int|string|null $defaultStoreId
11841211
* @return int
1212+
* @throws LocalizedException
11851213
* @deprecated 100.1.0
11861214
*/
11871215
protected function getWebsiteStoreId($customer, $defaultStoreId = null)
@@ -1195,6 +1223,8 @@ protected function getWebsiteStoreId($customer, $defaultStoreId = null)
11951223
}
11961224

11971225
/**
1226+
* Get email template types
1227+
*
11981228
* @return array
11991229
* @deprecated 100.1.0
12001230
*/
@@ -1228,6 +1258,7 @@ protected function getTemplateTypes()
12281258
* @param int|null $storeId
12291259
* @param string $email
12301260
* @return $this
1261+
* @throws MailException
12311262
* @deprecated 100.1.0
12321263
*/
12331264
protected function sendEmailTemplate(
@@ -1334,6 +1365,9 @@ public function isResetPasswordLinkTokenExpired($rpToken, $rpTokenCreatedAt)
13341365
* @param string $passwordLinkToken
13351366
* @return bool
13361367
* @throws InputException
1368+
* @throws InputMismatchException
1369+
* @throws LocalizedException
1370+
* @throws NoSuchEntityException
13371371
*/
13381372
public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13391373
{
@@ -1361,6 +1395,8 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13611395
*
13621396
* @param CustomerInterface $customer
13631397
* @return $this
1398+
* @throws LocalizedException
1399+
* @throws NoSuchEntityException
13641400
* @deprecated 100.1.0
13651401
*/
13661402
public function sendPasswordReminderEmail($customer)
@@ -1388,6 +1424,8 @@ public function sendPasswordReminderEmail($customer)
13881424
*
13891425
* @param CustomerInterface $customer
13901426
* @return $this
1427+
* @throws LocalizedException
1428+
* @throws NoSuchEntityException
13911429
* @deprecated 100.1.0
13921430
*/
13931431
public function sendPasswordResetConfirmationEmail($customer)
@@ -1432,6 +1470,7 @@ protected function getAddressById(CustomerInterface $customer, $addressId)
14321470
*
14331471
* @param CustomerInterface $customer
14341472
* @return Data\CustomerSecure
1473+
* @throws NoSuchEntityException
14351474
* @deprecated 100.1.0
14361475
*/
14371476
protected function getFullCustomerObject($customer)
@@ -1457,6 +1496,20 @@ public function getPasswordHash($password)
14571496
return $this->encryptor->getHash($password);
14581497
}
14591498

1499+
/**
1500+
* Disable Customer Address Validation
1501+
*
1502+
* @param CustomerInterface $customer
1503+
* @throws NoSuchEntityException
1504+
*/
1505+
private function disableAddressValidation($customer)
1506+
{
1507+
foreach ($customer->getAddresses() as $address) {
1508+
$addressModel = $this->addressRegistry->retrieve($address->getId());
1509+
$addressModel->setShouldIgnoreValidation(true);
1510+
}
1511+
}
1512+
14601513
/**
14611514
* Get email notification
14621515
*

0 commit comments

Comments
 (0)