Skip to content

Commit 782acf6

Browse files
authored
Merge pull request #5380 from magento-tsg/2.4.0-develop-pr17
[TSG] Fixes for 2.4 (pr17) (2.4.0-develop)
2 parents dab9a89 + 4b55e4d commit 782acf6

File tree

9 files changed

+242
-137
lines changed

9 files changed

+242
-137
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Api;
9+
10+
/**
11+
* Interface for cleaning customer session data.
12+
*/
13+
interface SessionCleanerInterface
14+
{
15+
/**
16+
* Destroy all active customer sessions related to given customer id, including current session.
17+
*
18+
* @param int $customerId
19+
* @return void
20+
*/
21+
public function clearFor(int $customerId): void;
22+
}

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

Lines changed: 57 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Customer\Api\Data\AddressInterface;
1414
use Magento\Customer\Api\Data\CustomerInterface;
1515
use Magento\Customer\Api\Data\ValidationResultsInterfaceFactory;
16+
use Magento\Customer\Api\SessionCleanerInterface;
1617
use Magento\Customer\Helper\View as CustomerViewHelper;
1718
use Magento\Customer\Model\Config\Share as ConfigShare;
1819
use Magento\Customer\Model\Customer as CustomerModel;
@@ -68,104 +69,104 @@ class AccountManagement implements AccountManagementInterface
6869
/**
6970
* Configuration paths for create account email template
7071
*
71-
* @deprecated Get rid of Helpers in Password Security Management
72-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
72+
* @deprecated get rid of Helpers in Password Security Management.
73+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
7374
*/
7475
const XML_PATH_REGISTER_EMAIL_TEMPLATE = 'customer/create_account/email_template';
7576

7677
/**
7778
* Configuration paths for register no password email template
7879
*
79-
* @deprecated Get rid of Helpers in Password Security Management
80-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
80+
* @deprecated get rid of Helpers in Password Security Management.
81+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_REGISTER_NO_PASSWORD_EMAIL_TEMPLATE
8182
*/
8283
const XML_PATH_REGISTER_NO_PASSWORD_EMAIL_TEMPLATE = 'customer/create_account/email_no_password_template';
8384

8485
/**
8586
* Configuration paths for remind email identity
8687
*
87-
* @deprecated Get rid of Helpers in Password Security Management
88-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
88+
* @deprecated get rid of Helpers in Password Security Management.
89+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_REGISTER_EMAIL_IDENTITY
8990
*/
9091
const XML_PATH_REGISTER_EMAIL_IDENTITY = 'customer/create_account/email_identity';
9192

9293
/**
9394
* Configuration paths for remind email template
9495
*
95-
* @deprecated Get rid of Helpers in Password Security Management
96-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
96+
* @deprecated get rid of Helpers in Password Security Management.
97+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_REMIND_EMAIL_TEMPLATE
9798
*/
9899
const XML_PATH_REMIND_EMAIL_TEMPLATE = 'customer/password/remind_email_template';
99100

100101
/**
101102
* Configuration paths for forgot email email template
102103
*
103-
* @deprecated Get rid of Helpers in Password Security Management
104-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
104+
* @deprecated get rid of Helpers in Password Security Management.
105+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_FORGOT_EMAIL_TEMPLATE
105106
*/
106107
const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'customer/password/forgot_email_template';
107108

108109
/**
109110
* Configuration paths for forgot email identity
110111
*
111-
* @deprecated Get rid of Helpers in Password Security Management
112-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
112+
* @deprecated get rid of Helpers in Password Security Management.
113+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY
113114
*/
114115
const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';
115116

116117
/**
117118
* Configuration paths for account confirmation required
118119
*
119-
* @deprecated Get rid of Helpers in Password Security Management
120+
* @deprecated get rid of Helpers in Password Security Management.
120121
* @see AccountConfirmation::XML_PATH_IS_CONFIRM
121122
*/
122123
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
123124

124125
/**
125126
* Configuration paths for account confirmation email template
126127
*
127-
* @deprecated Get rid of Helpers in Password Security Management
128-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
128+
* @deprecated get rid of Helpers in Password Security Management.
129+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_CONFIRM_EMAIL_TEMPLATE
129130
*/
130131
const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';
131132

132133
/**
133134
* Configuration paths for confirmation confirmed email template
134135
*
135-
* @deprecated Get rid of Helpers in Password Security Management
136-
* @see EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
136+
* @deprecated get rid of Helpers in Password Security Management.
137+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_CONFIRMED_EMAIL_TEMPLATE
137138
*/
138139
const XML_PATH_CONFIRMED_EMAIL_TEMPLATE = 'customer/create_account/email_confirmed_template';
139140

140141
/**
141142
* Constants for the type of new account email to be sent
142143
*
143-
* @deprecated Get rid of Helpers in Password Security Management
144-
* @see EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED
144+
* @deprecated get rid of Helpers in Password Security Management.
145+
* @see \Magento\Customer\Model\EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED
145146
*/
146147
const NEW_ACCOUNT_EMAIL_REGISTERED = 'registered';
147148

148149
/**
149150
* Welcome email, when password setting is required
150151
*
151-
* @deprecated Get rid of Helpers in Password Security Management
152-
* @see EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED
152+
* @deprecated get rid of Helpers in Password Security Management.
153+
* @see \Magento\Customer\Model\EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD
153154
*/
154155
const NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD = 'registered_no_password';
155156

156157
/**
157158
* Welcome email, when confirmation is enabled
158159
*
159-
* @deprecated Get rid of Helpers in Password Security Management
160-
* @see EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED
160+
* @deprecated get rid of Helpers in Password Security Management.
161+
* @see \Magento\Customer\Model\EmailNotificationInterface::NEW_ACCOUNT_EMAIL_CONFIRMATION
161162
*/
162163
const NEW_ACCOUNT_EMAIL_CONFIRMATION = 'confirmation';
163164

164165
/**
165166
* Confirmation email, when account is confirmed
166167
*
167-
* @deprecated Get rid of Helpers in Password Security Management
168-
* @see EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED
168+
* @deprecated get rid of Helpers in Password Security Management.
169+
* @see \Magento\Customer\Model\EmailNotificationInterface::NEW_ACCOUNT_EMAIL_CONFIRMED
169170
*/
170171
const NEW_ACCOUNT_EMAIL_CONFIRMED = 'confirmed';
171172

@@ -191,15 +192,16 @@ class AccountManagement implements AccountManagementInterface
191192
/**
192193
* Configuration path to customer reset password email template
193194
*
194-
* @deprecated Get rid of Helpers in Password Security Management
195-
* @see Magento/Customer/Model/EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE
195+
* @deprecated get rid of Helpers in Password Security Management.
196+
* @see \Magento\Customer\Model\EmailNotification::XML_PATH_RESET_PASSWORD_TEMPLATE
196197
*/
197198
const XML_PATH_RESET_PASSWORD_TEMPLATE = 'customer/password/reset_password_template';
198199

199200
/**
200201
* Minimum password length
201202
*
202-
* @deprecated Get rid of Helpers in Password Security Management
203+
* @deprecated get rid of Helpers in Password Security Management.
204+
* @see \Magento\Customer\Model\AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH
203205
*/
204206
const MIN_PASSWORD_LENGTH = 6;
205207

@@ -283,21 +285,6 @@ class AccountManagement implements AccountManagementInterface
283285
*/
284286
private $transportBuilder;
285287

286-
/**
287-
* @var SessionManagerInterface
288-
*/
289-
private $sessionManager;
290-
291-
/**
292-
* @var SaveHandlerInterface
293-
*/
294-
private $saveHandler;
295-
296-
/**
297-
* @var CollectionFactory
298-
*/
299-
private $visitorCollectionFactory;
300-
301288
/**
302289
* @var DataObjectProcessor
303290
*/
@@ -383,6 +370,11 @@ class AccountManagement implements AccountManagementInterface
383370
*/
384371
private $getByToken;
385372

373+
/**
374+
* @var SessionCleanerInterface
375+
*/
376+
private $sessionCleaner;
377+
386378
/**
387379
* @param CustomerFactory $customerFactory
388380
* @param ManagerInterface $eventManager
@@ -417,10 +409,12 @@ class AccountManagement implements AccountManagementInterface
417409
* @param AddressRegistry|null $addressRegistry
418410
* @param GetCustomerByToken|null $getByToken
419411
* @param AllowedCountries|null $allowedCountriesReader
412+
* @param SessionCleanerInterface|null $sessionCleaner
420413
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
421414
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
422415
* @SuppressWarnings(PHPMD.NPathComplexity)
423416
* @SuppressWarnings(PHPMD.LongVariable)
417+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
424418
*/
425419
public function __construct(
426420
CustomerFactory $customerFactory,
@@ -455,7 +449,8 @@ public function __construct(
455449
SearchCriteriaBuilder $searchCriteriaBuilder = null,
456450
AddressRegistry $addressRegistry = null,
457451
GetCustomerByToken $getByToken = null,
458-
AllowedCountries $allowedCountriesReader = null
452+
AllowedCountries $allowedCountriesReader = null,
453+
SessionCleanerInterface $sessionCleaner = null
459454
) {
460455
$this->customerFactory = $customerFactory;
461456
$this->eventManager = $eventManager;
@@ -486,12 +481,6 @@ public function __construct(
486481
$this->dateTimeFactory = $dateTimeFactory ?: $objectManager->get(DateTimeFactory::class);
487482
$this->accountConfirmation = $accountConfirmation ?: $objectManager
488483
->get(AccountConfirmation::class);
489-
$this->sessionManager = $sessionManager
490-
?: $objectManager->get(SessionManagerInterface::class);
491-
$this->saveHandler = $saveHandler
492-
?: $objectManager->get(SaveHandlerInterface::class);
493-
$this->visitorCollectionFactory = $visitorCollectionFactory
494-
?: $objectManager->get(CollectionFactory::class);
495484
$this->searchCriteriaBuilder = $searchCriteriaBuilder
496485
?: $objectManager->get(SearchCriteriaBuilder::class);
497486
$this->addressRegistry = $addressRegistry
@@ -500,6 +489,7 @@ public function __construct(
500489
?: $objectManager->get(GetCustomerByToken::class);
501490
$this->allowedCountriesReader = $allowedCountriesReader
502491
?: $objectManager->get(AllowedCountries::class);
492+
$this->sessionCleaner = $sessionCleaner ?? $objectManager->get(SessionCleanerInterface::class);
503493
}
504494

505495
/**
@@ -538,7 +528,10 @@ public function resendConfirmation($email, $websiteId = null, $redirectUrl = '')
538528
} catch (MailException $e) {
539529
// If we are not able to send a new account email, this should be ignored
540530
$this->logger->critical($e);
531+
532+
return false;
541533
}
534+
542535
return true;
543536
}
544537

@@ -685,16 +678,18 @@ public function initiatePasswordReset($email, $template, $websiteId = null)
685678
*/
686679
private function handleUnknownTemplate($template)
687680
{
688-
$phrase = __(
689-
'Invalid value of "%value" provided for the %fieldName field. Possible values: %template1 or %template2.',
690-
[
691-
'value' => $template,
692-
'fieldName' => 'template',
693-
'template1' => AccountManagement::EMAIL_REMINDER,
694-
'template2' => AccountManagement::EMAIL_RESET
695-
]
681+
throw new InputException(
682+
__(
683+
'Invalid value of "%value" provided for the %fieldName field. '
684+
. 'Possible values: %template1 or %template2.',
685+
[
686+
'value' => $template,
687+
'fieldName' => 'template',
688+
'template1' => AccountManagement::EMAIL_REMINDER,
689+
'template2' => AccountManagement::EMAIL_RESET
690+
]
691+
)
696692
);
697-
throw new InputException($phrase);
698693
}
699694

700695
/**
@@ -725,7 +720,7 @@ public function resetPassword($email, $resetToken, $newPassword)
725720
$customerSecure->setRpToken(null);
726721
$customerSecure->setRpTokenCreatedAt(null);
727722
$customerSecure->setPasswordHash($this->createPasswordHash($newPassword));
728-
$this->destroyCustomerSessions($customer->getId());
723+
$this->sessionCleaner->clearFor((int)$customer->getId());
729724
$this->customerRepository->save($customer);
730725

731726
return true;
@@ -872,6 +867,7 @@ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash
872867
if ($customer->getId()) {
873868
$customer = $this->customerRepository->get($customer->getEmail());
874869
$websiteId = $customer->getWebsiteId();
870+
875871
if ($this->isCustomerInStore($websiteId, $customer->getStoreId())) {
876872
throw new InputException(__('This customer already exists in this store.'));
877873
}
@@ -1050,7 +1046,7 @@ private function changePasswordForCustomer($customer, $currentPassword, $newPass
10501046
$customerSecure->setRpTokenCreatedAt(null);
10511047
$this->checkPasswordStrength($newPassword);
10521048
$customerSecure->setPasswordHash($this->createPasswordHash($newPassword));
1053-
$this->destroyCustomerSessions($customer->getId());
1049+
$this->sessionCleaner->clearFor((int)$customer->getId());
10541050
$this->disableAddressValidation($customer);
10551051
$this->customerRepository->save($customer);
10561052

@@ -1619,36 +1615,6 @@ private function getEmailNotification()
16191615
}
16201616
}
16211617

1622-
/**
1623-
* Destroy all active customer sessions by customer id (current session will not be destroyed).
1624-
*
1625-
* Customer sessions which should be deleted are collecting from the "customer_visitor" table considering
1626-
* configured session lifetime.
1627-
*
1628-
* @param string|int $customerId
1629-
* @return void
1630-
*/
1631-
private function destroyCustomerSessions($customerId)
1632-
{
1633-
$sessionLifetime = $this->scopeConfig->getValue(
1634-
\Magento\Framework\Session\Config::XML_PATH_COOKIE_LIFETIME,
1635-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
1636-
);
1637-
$dateTime = $this->dateTimeFactory->create();
1638-
$activeSessionsTime = $dateTime->setTimestamp($dateTime->getTimestamp() - $sessionLifetime)
1639-
->format(DateTime::DATETIME_PHP_FORMAT);
1640-
/** @var \Magento\Customer\Model\ResourceModel\Visitor\Collection $visitorCollection */
1641-
$visitorCollection = $this->visitorCollectionFactory->create();
1642-
$visitorCollection->addFieldToFilter('customer_id', $customerId);
1643-
$visitorCollection->addFieldToFilter('last_visit_at', ['from' => $activeSessionsTime]);
1644-
$visitorCollection->addFieldToFilter('session_id', ['neq' => $this->sessionManager->getSessionId()]);
1645-
/** @var \Magento\Customer\Model\Visitor $visitor */
1646-
foreach ($visitorCollection->getItems() as $visitor) {
1647-
$sessionId = $visitor->getSessionId();
1648-
$this->saveHandler->destroy($sessionId);
1649-
}
1650-
}
1651-
16521618
/**
16531619
* Set ignore_validation_flag for reset password flow to skip unnecessary address and customer validation
16541620
*

0 commit comments

Comments
 (0)