Skip to content

Commit fb30023

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-91513' into EPAM-PR-34
2 parents 503d05a + 059fc9c commit fb30023

File tree

13 files changed

+353
-19
lines changed

13 files changed

+353
-19
lines changed

app/code/Magento/Customer/Controller/Account/EditPost.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Customer\Controller\Account;
99

10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\AddressRegistry;
1012
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1113
use Magento\Customer\Model\AuthenticationInterface;
1214
use Magento\Customer\Model\Customer\Mapper;
@@ -25,6 +27,7 @@
2527
use Magento\Framework\Escaper;
2628
use Magento\Framework\Exception\InputException;
2729
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
30+
use Magento\Framework\Exception\NoSuchEntityException;
2831
use Magento\Framework\Exception\State\UserLockedException;
2932
use Magento\Customer\Controller\AbstractAccount;
3033
use Magento\Framework\Phrase;
@@ -85,6 +88,11 @@ class EditPost extends AbstractAccount implements CsrfAwareActionInterface, Http
8588
*/
8689
private $escaper;
8790

91+
/**
92+
* @var AddressRegistry
93+
*/
94+
private $addressRegistry;
95+
8896
/**
8997
* @param Context $context
9098
* @param Session $customerSession
@@ -93,6 +101,7 @@ class EditPost extends AbstractAccount implements CsrfAwareActionInterface, Http
93101
* @param Validator $formKeyValidator
94102
* @param CustomerExtractor $customerExtractor
95103
* @param Escaper|null $escaper
104+
* @param AddressRegistry|null $addressRegistry
96105
*/
97106
public function __construct(
98107
Context $context,
@@ -101,7 +110,8 @@ public function __construct(
101110
CustomerRepositoryInterface $customerRepository,
102111
Validator $formKeyValidator,
103112
CustomerExtractor $customerExtractor,
104-
?Escaper $escaper = null
113+
?Escaper $escaper = null,
114+
AddressRegistry $addressRegistry = null
105115
) {
106116
parent::__construct($context);
107117
$this->session = $customerSession;
@@ -110,6 +120,7 @@ public function __construct(
110120
$this->formKeyValidator = $formKeyValidator;
111121
$this->customerExtractor = $customerExtractor;
112122
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
123+
$this->addressRegistry = $addressRegistry ?: ObjectManager::getInstance()->get(AddressRegistry::class);
113124
}
114125

115126
/**
@@ -195,6 +206,9 @@ public function execute()
195206
// whether a customer enabled change password option
196207
$isPasswordChanged = $this->changeCustomerPassword($currentCustomerDataObject->getEmail());
197208

209+
// No need to validate customer address while editing customer profile
210+
$this->disableAddressValidation($customerCandidateDataObject);
211+
198212
$this->customerRepository->save($customerCandidateDataObject);
199213
$this->getEmailNotification()->credentialsChanged(
200214
$customerCandidateDataObject,
@@ -352,4 +366,18 @@ private function getCustomerMapper()
352366
}
353367
return $this->customerMapper;
354368
}
369+
370+
/**
371+
* Disable Customer Address Validation
372+
*
373+
* @param CustomerInterface $customer
374+
* @throws NoSuchEntityException
375+
*/
376+
private function disableAddressValidation($customer)
377+
{
378+
foreach ($customer->getAddresses() as $address) {
379+
$addressModel = $this->addressRegistry->retrieve($address->getId());
380+
$addressModel->setShouldIgnoreValidation(true);
381+
}
382+
}
355383
}

app/code/Magento/Customer/Controller/Adminhtml/Index/InlineEdit.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
use Magento\Backend\App\Action;
99
use Magento\Customer\Api\CustomerRepositoryInterface;
1010
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\AddressRegistry;
1112
use Magento\Customer\Model\EmailNotificationInterface;
1213
use Magento\Customer\Ui\Component\Listing\AttributeRepository;
1314
use Magento\Framework\App\Action\HttpPostActionInterface;
15+
use Magento\Framework\Exception\NoSuchEntityException;
1416
use Magento\Framework\Message\MessageInterface;
17+
use Magento\Framework\App\ObjectManager;
1518

1619
/**
1720
* Customer inline edit action
@@ -62,27 +65,35 @@ class InlineEdit extends \Magento\Backend\App\Action implements HttpPostActionIn
6265
*/
6366
private $emailNotification;
6467

68+
/**
69+
* @var AddressRegistry
70+
*/
71+
private $addressRegistry;
72+
6573
/**
6674
* @param Action\Context $context
6775
* @param CustomerRepositoryInterface $customerRepository
6876
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
6977
* @param \Magento\Customer\Model\Customer\Mapper $customerMapper
7078
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
7179
* @param \Psr\Log\LoggerInterface $logger
80+
* @param AddressRegistry|null $addressRegistry
7281
*/
7382
public function __construct(
7483
Action\Context $context,
7584
CustomerRepositoryInterface $customerRepository,
7685
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
7786
\Magento\Customer\Model\Customer\Mapper $customerMapper,
7887
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
79-
\Psr\Log\LoggerInterface $logger
88+
\Psr\Log\LoggerInterface $logger,
89+
AddressRegistry $addressRegistry = null
8090
) {
8191
$this->customerRepository = $customerRepository;
8292
$this->resultJsonFactory = $resultJsonFactory;
8393
$this->customerMapper = $customerMapper;
8494
$this->dataObjectHelper = $dataObjectHelper;
8595
$this->logger = $logger;
96+
$this->addressRegistry = $addressRegistry ?: ObjectManager::getInstance()->get(AddressRegistry::class);
8697
parent::__construct($context);
8798
}
8899

@@ -219,6 +230,8 @@ protected function updateDefaultBilling(array $data)
219230
protected function saveCustomer(CustomerInterface $customer)
220231
{
221232
try {
233+
// No need to validate customer address during inline edit action
234+
$this->disableAddressValidation($customer);
222235
$this->customerRepository->save($customer);
223236
} catch (\Magento\Framework\Exception\InputException $e) {
224237
$this->getMessageManager()->addError($this->getErrorWithCustomerId($e->getMessage()));
@@ -304,4 +317,18 @@ protected function getErrorWithCustomerId($errorText)
304317
{
305318
return '[Customer ID: ' . $this->getCustomer()->getId() . '] ' . __($errorText);
306319
}
320+
321+
/**
322+
* Disable Customer Address Validation
323+
*
324+
* @param CustomerInterface $customer
325+
* @throws NoSuchEntityException
326+
*/
327+
private function disableAddressValidation($customer)
328+
{
329+
foreach ($customer->getAddresses() as $address) {
330+
$addressModel = $this->addressRegistry->retrieve($address->getId());
331+
$addressModel->setShouldIgnoreValidation(true);
332+
}
333+
}
307334
}

app/code/Magento/Customer/Controller/Adminhtml/Index/MassAssignGroup.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Customer\Controller\Adminhtml\Index;
77

8+
use Magento\Customer\Model\Customer;
89
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
910
use Magento\Backend\App\Action\Context;
1011
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
@@ -52,6 +53,8 @@ protected function massAction(AbstractCollection $collection)
5253
// Verify customer exists
5354
$customer = $this->customerRepository->getById($customerId);
5455
$customer->setGroupId($this->getRequest()->getParam('group'));
56+
// No need to validate customer and customer address during assigning customer to the group
57+
$this->setIgnoreValidationFlag($customer);
5558
$this->customerRepository->save($customer);
5659
$customersUpdated++;
5760
}
@@ -65,4 +68,15 @@ protected function massAction(AbstractCollection $collection)
6568

6669
return $resultRedirect;
6770
}
71+
72+
/**
73+
* Set ignore_validation_flag to skip unnecessary address and customer validation
74+
*
75+
* @param Customer $customer
76+
* @return void
77+
*/
78+
private function setIgnoreValidationFlag($customer)
79+
{
80+
$customer->setData('ignore_validation_flag', true);
81+
}
6882
}

app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
*/
66
namespace Magento\Customer\Controller\Adminhtml\Index;
77

8+
use Magento\Customer\Api\AccountManagementInterface;
9+
use Magento\Customer\Api\AddressRepositoryInterface;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Address\Mapper;
12+
use Magento\Customer\Model\AddressRegistry;
13+
use Magento\Framework\Api\DataObjectHelper;
14+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
15+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
16+
use Magento\Framework\DataObjectFactory as ObjectFactory;
817
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
918
use Magento\Customer\Api\AddressMetadataInterface;
1019
use Magento\Customer\Api\CustomerMetadataInterface;
@@ -13,6 +22,8 @@
1322
use Magento\Customer\Model\EmailNotificationInterface;
1423
use Magento\Customer\Model\Metadata\Form;
1524
use Magento\Framework\Exception\LocalizedException;
25+
use Magento\Framework\Exception\NoSuchEntityException;
26+
use Magento\Framework\App\ObjectManager;
1627

1728
/**
1829
* Save customer action.
@@ -26,6 +37,100 @@ class Save extends \Magento\Customer\Controller\Adminhtml\Index implements HttpP
2637
*/
2738
private $emailNotification;
2839

40+
/**
41+
* @var AddressRegistry
42+
*/
43+
private $addressRegistry;
44+
45+
/**
46+
* Constructor
47+
*
48+
* @param \Magento\Backend\App\Action\Context $context
49+
* @param \Magento\Framework\Registry $coreRegistry
50+
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
51+
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
52+
* @param \Magento\Customer\Model\AddressFactory $addressFactory
53+
* @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
54+
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
55+
* @param \Magento\Customer\Helper\View $viewHelper
56+
* @param \Magento\Framework\Math\Random $random
57+
* @param CustomerRepositoryInterface $customerRepository
58+
* @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
59+
* @param Mapper $addressMapper
60+
* @param AccountManagementInterface $customerAccountManagement
61+
* @param AddressRepositoryInterface $addressRepository
62+
* @param CustomerInterfaceFactory $customerDataFactory
63+
* @param AddressInterfaceFactory $addressDataFactory
64+
* @param \Magento\Customer\Model\Customer\Mapper $customerMapper
65+
* @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
66+
* @param DataObjectHelper $dataObjectHelper
67+
* @param ObjectFactory $objectFactory
68+
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
69+
* @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
70+
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
71+
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
72+
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
73+
* @param AddressRegistry|null $addressRegistry
74+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
75+
*/
76+
public function __construct(
77+
\Magento\Backend\App\Action\Context $context,
78+
\Magento\Framework\Registry $coreRegistry,
79+
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
80+
\Magento\Customer\Model\CustomerFactory $customerFactory,
81+
\Magento\Customer\Model\AddressFactory $addressFactory,
82+
\Magento\Customer\Model\Metadata\FormFactory $formFactory,
83+
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
84+
\Magento\Customer\Helper\View $viewHelper,
85+
\Magento\Framework\Math\Random $random,
86+
CustomerRepositoryInterface $customerRepository,
87+
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
88+
Mapper $addressMapper,
89+
AccountManagementInterface $customerAccountManagement,
90+
AddressRepositoryInterface $addressRepository,
91+
CustomerInterfaceFactory $customerDataFactory,
92+
AddressInterfaceFactory $addressDataFactory,
93+
\Magento\Customer\Model\Customer\Mapper $customerMapper,
94+
\Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
95+
DataObjectHelper $dataObjectHelper,
96+
ObjectFactory $objectFactory,
97+
\Magento\Framework\View\LayoutFactory $layoutFactory,
98+
\Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
99+
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
100+
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
101+
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
102+
AddressRegistry $addressRegistry = null
103+
) {
104+
parent::__construct(
105+
$context,
106+
$coreRegistry,
107+
$fileFactory,
108+
$customerFactory,
109+
$addressFactory,
110+
$formFactory,
111+
$subscriberFactory,
112+
$viewHelper,
113+
$random,
114+
$customerRepository,
115+
$extensibleDataObjectConverter,
116+
$addressMapper,
117+
$customerAccountManagement,
118+
$addressRepository,
119+
$customerDataFactory,
120+
$addressDataFactory,
121+
$customerMapper,
122+
$dataObjectProcessor,
123+
$dataObjectHelper,
124+
$objectFactory,
125+
$layoutFactory,
126+
$resultLayoutFactory,
127+
$resultPageFactory,
128+
$resultForwardFactory,
129+
$resultJsonFactory
130+
);
131+
$this->addressRegistry = $addressRegistry ?: ObjectManager::getInstance()->get(AddressRegistry::class);
132+
}
133+
29134
/**
30135
* Reformat customer account data to be compatible with customer service interface
31136
*
@@ -195,6 +300,8 @@ public function execute()
195300

196301
if ($customerId) {
197302
$currentCustomer = $this->_customerRepository->getById($customerId);
303+
// No need to validate customer address while editing customer profile
304+
$this->disableAddressValidation($currentCustomer);
198305
$customerData = array_merge(
199306
$this->customerMapper->toFlatArray($currentCustomer),
200307
$customerData
@@ -368,4 +475,18 @@ private function getCurrentCustomerId()
368475

369476
return $customerId;
370477
}
478+
479+
/**
480+
* Disable Customer Address Validation
481+
*
482+
* @param CustomerInterface $customer
483+
* @throws NoSuchEntityException
484+
*/
485+
private function disableAddressValidation($customer)
486+
{
487+
foreach ($customer->getAddresses() as $address) {
488+
$addressModel = $this->addressRegistry->retrieve($address->getId());
489+
$addressModel->setShouldIgnoreValidation(true);
490+
}
491+
}
371492
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
6161
* @SuppressWarnings(PHPMD.TooManyFields)
6262
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
63+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
6364
*/
6465
class AccountManagement implements AccountManagementInterface
6566
{
@@ -524,6 +525,8 @@ private function activateCustomer($customer, $confirmationKey)
524525
}
525526

526527
$customer->setConfirmation(null);
528+
// No need to validate customer and customer address while activating customer
529+
$this->setIgnoreValidationFlag($customer);
527530
$this->customerRepository->save($customer);
528531
$this->getEmailNotification()->newAccount(
529532
$customer,
@@ -683,8 +686,9 @@ public function resetPassword($email, $resetToken, $newPassword)
683686
$customer = $this->customerRepository->get($email);
684687
}
685688

686-
// No need to validate customer address while saving customer reset password token
689+
// No need to validate customer and customer address while saving customer reset password token
687690
$this->disableAddressValidation($customer);
691+
$this->setIgnoreValidationFlag($customer);
688692

689693
//Validate Token and new password strength
690694
$this->validateResetPasswordToken($customer->getId(), $resetToken);
@@ -1029,6 +1033,7 @@ private function changePasswordForCustomer($customer, $currentPassword, $newPass
10291033
$this->checkPasswordStrength($newPassword);
10301034
$customerSecure->setPasswordHash($this->createPasswordHash($newPassword));
10311035
$this->destroyCustomerSessions($customer->getId());
1036+
$this->disableAddressValidation($customer);
10321037
$this->customerRepository->save($customer);
10331038

10341039
return true;

0 commit comments

Comments
 (0)