8
8
9
9
use Magento \Framework \App \Action ;
10
10
use Magento \Framework \App \Config \ScopeConfigInterface ;
11
+ use Magento \Framework \App \ResponseInterface ;
11
12
use Magento \Framework \Exception \NotFoundException ;
12
13
use Magento \Framework \Session \Generic as WishlistSession ;
13
14
use Magento \Store \Model \StoreManagerInterface ;
14
15
use Magento \Framework \Controller \ResultFactory ;
15
16
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 ;
16
25
17
26
/**
18
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -69,6 +78,16 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
69
78
*/
70
79
protected $ storeManager ;
71
80
81
+ /**
82
+ * @var CaptchaHelper
83
+ */
84
+ private $ captchaHelper ;
85
+
86
+ /**
87
+ * @var CaptchaStringResolver
88
+ */
89
+ private $ captchaStringResolver ;
90
+
72
91
/**
73
92
* @param Action\Context $context
74
93
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
@@ -81,6 +100,8 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
81
100
* @param WishlistSession $wishlistSession
82
101
* @param ScopeConfigInterface $scopeConfig
83
102
* @param StoreManagerInterface $storeManager
103
+ * @param CaptchaHelper|null $captchaHelper
104
+ * @param CaptchaStringResolver|null $captchaStringResolver
84
105
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
85
106
*/
86
107
public function __construct (
@@ -94,7 +115,9 @@ public function __construct(
94
115
\Magento \Customer \Helper \View $ customerHelperView ,
95
116
WishlistSession $ wishlistSession ,
96
117
ScopeConfigInterface $ scopeConfig ,
97
- StoreManagerInterface $ storeManager
118
+ StoreManagerInterface $ storeManager ,
119
+ CaptchaHelper $ captchaHelper = null ,
120
+ CaptchaStringResolver $ captchaStringResolver = null
98
121
) {
99
122
$ this ->_formKeyValidator = $ formKeyValidator ;
100
123
$ this ->_customerSession = $ customerSession ;
@@ -106,27 +129,45 @@ public function __construct(
106
129
$ this ->wishlistSession = $ wishlistSession ;
107
130
$ this ->scopeConfig = $ scopeConfig ;
108
131
$ this ->storeManager = $ storeManager ;
132
+ $ this ->captchaHelper = $ captchaHelper ?: ObjectManager::getInstance ()->get (CaptchaHelper::class);
133
+ $ this ->captchaStringResolver = $ captchaStringResolver ?
134
+ : ObjectManager::getInstance ()->get (CaptchaStringResolver::class);
135
+
109
136
parent ::__construct ($ context );
110
137
}
111
138
112
139
/**
113
- * Share wishlist
114
- *
115
- * @return \Magento\Framework\Controller\Result\Redirect
140
+ * @return ResponseInterface|Redirect|ResultInterface
116
141
* @throws NotFoundException
142
+ * @throws LocalizedException
117
143
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
118
144
* @SuppressWarnings(PHPMD.NPathComplexity)
119
145
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
146
+ * @throws \Zend_Validate_Exception
120
147
*/
121
148
public function execute ()
122
149
{
123
150
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
124
151
$ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
152
+ $ captchaFormName = 'share_wishlist_form ' ;
153
+ /** @var CaptchaModel $captchaModel */
154
+ $ captchaModel = $ this ->captchaHelper ->getCaptcha ($ captchaFormName );
155
+
125
156
if (!$ this ->_formKeyValidator ->validate ($ this ->getRequest ())) {
126
157
$ resultRedirect ->setPath ('*/*/ ' );
127
158
return $ resultRedirect ;
128
159
}
129
160
161
+ $ isCorrectCaptcha = $ this ->validateCaptcha ($ captchaModel , $ captchaFormName );
162
+
163
+ $ this ->logCaptchaAttempt ($ captchaModel );
164
+
165
+ if (!$ isCorrectCaptcha ) {
166
+ $ this ->messageManager ->addErrorMessage (__ ('Incorrect CAPTCHA ' ));
167
+ $ resultRedirect ->setPath ('*/*/share ' );
168
+ return $ resultRedirect ;
169
+ }
170
+
130
171
$ wishlist = $ this ->wishlistProvider ->getWishlist ();
131
172
if (!$ wishlist ) {
132
173
throw new NotFoundException (__ ('Page not found. ' ));
@@ -288,4 +329,43 @@ protected function getWishlistItems(ResultLayout $resultLayout)
288
329
->getBlock ('wishlist.email.items ' )
289
330
->toHtml ();
290
331
}
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
+ }
291
371
}
0 commit comments