Skip to content

Commit 3d03cb4

Browse files
committed
magento/magento2#: Replace deprecated Magento\Customer\Model\Customer::isConfirmationRequired method
1 parent 7cf99d1 commit 3d03cb4

File tree

3 files changed

+87
-32
lines changed

3 files changed

+87
-32
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ public function getSharingConfig()
394394
public function authenticate($login, $password)
395395
{
396396
$this->loadByEmail($login);
397-
if ($this->getConfirmation() && $this->isConfirmationRequired()) {
397+
if ($this->getConfirmation() &&
398+
$this->accountConfirmation->isConfirmationRequired($this->getWebsiteId(), $this->getId(), $this->getEmail())
399+
) {
398400
throw new EmailNotConfirmedException(
399401
__("This account isn't confirmed. Verify and try again.")
400402
);
@@ -415,8 +417,9 @@ public function authenticate($login, $password)
415417
/**
416418
* Load customer by email
417419
*
418-
* @param string $customerEmail
419-
* @return $this
420+
* @param string $customerEmail
421+
* @return $this
422+
* @throws \Magento\Framework\Exception\LocalizedException
420423
*/
421424
public function loadByEmail($customerEmail)
422425
{
@@ -427,8 +430,9 @@ public function loadByEmail($customerEmail)
427430
/**
428431
* Change customer password
429432
*
430-
* @param string $newPassword
431-
* @return $this
433+
* @param string $newPassword
434+
* @return $this
435+
* @throws \Magento\Framework\Exception\LocalizedException
432436
*/
433437
public function changePassword($newPassword)
434438
{
@@ -440,6 +444,7 @@ public function changePassword($newPassword)
440444
* Get full customer name
441445
*
442446
* @return string
447+
* @throws \Magento\Framework\Exception\LocalizedException
443448
*/
444449
public function getName()
445450
{
@@ -462,8 +467,9 @@ public function getName()
462467
/**
463468
* Add address to address collection
464469
*
465-
* @param Address $address
466-
* @return $this
470+
* @param Address $address
471+
* @return $this
472+
* @throws \Magento\Framework\Exception\LocalizedException
467473
*/
468474
public function addAddress(Address $address)
469475
{
@@ -486,7 +492,8 @@ public function getAddressById($addressId)
486492
* Getting customer address object from collection by identifier
487493
*
488494
* @param int $addressId
489-
* @return Address
495+
* @return \Magento\Framework\DataObject
496+
* @throws \Magento\Framework\Exception\LocalizedException
490497
*/
491498
public function getAddressItemById($addressId)
492499
{
@@ -506,7 +513,8 @@ public function getAddressCollection()
506513
/**
507514
* Customer addresses collection
508515
*
509-
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
516+
* @return ResourceModel\Address\Collection
517+
* @throws \Magento\Framework\Exception\LocalizedException
510518
*/
511519
public function getAddressesCollection()
512520
{
@@ -537,7 +545,8 @@ public function getAddresses()
537545
/**
538546
* Retrieve all customer attributes
539547
*
540-
* @return Attribute[]
548+
* @return array
549+
* @throws \Magento\Framework\Exception\LocalizedException
541550
*/
542551
public function getAttributes()
543552
{
@@ -591,7 +600,8 @@ public function hashPassword($password, $salt = true)
591600
* Validate password with salted hash
592601
*
593602
* @param string $password
594-
* @return boolean
603+
* @return bool
604+
* @throws \Exception
595605
*/
596606
public function validatePassword($password)
597607
{
@@ -805,6 +815,7 @@ public function isConfirmationRequired()
805815
*/
806816
public function getRandomConfirmationKey()
807817
{
818+
// phpcs:ignore Magento2.Security.InsecureFunction
808819
return md5(uniqid());
809820
}
810821

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Customer\Model\ResourceModel;
88

9+
use Magento\Customer\Model\AccountConfirmation;
910
use Magento\Customer\Model\Customer\NotificationStorage;
1011
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Validator\Exception as ValidatorException;
@@ -21,9 +22,9 @@
2122
class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
2223
{
2324
/**
24-
* @var \Magento\Framework\Validator\Factory
25+
* @var \Magento\Framework\Stdlib\DateTime
2526
*/
26-
protected $_validatorFactory;
27+
protected $dateTime;
2728

2829
/**
2930
* Core store config
@@ -33,21 +34,28 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
3334
protected $_scopeConfig;
3435

3536
/**
36-
* @var \Magento\Framework\Stdlib\DateTime
37+
* @var \Magento\Store\Model\StoreManagerInterface
3738
*/
38-
protected $dateTime;
39+
protected $storeManager;
3940

4041
/**
41-
* @var \Magento\Store\Model\StoreManagerInterface
42+
* @var \Magento\Framework\Validator\Factory
4243
*/
43-
protected $storeManager;
44+
protected $_validatorFactory;
45+
46+
/**
47+
* @var AccountConfirmation
48+
*/
49+
private $accountConfirmation;
4450

4551
/**
4652
* @var NotificationStorage
4753
*/
4854
private $notificationStorage;
4955

5056
/**
57+
* Customer constructor.
58+
*
5159
* @param \Magento\Eav\Model\Entity\Context $context
5260
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
5361
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite
@@ -56,6 +64,7 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
5664
* @param \Magento\Framework\Stdlib\DateTime $dateTime
5765
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
5866
* @param array $data
67+
* @param AccountConfirmation $accountConfirmation
5968
*/
6069
public function __construct(
6170
\Magento\Eav\Model\Entity\Context $context,
@@ -65,15 +74,19 @@ public function __construct(
6574
\Magento\Framework\Validator\Factory $validatorFactory,
6675
\Magento\Framework\Stdlib\DateTime $dateTime,
6776
\Magento\Store\Model\StoreManagerInterface $storeManager,
68-
$data = []
77+
$data = [],
78+
AccountConfirmation $accountConfirmation = null
6979
) {
7080
parent::__construct($context, $entitySnapshot, $entityRelationComposite, $data);
81+
7182
$this->_scopeConfig = $scopeConfig;
7283
$this->_validatorFactory = $validatorFactory;
7384
$this->dateTime = $dateTime;
74-
$this->storeManager = $storeManager;
85+
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
86+
->get(AccountConfirmation::class);
7587
$this->setType('customer');
7688
$this->setConnection('customer_read', 'customer_write');
89+
$this->storeManager = $storeManager;
7790
}
7891

7992
/**
@@ -144,7 +157,13 @@ protected function _beforeSave(\Magento\Framework\DataObject $customer)
144157
}
145158

146159
// set confirmation key logic
147-
if (!$customer->getId() && $customer->isConfirmationRequired()) {
160+
if (!$customer->getId() &&
161+
$this->accountConfirmation->isConfirmationRequired(
162+
$customer->getWebsiteId(),
163+
$customer->getId(),
164+
$customer->getEmail()
165+
)
166+
) {
148167
$customer->setConfirmation($customer->getRandomConfirmationKey());
149168
}
150169
// remove customer confirmation key from database, if empty

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
use Magento\Customer\Api\GroupManagementInterface;
1111
use Magento\Customer\Model\Config\Share;
1212
use Magento\Customer\Model\ResourceModel\Customer as ResourceCustomer;
13+
use Magento\Framework\App\ObjectManager;
1314

1415
/**
1516
* Customer session model
1617
*
1718
* @api
1819
* @method string getNoReferer()
1920
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2022
* @since 100.0.2
2123
*/
2224
class Session extends \Magento\Framework\Session\SessionManager
@@ -107,6 +109,8 @@ class Session extends \Magento\Framework\Session\SessionManager
107109
protected $response;
108110

109111
/**
112+
* Session constructor.
113+
*
110114
* @param \Magento\Framework\App\Request\Http $request
111115
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
112116
* @param \Magento\Framework\Session\Config\ConfigInterface $sessionConfig
@@ -118,7 +122,7 @@ class Session extends \Magento\Framework\Session\SessionManager
118122
* @param \Magento\Framework\App\State $appState
119123
* @param Share $configShare
120124
* @param \Magento\Framework\Url\Helper\Data $coreUrl
121-
* @param \Magento\Customer\Model\Url $customerUrl
125+
* @param Url $customerUrl
122126
* @param ResourceCustomer $customerResource
123127
* @param CustomerFactory $customerFactory
124128
* @param \Magento\Framework\UrlFactory $urlFactory
@@ -128,6 +132,7 @@ class Session extends \Magento\Framework\Session\SessionManager
128132
* @param CustomerRepositoryInterface $customerRepository
129133
* @param GroupManagementInterface $groupManagement
130134
* @param \Magento\Framework\App\Response\Http $response
135+
* @param AccountConfirmation $accountConfirmation
131136
* @throws \Magento\Framework\Exception\SessionException
132137
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
133138
*/
@@ -152,18 +157,20 @@ public function __construct(
152157
\Magento\Framework\App\Http\Context $httpContext,
153158
CustomerRepositoryInterface $customerRepository,
154159
GroupManagementInterface $groupManagement,
155-
\Magento\Framework\App\Response\Http $response
160+
\Magento\Framework\App\Response\Http $response,
161+
AccountConfirmation $accountConfirmation = null
156162
) {
157-
$this->_coreUrl = $coreUrl;
158-
$this->_customerUrl = $customerUrl;
159163
$this->_configShare = $configShare;
164+
$this->_coreUrl = $coreUrl;
160165
$this->_customerResource = $customerResource;
166+
$this->_customerUrl = $customerUrl;
161167
$this->_customerFactory = $customerFactory;
162-
$this->_urlFactory = $urlFactory;
163-
$this->_session = $session;
164-
$this->customerRepository = $customerRepository;
165168
$this->_eventManager = $eventManager;
166169
$this->_httpContext = $httpContext;
170+
$this->_session = $session;
171+
$this->_urlFactory = $urlFactory;
172+
$this->customerRepository = $customerRepository;
173+
167174
parent::__construct(
168175
$request,
169176
$sidResolver,
@@ -175,9 +182,11 @@ public function __construct(
175182
$cookieMetadataFactory,
176183
$appState
177184
);
185+
$this->_eventManager->dispatch('customer_session_init', ['customer_session' => $this]);
186+
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
187+
->get(AccountConfirmation::class);
178188
$this->groupManagement = $groupManagement;
179189
$this->response = $response;
180-
$this->_eventManager->dispatch('customer_session_init', ['customer_session' => $this]);
181190
}
182191

183192
/**
@@ -216,6 +225,8 @@ public function setCustomerData(CustomerData $customer)
216225
* Retrieve customer model object
217226
*
218227
* @return CustomerData
228+
* @throws \Magento\Framework\Exception\LocalizedException
229+
* @throws \Magento\Framework\Exception\NoSuchEntityException
219230
*/
220231
public function getCustomerData()
221232
{
@@ -266,7 +277,12 @@ public function setCustomer(Customer $customerModel)
266277
\Magento\Customer\Model\Group::NOT_LOGGED_IN_ID
267278
);
268279
$this->setCustomerId($customerModel->getId());
269-
if (!$customerModel->isConfirmationRequired() && $customerModel->getConfirmation()) {
280+
$accountConfirmationRequired = $this->accountConfirmation->isConfirmationRequired(
281+
$customerModel->getWebsiteId(),
282+
$customerModel->getId(),
283+
$customerModel->getEmail()
284+
);
285+
if (!$accountConfirmationRequired && $customerModel->getConfirmation()) {
270286
$customerModel->setConfirmation(null)->save();
271287
}
272288

@@ -354,10 +370,11 @@ public function setCustomerGroupId($id)
354370
}
355371

356372
/**
357-
* Get customer group id
358-
* If customer is not logged in system, 'not logged in' group id will be returned
373+
* Get customer group id. If customer is not logged in system, 'not logged in' group id will be returned.
359374
*
360-
* @return int
375+
* @return int|null
376+
* @throws \Magento\Framework\Exception\LocalizedException
377+
* @throws \Magento\Framework\Exception\NoSuchEntityException
361378
*/
362379
public function getCustomerGroupId()
363380
{
@@ -407,6 +424,8 @@ public function checkCustomerId($customerId)
407424
}
408425

409426
/**
427+
* Sets customer as logged in
428+
*
410429
* @param Customer $customer
411430
* @return $this
412431
*/
@@ -420,6 +439,8 @@ public function setCustomerAsLoggedIn($customer)
420439
}
421440

422441
/**
442+
* Sets customer data as logged in
443+
*
423444
* @param CustomerData $customer
424445
* @return $this
425446
*/
@@ -521,6 +542,8 @@ protected function _setAuthUrl($key, $url)
521542
* Logout without dispatching event
522543
*
523544
* @return $this
545+
* @throws \Magento\Framework\Exception\LocalizedException
546+
* @throws \Magento\Framework\Exception\NoSuchEntityException
524547
*/
525548
protected function _logout()
526549
{
@@ -567,6 +590,8 @@ public function regenerateId()
567590
}
568591

569592
/**
593+
* Creates URL factory
594+
*
570595
* @return \Magento\Framework\UrlInterface
571596
*/
572597
protected function _createUrl()

0 commit comments

Comments
 (0)