16
16
use Magento \Framework \View \Result \Layout as ResultLayout ;
17
17
use Magento \Captcha \Helper \Data as CaptchaHelper ;
18
18
use Magento \Captcha \Observer \CaptchaStringResolver ;
19
+ use Magento \Framework \Controller \Result \Redirect ;
20
+ use Magento \Framework \Controller \ResultInterface ;
19
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 ;
20
25
21
26
/**
22
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -74,14 +79,14 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
74
79
protected $ storeManager ;
75
80
76
81
/**
77
- * @var CaptchaHelper|null
82
+ * @var CaptchaHelper
78
83
*/
79
- protected $ captchaHelper ;
84
+ private $ captchaHelper ;
80
85
81
86
/**
82
- * @var CaptchaStringResolver|null
87
+ * @var CaptchaStringResolver
83
88
*/
84
- protected $ captchaStringResolver ;
89
+ private $ captchaStringResolver ;
85
90
86
91
/**
87
92
* @param Action\Context $context
@@ -95,8 +100,8 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
95
100
* @param WishlistSession $wishlistSession
96
101
* @param ScopeConfigInterface $scopeConfig
97
102
* @param StoreManagerInterface $storeManager
98
- * @param CaptchaHelper $captchaHelper|null
99
- * @param CaptchaStringResolver $captchaStringResolver|null
103
+ * @param CaptchaHelper|null $captchaHelper
104
+ * @param CaptchaStringResolver|null $captchaStringResolver
100
105
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
101
106
*/
102
107
public function __construct (
@@ -127,14 +132,14 @@ public function __construct(
127
132
$ this ->captchaHelper = $ captchaHelper ?: ObjectManager::getInstance ()->get (CaptchaHelper::class);
128
133
$ this ->captchaStringResolver = $ captchaStringResolver ?
129
134
: ObjectManager::getInstance ()->get (CaptchaStringResolver::class);
135
+
130
136
parent ::__construct ($ context );
131
137
}
132
138
133
139
/**
134
- * Share wishlist
135
- *
136
- * @return \Magento\Framework\Controller\Result\Redirect
140
+ * @return ResponseInterface|Redirect|ResultInterface
137
141
* @throws NotFoundException
142
+ * @throws LocalizedException
138
143
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
139
144
* @SuppressWarnings(PHPMD.NPathComplexity)
140
145
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -145,25 +150,22 @@ public function execute()
145
150
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
146
151
$ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
147
152
$ captchaFormName = 'share_wishlist_form ' ;
148
- /** @var \Magento\Captcha\Model\DefaultModel $captchaModel */
153
+ /** @var CaptchaModel $captchaModel */
149
154
$ captchaModel = $ this ->captchaHelper ->getCaptcha ($ captchaFormName );
150
155
151
156
if (!$ this ->_formKeyValidator ->validate ($ this ->getRequest ())) {
152
157
$ resultRedirect ->setPath ('*/*/ ' );
153
158
return $ resultRedirect ;
154
159
}
155
160
156
- if ($ captchaModel ->isRequired ()) {
157
- $ word = $ this ->captchaStringResolver ->resolve (
158
- $ this ->getRequest (),
159
- $ captchaFormName
160
- );
161
+ $ isCorrectCaptcha = $ this ->validateCaptcha ($ captchaModel , $ captchaFormName );
161
162
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 ;
167
169
}
168
170
169
171
$ wishlist = $ this ->wishlistProvider ->getWishlist ();
@@ -327,4 +329,43 @@ protected function getWishlistItems(ResultLayout $resultLayout)
327
329
->getBlock ('wishlist.email.items ' )
328
330
->toHtml ();
329
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
+ }
330
371
}
0 commit comments