Skip to content

magento/magento2#: Replace deprecated Magento\Customer\Model\Customer::isConfirmationRequired method #24334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
33 changes: 22 additions & 11 deletions app/code/Magento/Customer/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
);
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -440,6 +444,7 @@ public function changePassword($newPassword)
* Get full customer name
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getName()
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -537,7 +545,8 @@ public function getAddresses()
/**
* Retrieve all customer attributes
*
* @return Attribute[]
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAttributes()
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -805,6 +815,7 @@ public function isConfirmationRequired()
*/
public function getRandomConfirmationKey()
{
// phpcs:ignore Magento2.Security.InsecureFunction
return md5(uniqid());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, keep as it is.
Just ignore this rule:

// phpcs:ignore Magento2.Security.InsecureFunction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, @dmytro-ch!

implemented

Thank you!

}

Expand Down
37 changes: 28 additions & 9 deletions app/code/Magento/Customer/Model/ResourceModel/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -33,21 +34,28 @@ 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
*/
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
Expand All @@ -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,
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down
49 changes: 37 additions & 12 deletions app/code/Magento/Customer/Model/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
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
*
* @api
* @method string getNoReferer()
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
* @since 100.0.2
*/
class Session extends \Magento\Framework\Session\SessionManager
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
*/
Expand All @@ -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,
Expand All @@ -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]);
}

/**
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -407,6 +424,8 @@ public function checkCustomerId($customerId)
}

/**
* Sets customer as logged in
*
* @param Customer $customer
* @return $this
*/
Expand All @@ -420,6 +439,8 @@ public function setCustomerAsLoggedIn($customer)
}

/**
* Sets customer data as logged in
*
* @param CustomerData $customer
* @return $this
*/
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -567,6 +590,8 @@ public function regenerateId()
}

/**
* Creates URL factory
*
* @return \Magento\Framework\UrlInterface
*/
protected function _createUrl()
Expand Down