Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 46 additions & 38 deletions app/code/Magento/Customer/Model/AccountManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\ValidationResultsInterfaceFactory;
use Magento\Customer\Model\EmailNotificationInterface;
use Magento\Customer\Helper\View as CustomerViewHelper;
use Magento\Customer\Model\Config\Share as ConfigShare;
use Magento\Customer\Model\Customer as CustomerModel;
Expand All @@ -22,6 +21,7 @@
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObjectFactory as ObjectFactory;
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
use Magento\Framework\Encryption\Helper\Security;
use Magento\Framework\Event\ManagerInterface;
Expand All @@ -30,22 +30,22 @@
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\State\ExpiredException;
use Magento\Framework\Exception\State\InputMismatchException;
use Magento\Framework\Exception\State\InvalidTransitionException;
use Magento\Framework\DataObjectFactory as ObjectFactory;
use Magento\Framework\Exception\State\UserLockedException;
use Magento\Framework\Registry;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface as PsrLogger;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Intl\DateTimeFactory;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Math\Random;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\StringUtils as StringHelper;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface as PsrLogger;

/**
* Handle various customer account actions
Expand Down Expand Up @@ -287,6 +287,11 @@ class AccountManagement implements AccountManagementInterface
*/
private $eavValidator;

/**
* @var DateTimeFactory
*/
private $dateTimeFactory;

/**
* @param CustomerFactory $customerFactory
* @param ManagerInterface $eventManager
Expand All @@ -311,6 +316,7 @@ class AccountManagement implements AccountManagementInterface
* @param CustomerModel $customerModel
* @param ObjectFactory $objectFactory
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
* @param DateTimeFactory $dateTimeFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -336,7 +342,8 @@ public function __construct(
DateTime $dateTime,
CustomerModel $customerModel,
ObjectFactory $objectFactory,
ExtensibleDataObjectConverter $extensibleDataObjectConverter
ExtensibleDataObjectConverter $extensibleDataObjectConverter,
DateTimeFactory $dateTimeFactory = null
) {
$this->customerFactory = $customerFactory;
$this->eventManager = $eventManager;
Expand All @@ -361,6 +368,7 @@ public function __construct(
$this->customerModel = $customerModel;
$this->objectFactory = $objectFactory;
$this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
$this->dateTimeFactory = $dateTimeFactory ?: ObjectManager::getInstance()->get(DateTimeFactory::class);
}

/**
Expand All @@ -370,7 +378,6 @@ public function __construct(
*/
private function getAuthentication()
{

if (!($this->authentication instanceof AuthenticationInterface)) {
return \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Customer\Model\AuthenticationInterface::class
Expand Down Expand Up @@ -603,16 +610,16 @@ protected function makeRequiredCharactersCheck($password)
$return = 0;

if (preg_match('/[0-9]+/', $password)) {
$counter ++;
$counter++;
}
if (preg_match('/[A-Z]+/', $password)) {
$counter ++;
$counter++;
}
if (preg_match('/[a-z]+/', $password)) {
$counter ++;
$counter++;
}
if (preg_match('/[^a-zA-Z0-9]+/', $password)) {
$counter ++;
$counter++;
}

if ($counter < $requiredNumber) {
Expand Down Expand Up @@ -717,12 +724,12 @@ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash
}
try {
foreach ($customerAddresses as $address) {
if ($address->getId()) {
if ($address->getId()) {
$newAddress = clone $address;
$newAddress->setId(null);
$newAddress->setCustomerId($customer->getId());
$this->addressRepository->save($newAddress);
} else {
} else {
$address->setCustomerId($customer->getId());
$this->addressRepository->save($address);
}
Expand Down Expand Up @@ -871,16 +878,14 @@ public function validate(CustomerInterface $customer)

$result = $this->getEavValidator()->isValid($customerModel);
if ($result === false && is_array($this->getEavValidator()->getMessages())) {
return $validationResults->setIsValid(false)
->setMessages(
call_user_func_array(
'array_merge',
$this->getEavValidator()->getMessages()
)
);
return $validationResults->setIsValid(false)->setMessages(
call_user_func_array(
'array_merge',
$this->getEavValidator()->getMessages()
)
);
}
return $validationResults->setIsValid(true)
->setMessages([]);
return $validationResults->setIsValid(true)->setMessages([]);
}

/**
Expand Down Expand Up @@ -930,10 +935,12 @@ public function isCustomerInStore($customerWebsiteId, $storeId)
private function validateResetPasswordToken($customerId, $resetPasswordLinkToken)
{
if (empty($customerId) || $customerId < 0) {
throw new InputException(__(
'Invalid value of "%value" provided for the %fieldName field.',
['value' => $customerId, 'fieldName' => 'customerId']
));
throw new InputException(
__(
'Invalid value of "%value" provided for the %fieldName field.',
['value' => $customerId, 'fieldName' => 'customerId']
)
);
}
if (!is_string($resetPasswordLinkToken) || empty($resetPasswordLinkToken)) {
$params = ['fieldName' => 'resetPasswordLinkToken'];
Expand Down Expand Up @@ -1090,12 +1097,11 @@ protected function sendEmailTemplate(
$email = $customer->getEmail();
}

$transport = $this->transportBuilder->setTemplateIdentifier($templateId)
->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars($templateParams)
->setFrom($this->scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId))
->addTo($email, $this->customerViewHelper->getCustomerName($customer))
->getTransport();
$transport = $this->transportBuilder->setTemplateIdentifier($templateId)->setTemplateOptions(
['area' => Area::AREA_FRONTEND, 'store' => $storeId]
)->setTemplateVars($templateParams)->setFrom(
$this->scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId)
)->addTo($email, $this->customerViewHelper->getCustomerName($customer))->getTransport();

$transport->sendMessage();

Expand Down Expand Up @@ -1159,8 +1165,8 @@ public function isResetPasswordLinkTokenExpired($rpToken, $rpTokenCreatedAt)

$expirationPeriod = $this->customerModel->getResetPasswordLinkExpirationPeriod();

$currentTimestamp = (new \DateTime())->getTimestamp();
$tokenTimestamp = (new \DateTime($rpTokenCreatedAt))->getTimestamp();
$currentTimestamp = $this->dateTimeFactory->create()->getTimestamp();
$tokenTimestamp = $this->dateTimeFactory->create($rpTokenCreatedAt)->getTimestamp();
if ($tokenTimestamp > $currentTimestamp) {
return true;
}
Expand Down Expand Up @@ -1196,7 +1202,9 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
if (is_string($passwordLinkToken) && !empty($passwordLinkToken)) {
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
$customerSecure->setRpToken($passwordLinkToken);
$customerSecure->setRpTokenCreatedAt((new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT));
$customerSecure->setRpTokenCreatedAt(
$this->dateTimeFactory->create()->format(DateTime::DATETIME_PHP_FORMAT)
);
$this->customerRepository->save($customer);
}
return true;
Expand Down Expand Up @@ -1285,8 +1293,8 @@ protected function getFullCustomerObject($customer)
// No need to flatten the custom attributes or nested objects since the only usage is for email templates and
// object passed for events
$mergedCustomerData = $this->customerRegistry->retrieveSecureData($customer->getId());
$customerData = $this->dataProcessor
->buildOutputDataArray($customer, '\Magento\Customer\Api\Data\CustomerInterface');
$customerData =
$this->dataProcessor->buildOutputDataArray($customer, '\Magento\Customer\Api\Data\CustomerInterface');
$mergedCustomerData->addData($customerData);
$mergedCustomerData->setData('name', $this->customerViewHelper->getCustomerName($customer));
return $mergedCustomerData;
Expand Down
Loading