Skip to content

Commit d8eacad

Browse files
author
roman
committed
MAGETWO-90467: Added posibility to use captcha on share wishlist page
1 parent b55989d commit d8eacad

File tree

11 files changed

+241
-515
lines changed

11 files changed

+241
-515
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: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@
88

99
use Magento\Framework\App\Action;
1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\ResponseInterface;
1112
use Magento\Framework\Exception\NotFoundException;
1213
use Magento\Framework\Session\Generic as WishlistSession;
1314
use Magento\Store\Model\StoreManagerInterface;
1415
use Magento\Framework\Controller\ResultFactory;
1516
use Magento\Framework\View\Result\Layout as ResultLayout;
17+
use Magento\Captcha\Helper\Data as CaptchaHelper;
18+
use Magento\Captcha\Observer\CaptchaStringResolver;
19+
use Magento\Framework\Controller\Result\Redirect;
20+
use Magento\Framework\Controller\ResultInterface;
21+
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;
1625

1726
/**
1827
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -69,6 +78,16 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
6978
*/
7079
protected $storeManager;
7180

81+
/**
82+
* @var CaptchaHelper
83+
*/
84+
private $captchaHelper;
85+
86+
/**
87+
* @var CaptchaStringResolver
88+
*/
89+
private $captchaStringResolver;
90+
7291
/**
7392
* @param Action\Context $context
7493
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
@@ -81,6 +100,8 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
81100
* @param WishlistSession $wishlistSession
82101
* @param ScopeConfigInterface $scopeConfig
83102
* @param StoreManagerInterface $storeManager
103+
* @param CaptchaHelper|null $captchaHelper
104+
* @param CaptchaStringResolver|null $captchaStringResolver
84105
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
85106
*/
86107
public function __construct(
@@ -94,7 +115,9 @@ public function __construct(
94115
\Magento\Customer\Helper\View $customerHelperView,
95116
WishlistSession $wishlistSession,
96117
ScopeConfigInterface $scopeConfig,
97-
StoreManagerInterface $storeManager
118+
StoreManagerInterface $storeManager,
119+
?CaptchaHelper $captchaHelper = null,
120+
?CaptchaStringResolver $captchaStringResolver = null
98121
) {
99122
$this->_formKeyValidator = $formKeyValidator;
100123
$this->_customerSession = $customerSession;
@@ -106,6 +129,10 @@ public function __construct(
106129
$this->wishlistSession = $wishlistSession;
107130
$this->scopeConfig = $scopeConfig;
108131
$this->storeManager = $storeManager;
132+
$this->captchaHelper = $captchaHelper ?: ObjectManager::getInstance()->get(CaptchaHelper::class);
133+
$this->captchaStringResolver = $captchaStringResolver ?
134+
: ObjectManager::getInstance()->get(CaptchaStringResolver::class);
135+
109136
parent::__construct($context);
110137
}
111138

@@ -114,6 +141,7 @@ public function __construct(
114141
*
115142
* @return \Magento\Framework\Controller\Result\Redirect
116143
* @throws NotFoundException
144+
* @throws \Zend_Validate_Exception
117145
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
118146
* @SuppressWarnings(PHPMD.NPathComplexity)
119147
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -122,11 +150,25 @@ public function execute()
122150
{
123151
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
124152
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
153+
$captchaForName = 'share_wishlist_form';
154+
/** @var CaptchaModel $captchaModel */
155+
$captchaModel = $this->captchaHelper->getCaptcha($captchaForName);
156+
125157
if (!$this->_formKeyValidator->validate($this->getRequest())) {
126158
$resultRedirect->setPath('*/*/');
127159
return $resultRedirect;
128160
}
129161

162+
$isCorrectCaptcha = $this->validateCaptcha($captchaModel, $captchaForName);
163+
164+
$this->logCaptchaAttempt($captchaModel);
165+
166+
if (!$isCorrectCaptcha) {
167+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
168+
$resultRedirect->setPath('*/*/share');
169+
return $resultRedirect;
170+
}
171+
130172
$wishlist = $this->wishlistProvider->getWishlist();
131173
if (!$wishlist) {
132174
throw new NotFoundException(__('Page not found.'));
@@ -288,4 +330,43 @@ protected function getWishlistItems(ResultLayout $resultLayout)
288330
->getBlock('wishlist.email.items')
289331
->toHtml();
290332
}
333+
334+
/**
335+
* Log customer action attempts
336+
* @param CaptchaModel $captchaModel
337+
* @return void
338+
*/
339+
private function logCaptchaAttempt(CaptchaModel $captchaModel)
340+
{
341+
/** @var Customer $customer */
342+
$customer = $this->_customerSession->getCustomer();
343+
$email = '';
344+
345+
if ($customer->getId()) {
346+
$email = $customer->getEmail();
347+
}
348+
349+
$captchaModel->logAttempt($email);
350+
}
351+
352+
/**
353+
* @param CaptchaModel $captchaModel
354+
* @param string $captchaFormName
355+
* @return bool
356+
*/
357+
private function validateCaptcha(CaptchaModel $captchaModel, string $captchaFormName) : bool
358+
{
359+
if ($captchaModel->isRequired()) {
360+
$word = $this->captchaStringResolver->resolve(
361+
$this->getRequest(),
362+
$captchaFormName
363+
);
364+
365+
if (!$captchaModel->isCorrect($word)) {
366+
return false;
367+
}
368+
}
369+
370+
return true;
371+
}
291372
}

0 commit comments

Comments
 (0)