Skip to content

Commit 0d7ae65

Browse files
author
Roman Leshchenko
committed
Merge branch 'MAGETWO-90468' of github.com:magento-qwerty/magento2ce into MAGETWO-90468
2 parents 0f4569b + a5d1e06 commit 0d7ae65

File tree

6 files changed

+190
-159
lines changed

6 files changed

+190
-159
lines changed

app/code/Magento/Wishlist/Block/Customer/Sharing.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
namespace Magento\Wishlist\Block\Customer;
1313

14+
use Magento\Captcha\Block\Captcha;
15+
1416
/**
1517
* @api
1618
* @since 100.0.2
@@ -60,6 +62,20 @@ public function __construct(
6062
*/
6163
protected function _prepareLayout()
6264
{
65+
if (!$this->getChildBlock('captcha')) {
66+
$this->addChild(
67+
'captcha',
68+
Captcha::class,
69+
[
70+
'cacheable' => false,
71+
'after' => '-',
72+
'form_id' => 'share_wishlist_form',
73+
'image_width' => 230,
74+
'image_height' => 230
75+
]
76+
);
77+
}
78+
6379
$this->pageConfig->getTitle()->set(__('Wish List Sharing'));
6480
}
6581

app/code/Magento/Wishlist/Controller/Index/Send.php

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
use Magento\Framework\View\Result\Layout as ResultLayout;
1717
use Magento\Captcha\Helper\Data as CaptchaHelper;
1818
use Magento\Captcha\Observer\CaptchaStringResolver;
19+
use Magento\Framework\Controller\Result\Redirect;
20+
use Magento\Framework\Controller\ResultInterface;
1921
use Magento\Framework\App\ObjectManager;
22+
use Magento\Captcha\Model\DefaultModel as CaptchaModel;
23+
use Magento\Framework\Exception\LocalizedException;
24+
use Magento\Customer\Model\Customer;
2025

2126
/**
2227
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -74,14 +79,14 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
7479
protected $storeManager;
7580

7681
/**
77-
* @var CaptchaHelper|null
82+
* @var CaptchaHelper
7883
*/
79-
protected $captchaHelper;
84+
private $captchaHelper;
8085

8186
/**
82-
* @var CaptchaStringResolver|null
87+
* @var CaptchaStringResolver
8388
*/
84-
protected $captchaStringResolver;
89+
private $captchaStringResolver;
8590

8691
/**
8792
* @param Action\Context $context
@@ -95,8 +100,8 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
95100
* @param WishlistSession $wishlistSession
96101
* @param ScopeConfigInterface $scopeConfig
97102
* @param StoreManagerInterface $storeManager
98-
* @param CaptchaHelper $captchaHelper|null
99-
* @param CaptchaStringResolver $captchaStringResolver|null
103+
* @param CaptchaHelper|null $captchaHelper
104+
* @param CaptchaStringResolver|null $captchaStringResolver
100105
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
101106
*/
102107
public function __construct(
@@ -127,14 +132,14 @@ public function __construct(
127132
$this->captchaHelper = $captchaHelper ?: ObjectManager::getInstance()->get(CaptchaHelper::class);
128133
$this->captchaStringResolver = $captchaStringResolver ?
129134
: ObjectManager::getInstance()->get(CaptchaStringResolver::class);
135+
130136
parent::__construct($context);
131137
}
132138

133139
/**
134-
* Share wishlist
135-
*
136-
* @return \Magento\Framework\Controller\Result\Redirect
140+
* @return ResponseInterface|Redirect|ResultInterface
137141
* @throws NotFoundException
142+
* @throws LocalizedException
138143
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
139144
* @SuppressWarnings(PHPMD.NPathComplexity)
140145
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -145,25 +150,22 @@ public function execute()
145150
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
146151
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
147152
$captchaFormName = 'share_wishlist_form';
148-
/** @var \Magento\Captcha\Model\DefaultModel $captchaModel */
153+
/** @var CaptchaModel $captchaModel */
149154
$captchaModel = $this->captchaHelper->getCaptcha($captchaFormName);
150155

151156
if (!$this->_formKeyValidator->validate($this->getRequest())) {
152157
$resultRedirect->setPath('*/*/');
153158
return $resultRedirect;
154159
}
155160

156-
if ($captchaModel->isRequired()) {
157-
$word = $this->captchaStringResolver->resolve(
158-
$this->getRequest(),
159-
$captchaFormName
160-
);
161+
$isCorrectCaptcha = $this->validateCaptcha($captchaModel, $captchaFormName);
161162

162-
if (!$captchaModel->isCorrect($word)) {
163-
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
164-
$resultRedirect->setPath('*/*/share');
165-
return $resultRedirect;
166-
}
163+
$this->logCaptchaAttempt($captchaModel);
164+
165+
if (!$isCorrectCaptcha) {
166+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
167+
$resultRedirect->setPath('*/*/share');
168+
return $resultRedirect;
167169
}
168170

169171
$wishlist = $this->wishlistProvider->getWishlist();
@@ -327,4 +329,43 @@ protected function getWishlistItems(ResultLayout $resultLayout)
327329
->getBlock('wishlist.email.items')
328330
->toHtml();
329331
}
332+
333+
/**
334+
* Log customer action attempts
335+
* @param CaptchaModel $captchaModel
336+
* @return void
337+
*/
338+
private function logCaptchaAttempt(CaptchaModel $captchaModel)
339+
{
340+
/** @var Customer $customer */
341+
$customer = $this->_customerSession->getCustomer();
342+
$email = '';
343+
344+
if ($customer->getId()) {
345+
$email = $customer->getEmail();
346+
}
347+
348+
$captchaModel->logAttempt($email);
349+
}
350+
351+
/**
352+
* @param CaptchaModel $captchaModel
353+
* @param string $captchaFormName
354+
* @return bool
355+
*/
356+
private function validateCaptcha(CaptchaModel $captchaModel, string $captchaFormName) : bool
357+
{
358+
if ($captchaModel->isRequired()) {
359+
$word = $this->captchaStringResolver->resolve(
360+
$this->getRequest(),
361+
$captchaFormName
362+
);
363+
364+
if (!$captchaModel->isCorrect($word)) {
365+
return false;
366+
}
367+
}
368+
369+
return true;
370+
}
330371
}

0 commit comments

Comments
 (0)