From 3d03cb40b00f1c978bd1dac5441536fc41be790f Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Wed, 28 Aug 2019 13:10:39 +0300 Subject: [PATCH] magento/magento2#: Replace deprecated Magento\Customer\Model\Customer::isConfirmationRequired method --- app/code/Magento/Customer/Model/Customer.php | 33 ++++++++----- .../Customer/Model/ResourceModel/Customer.php | 37 ++++++++++---- app/code/Magento/Customer/Model/Session.php | 49 ++++++++++++++----- 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/app/code/Magento/Customer/Model/Customer.php b/app/code/Magento/Customer/Model/Customer.php index 1287dbe5df708..82a9514b528d0 100644 --- a/app/code/Magento/Customer/Model/Customer.php +++ b/app/code/Magento/Customer/Model/Customer.php @@ -394,7 +394,9 @@ public function getSharingConfig() public function authenticate($login, $password) { $this->loadByEmail($login); - if ($this->getConfirmation() && $this->isConfirmationRequired()) { + if ($this->getConfirmation() && + $this->accountConfirmation->isConfirmationRequired($this->getWebsiteId(), $this->getId(), $this->getEmail()) + ) { throw new EmailNotConfirmedException( __("This account isn't confirmed. Verify and try again.") ); @@ -415,8 +417,9 @@ public function authenticate($login, $password) /** * Load customer by email * - * @param string $customerEmail - * @return $this + * @param string $customerEmail + * @return $this + * @throws \Magento\Framework\Exception\LocalizedException */ public function loadByEmail($customerEmail) { @@ -427,8 +430,9 @@ public function loadByEmail($customerEmail) /** * Change customer password * - * @param string $newPassword - * @return $this + * @param string $newPassword + * @return $this + * @throws \Magento\Framework\Exception\LocalizedException */ public function changePassword($newPassword) { @@ -440,6 +444,7 @@ public function changePassword($newPassword) * Get full customer name * * @return string + * @throws \Magento\Framework\Exception\LocalizedException */ public function getName() { @@ -462,8 +467,9 @@ public function getName() /** * Add address to address collection * - * @param Address $address - * @return $this + * @param Address $address + * @return $this + * @throws \Magento\Framework\Exception\LocalizedException */ public function addAddress(Address $address) { @@ -486,7 +492,8 @@ public function getAddressById($addressId) * Getting customer address object from collection by identifier * * @param int $addressId - * @return Address + * @return \Magento\Framework\DataObject + * @throws \Magento\Framework\Exception\LocalizedException */ public function getAddressItemById($addressId) { @@ -506,7 +513,8 @@ public function getAddressCollection() /** * Customer addresses collection * - * @return \Magento\Customer\Model\ResourceModel\Address\Collection + * @return ResourceModel\Address\Collection + * @throws \Magento\Framework\Exception\LocalizedException */ public function getAddressesCollection() { @@ -537,7 +545,8 @@ public function getAddresses() /** * Retrieve all customer attributes * - * @return Attribute[] + * @return array + * @throws \Magento\Framework\Exception\LocalizedException */ public function getAttributes() { @@ -591,7 +600,8 @@ public function hashPassword($password, $salt = true) * Validate password with salted hash * * @param string $password - * @return boolean + * @return bool + * @throws \Exception */ public function validatePassword($password) { @@ -805,6 +815,7 @@ public function isConfirmationRequired() */ public function getRandomConfirmationKey() { + // phpcs:ignore Magento2.Security.InsecureFunction return md5(uniqid()); } diff --git a/app/code/Magento/Customer/Model/ResourceModel/Customer.php b/app/code/Magento/Customer/Model/ResourceModel/Customer.php index 94196df6fe093..3ca8c7cc0ed76 100644 --- a/app/code/Magento/Customer/Model/ResourceModel/Customer.php +++ b/app/code/Magento/Customer/Model/ResourceModel/Customer.php @@ -6,6 +6,7 @@ namespace Magento\Customer\Model\ResourceModel; +use Magento\Customer\Model\AccountConfirmation; use Magento\Customer\Model\Customer\NotificationStorage; use Magento\Framework\App\ObjectManager; use Magento\Framework\Validator\Exception as ValidatorException; @@ -21,9 +22,9 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity { /** - * @var \Magento\Framework\Validator\Factory + * @var \Magento\Framework\Stdlib\DateTime */ - protected $_validatorFactory; + protected $dateTime; /** * Core store config @@ -33,14 +34,19 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity protected $_scopeConfig; /** - * @var \Magento\Framework\Stdlib\DateTime + * @var \Magento\Store\Model\StoreManagerInterface */ - protected $dateTime; + protected $storeManager; /** - * @var \Magento\Store\Model\StoreManagerInterface + * @var \Magento\Framework\Validator\Factory */ - protected $storeManager; + protected $_validatorFactory; + + /** + * @var AccountConfirmation + */ + private $accountConfirmation; /** * @var NotificationStorage @@ -48,6 +54,8 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity private $notificationStorage; /** + * Customer constructor. + * * @param \Magento\Eav\Model\Entity\Context $context * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite @@ -56,6 +64,7 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param array $data + * @param AccountConfirmation $accountConfirmation */ public function __construct( \Magento\Eav\Model\Entity\Context $context, @@ -65,15 +74,19 @@ public function __construct( \Magento\Framework\Validator\Factory $validatorFactory, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Store\Model\StoreManagerInterface $storeManager, - $data = [] + $data = [], + AccountConfirmation $accountConfirmation = null ) { parent::__construct($context, $entitySnapshot, $entityRelationComposite, $data); + $this->_scopeConfig = $scopeConfig; $this->_validatorFactory = $validatorFactory; $this->dateTime = $dateTime; - $this->storeManager = $storeManager; + $this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance() + ->get(AccountConfirmation::class); $this->setType('customer'); $this->setConnection('customer_read', 'customer_write'); + $this->storeManager = $storeManager; } /** @@ -144,7 +157,13 @@ protected function _beforeSave(\Magento\Framework\DataObject $customer) } // set confirmation key logic - if (!$customer->getId() && $customer->isConfirmationRequired()) { + if (!$customer->getId() && + $this->accountConfirmation->isConfirmationRequired( + $customer->getWebsiteId(), + $customer->getId(), + $customer->getEmail() + ) + ) { $customer->setConfirmation($customer->getRandomConfirmationKey()); } // remove customer confirmation key from database, if empty diff --git a/app/code/Magento/Customer/Model/Session.php b/app/code/Magento/Customer/Model/Session.php index 5900fed218edf..5dc5779c57baa 100644 --- a/app/code/Magento/Customer/Model/Session.php +++ b/app/code/Magento/Customer/Model/Session.php @@ -10,6 +10,7 @@ use Magento\Customer\Api\GroupManagementInterface; use Magento\Customer\Model\Config\Share; use Magento\Customer\Model\ResourceModel\Customer as ResourceCustomer; +use Magento\Framework\App\ObjectManager; /** * Customer session model @@ -17,6 +18,7 @@ * @api * @method string getNoReferer() * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) * @since 100.0.2 */ class Session extends \Magento\Framework\Session\SessionManager @@ -107,6 +109,8 @@ class Session extends \Magento\Framework\Session\SessionManager protected $response; /** + * Session constructor. + * * @param \Magento\Framework\App\Request\Http $request * @param \Magento\Framework\Session\SidResolverInterface $sidResolver * @param \Magento\Framework\Session\Config\ConfigInterface $sessionConfig @@ -118,7 +122,7 @@ class Session extends \Magento\Framework\Session\SessionManager * @param \Magento\Framework\App\State $appState * @param Share $configShare * @param \Magento\Framework\Url\Helper\Data $coreUrl - * @param \Magento\Customer\Model\Url $customerUrl + * @param Url $customerUrl * @param ResourceCustomer $customerResource * @param CustomerFactory $customerFactory * @param \Magento\Framework\UrlFactory $urlFactory @@ -128,6 +132,7 @@ class Session extends \Magento\Framework\Session\SessionManager * @param CustomerRepositoryInterface $customerRepository * @param GroupManagementInterface $groupManagement * @param \Magento\Framework\App\Response\Http $response + * @param AccountConfirmation $accountConfirmation * @throws \Magento\Framework\Exception\SessionException * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -152,18 +157,20 @@ public function __construct( \Magento\Framework\App\Http\Context $httpContext, CustomerRepositoryInterface $customerRepository, GroupManagementInterface $groupManagement, - \Magento\Framework\App\Response\Http $response + \Magento\Framework\App\Response\Http $response, + AccountConfirmation $accountConfirmation = null ) { - $this->_coreUrl = $coreUrl; - $this->_customerUrl = $customerUrl; $this->_configShare = $configShare; + $this->_coreUrl = $coreUrl; $this->_customerResource = $customerResource; + $this->_customerUrl = $customerUrl; $this->_customerFactory = $customerFactory; - $this->_urlFactory = $urlFactory; - $this->_session = $session; - $this->customerRepository = $customerRepository; $this->_eventManager = $eventManager; $this->_httpContext = $httpContext; + $this->_session = $session; + $this->_urlFactory = $urlFactory; + $this->customerRepository = $customerRepository; + parent::__construct( $request, $sidResolver, @@ -175,9 +182,11 @@ public function __construct( $cookieMetadataFactory, $appState ); + $this->_eventManager->dispatch('customer_session_init', ['customer_session' => $this]); + $this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance() + ->get(AccountConfirmation::class); $this->groupManagement = $groupManagement; $this->response = $response; - $this->_eventManager->dispatch('customer_session_init', ['customer_session' => $this]); } /** @@ -216,6 +225,8 @@ public function setCustomerData(CustomerData $customer) * Retrieve customer model object * * @return CustomerData + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getCustomerData() { @@ -266,7 +277,12 @@ public function setCustomer(Customer $customerModel) \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID ); $this->setCustomerId($customerModel->getId()); - if (!$customerModel->isConfirmationRequired() && $customerModel->getConfirmation()) { + $accountConfirmationRequired = $this->accountConfirmation->isConfirmationRequired( + $customerModel->getWebsiteId(), + $customerModel->getId(), + $customerModel->getEmail() + ); + if (!$accountConfirmationRequired && $customerModel->getConfirmation()) { $customerModel->setConfirmation(null)->save(); } @@ -354,10 +370,11 @@ public function setCustomerGroupId($id) } /** - * Get customer group id - * If customer is not logged in system, 'not logged in' group id will be returned + * Get customer group id. If customer is not logged in system, 'not logged in' group id will be returned. * - * @return int + * @return int|null + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getCustomerGroupId() { @@ -407,6 +424,8 @@ public function checkCustomerId($customerId) } /** + * Sets customer as logged in + * * @param Customer $customer * @return $this */ @@ -420,6 +439,8 @@ public function setCustomerAsLoggedIn($customer) } /** + * Sets customer data as logged in + * * @param CustomerData $customer * @return $this */ @@ -521,6 +542,8 @@ protected function _setAuthUrl($key, $url) * Logout without dispatching event * * @return $this + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function _logout() { @@ -567,6 +590,8 @@ public function regenerateId() } /** + * Creates URL factory + * * @return \Magento\Framework\UrlInterface */ protected function _createUrl()