Skip to content

Commit 430a437

Browse files
author
roman
committed
MAGETWO-95400: Incorrect send-friend feature flow
1 parent 033fd58 commit 430a437

File tree

3 files changed

+174
-85
lines changed

3 files changed

+174
-85
lines changed

app/code/Magento/SendFriend/Controller/Product/Sendmail.php

Lines changed: 51 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\Controller\ResultFactory;
13-
use Magento\Framework\Exception\LocalizedException;
14-
use Magento\Captcha\Helper\Data;
15-
use Magento\Captcha\Model\DefaultModel;
16-
use Magento\Captcha\Observer\CaptchaStringResolver;
17-
use Magento\Authorization\Model\UserContextInterface;
18-
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\SendFriend\Model\CaptchaValidator;
1914

2015
/**
2116
* Class Sendmail. Represents request flow logic of 'sendmail' feature
@@ -35,24 +30,9 @@ class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPos
3530
protected $catalogSession;
3631

3732
/**
38-
* @var Data
33+
* @var CaptchaValidator
3934
*/
40-
private $captchaHelper;
41-
42-
/**
43-
* @var CaptchaStringResolver
44-
*/
45-
private $captchaStringResolver;
46-
47-
/**
48-
* @var UserContextInterface
49-
*/
50-
private $currentUser;
51-
52-
/**
53-
* @var CustomerRepositoryInterface
54-
*/
55-
private $customerRepository;
35+
private $captchaValidator;
5636

5737
/**
5838
* Sendmail class construct
@@ -64,12 +44,7 @@ class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPos
6444
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
6545
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
6646
* @param \Magento\Catalog\Model\Session $catalogSession
67-
* @param Data|null $captchaHelper
68-
* @param CaptchaStringResolver|null $captchaStringResolver
69-
* @param UserContextInterface|null $currentUser
70-
* @param CustomerRepositoryInterface|null $customerRepository
71-
*
72-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
47+
* @param CaptchaValidator|null $captchaValidator
7348
*/
7449
public function __construct(
7550
\Magento\Framework\App\Action\Context $context,
@@ -79,20 +54,12 @@ public function __construct(
7954
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
8055
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
8156
\Magento\Catalog\Model\Session $catalogSession,
82-
?Data $captchaHelper = null,
83-
?CaptchaStringResolver $captchaStringResolver = null,
84-
?UserContextInterface $currentUser = null,
85-
?CustomerRepositoryInterface $customerRepository = null
57+
CaptchaValidator $captchaValidator = null
8658
) {
8759
parent::__construct($context, $coreRegistry, $formKeyValidator, $sendFriend, $productRepository);
8860
$this->categoryRepository = $categoryRepository;
8961
$this->catalogSession = $catalogSession;
90-
$this->captchaHelper = $captchaHelper ?: ObjectManager::getInstance()->create(Data::class);
91-
$this->captchaStringResolver = $captchaStringResolver ?:
92-
ObjectManager::getInstance()->create(CaptchaStringResolver::class);
93-
$this->currentUser = $currentUser ?: ObjectManager::getInstance()->get(UserContextInterface::class);
94-
$this->customerRepository = $customerRepository ?:
95-
ObjectManager::getInstance()->create(CustomerRepositoryInterface::class);
62+
$this->captchaValidator = $captchaValidator ?: ObjectManager::getInstance()->create(CaptchaValidator::class);
9663
}
9764

9865
/**
@@ -137,7 +104,7 @@ public function execute()
137104
try {
138105
$validate = $this->sendFriend->validate();
139106

140-
$this->validateCaptcha();
107+
$this->captchaValidator->validateSending($this->getRequest());
141108

142109
if ($validate === true) {
143110
$this->sendFriend->send();
@@ -167,48 +134,48 @@ public function execute()
167134
$resultRedirect->setUrl($this->_redirect->error($url));
168135
return $resultRedirect;
169136
}
170-
171-
/**
172-
* Method validates captcha, if it's enabled for target form
173-
*
174-
* @throws LocalizedException
175-
*/
176-
private function validateCaptcha() : void
177-
{
178-
$captchaTargetFormName = 'product_sendtofriend_form';
179-
/** @var DefaultModel $captchaModel */
180-
$captchaModel = $this->captchaHelper->getCaptcha($captchaTargetFormName);
181-
182-
if ($captchaModel->isRequired()) {
183-
$word = $this->captchaStringResolver->resolve(
184-
$this->getRequest(),
185-
$captchaTargetFormName
186-
);
187-
188-
$isCorrectCaptcha = $captchaModel->isCorrect($word);
189-
190-
if (!$isCorrectCaptcha) {
191-
$this->logCaptchaAttempt($captchaModel);
192-
throw new LocalizedException(__('Incorrect CAPTCHA'));
193-
}
194-
}
195-
196-
$this->logCaptchaAttempt($captchaModel);
197-
}
198-
199-
/**
200-
* Log captcha attempts
201-
*
202-
* @param DefaultModel $captchaModel
203-
*/
204-
private function logCaptchaAttempt(DefaultModel $captchaModel) : void
205-
{
206-
$email = '';
207-
208-
if ($this->currentUser->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
209-
$email = $this->customerRepository->getById($this->currentUser->getUserId())->getEmail();
210-
}
211-
212-
$captchaModel->logAttempt($email);
213-
}
137+
//
138+
// /**
139+
// * Method validates captcha, if it's enabled for target form
140+
// *
141+
// * @throws LocalizedException
142+
// */
143+
// private function validateCaptcha() : void
144+
// {
145+
// $captchaTargetFormName = 'product_sendtofriend_form';
146+
// /** @var DefaultModel $captchaModel */
147+
// $captchaModel = $this->captchaHelper->getCaptcha($captchaTargetFormName);
148+
//
149+
// if ($captchaModel->isRequired()) {
150+
// $word = $this->captchaStringResolver->resolve(
151+
// $this->getRequest(),
152+
// $captchaTargetFormName
153+
// );
154+
//
155+
// $isCorrectCaptcha = $captchaModel->isCorrect($word);
156+
//
157+
// if (!$isCorrectCaptcha) {
158+
// $this->logCaptchaAttempt($captchaModel);
159+
// throw new LocalizedException(__('Incorrect CAPTCHA'));
160+
// }
161+
// }
162+
//
163+
// $this->logCaptchaAttempt($captchaModel);
164+
// }
165+
//
166+
// /**
167+
// * Log captcha attempts
168+
// *
169+
// * @param DefaultModel $captchaModel
170+
// */
171+
// private function logCaptchaAttempt(DefaultModel $captchaModel) : void
172+
// {
173+
// $email = '';
174+
//
175+
// if ($this->currentUser->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
176+
// $email = $this->customerRepository->getById($this->currentUser->getUserId())->getEmail();
177+
// }
178+
//
179+
// $captchaModel->logAttempt($email);
180+
// }
214181
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\SendFriend\Model;
10+
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Captcha\Helper\Data;
14+
use Magento\Captcha\Model\DefaultModel;
15+
use Magento\Captcha\Observer\CaptchaStringResolver;
16+
use Magento\Authorization\Model\UserContextInterface;
17+
use Magento\Customer\Api\CustomerRepositoryInterface;
18+
19+
/**
20+
* Class CaptchaValidator. Performs captcha validation
21+
*/
22+
class CaptchaValidator
23+
{
24+
/**
25+
* @var Data
26+
*/
27+
private $captchaHelper;
28+
29+
/**
30+
* @var CaptchaStringResolver
31+
*/
32+
private $captchaStringResolver;
33+
34+
/**
35+
* @var UserContextInterface
36+
*/
37+
private $currentUser;
38+
39+
/**
40+
* @var CustomerRepositoryInterface
41+
*/
42+
private $customerRepository;
43+
44+
/**
45+
* CaptchaValidator constructor.
46+
*
47+
* @param Data $captchaHelper
48+
* @param CaptchaStringResolver $captchaStringResolver
49+
* @param UserContextInterface $currentUser
50+
* @param CustomerRepositoryInterface $customerRepository
51+
*/
52+
public function __construct(
53+
Data $captchaHelper,
54+
CaptchaStringResolver $captchaStringResolver,
55+
UserContextInterface $currentUser,
56+
CustomerRepositoryInterface $customerRepository
57+
) {
58+
$this->captchaHelper = $captchaHelper;
59+
$this->captchaStringResolver = $captchaStringResolver;
60+
$this->currentUser = $currentUser;
61+
$this->customerRepository = $customerRepository;
62+
}
63+
64+
/**
65+
* Entry point for captcha validation
66+
*
67+
* @param RequestInterface $request
68+
* @throws LocalizedException
69+
* @throws \Magento\Framework\Exception\NoSuchEntityException
70+
*/
71+
public function validateSending(RequestInterface $request): void
72+
{
73+
$this->validateCaptcha($request);
74+
}
75+
76+
/**
77+
* Validates captcha and triggers log attempt
78+
*
79+
* @param RequestInterface $request
80+
* @throws LocalizedException
81+
* @throws \Magento\Framework\Exception\NoSuchEntityException
82+
*/
83+
private function validateCaptcha(RequestInterface $request): void
84+
{
85+
$captchaTargetFormName = 'product_sendtofriend_form';
86+
/** @var DefaultModel $captchaModel */
87+
$captchaModel = $this->captchaHelper->getCaptcha($captchaTargetFormName);
88+
89+
if ($captchaModel->isRequired()) {
90+
$word = $this->captchaStringResolver->resolve(
91+
$request,
92+
$captchaTargetFormName
93+
);
94+
95+
$isCorrectCaptcha = $captchaModel->isCorrect($word);
96+
97+
if (!$isCorrectCaptcha) {
98+
$this->logCaptchaAttempt($captchaModel);
99+
throw new LocalizedException(__('Incorrect CAPTCHA'));
100+
}
101+
}
102+
103+
$this->logCaptchaAttempt($captchaModel);
104+
}
105+
106+
/**
107+
* Log captcha attempts
108+
*
109+
* @param DefaultModel $captchaModel
110+
* @throws LocalizedException
111+
* @throws \Magento\Framework\Exception\NoSuchEntityException
112+
*/
113+
private function logCaptchaAttempt(DefaultModel $captchaModel): void
114+
{
115+
$email = '';
116+
117+
if ($this->currentUser->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
118+
$email = $this->customerRepository->getById($this->currentUser->getUserId())->getEmail();
119+
}
120+
121+
$captchaModel->logAttempt($email);
122+
}
123+
}

app/code/Magento/SendFriend/etc/module.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<sequence>
1212
<module name="Magento_Catalog"/>
1313
<module name="Magento_Captcha"/>
14-
<module name="Magento_Authorization"/>
1514
</sequence>
1615
</module>
1716
</config>

0 commit comments

Comments
 (0)