Skip to content

Commit b2cdcea

Browse files
author
Mariana Lashch
committed
Merge branch 'MAGETWO-94052' into forwardport-1411
2 parents 31097f4 + c62229f commit b2cdcea

File tree

12 files changed

+266
-219
lines changed

12 files changed

+266
-219
lines changed

app/code/Magento/Captcha/Model/Customer/Plugin/AjaxLogin.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Captcha\Model\Customer\Plugin;
89

910
use Magento\Captcha\Helper\Data as CaptchaHelper;
10-
use Magento\Framework\Session\SessionManagerInterface;
11+
use Magento\Customer\Controller\Ajax\Login;
12+
use Magento\Framework\Controller\Result\Json;
1113
use Magento\Framework\Controller\Result\JsonFactory;
14+
use Magento\Framework\Session\SessionManagerInterface;
1215

16+
/**
17+
* The plugin for ajax login controller.
18+
*/
1319
class AjaxLogin
1420
{
1521
/**
@@ -61,14 +67,14 @@ public function __construct(
6167
}
6268

6369
/**
64-
* @param \Magento\Customer\Controller\Ajax\Login $subject
70+
* Validates captcha during request execution.
71+
*
72+
* @param Login $subject
6573
* @param \Closure $proceed
6674
* @return $this
67-
* @SuppressWarnings(PHPMD.NPathComplexity)
68-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6975
*/
7076
public function aroundExecute(
71-
\Magento\Customer\Controller\Ajax\Login $subject,
77+
Login $subject,
7278
\Closure $proceed
7379
) {
7480
$captchaFormIdField = 'captcha_form_id';
@@ -93,26 +99,31 @@ public function aroundExecute(
9399
foreach ($this->formIds as $formId) {
94100
if ($formId === $loginFormId) {
95101
$captchaModel = $this->helper->getCaptcha($formId);
102+
96103
if ($captchaModel->isRequired($username)) {
97-
$captchaModel->logAttempt($username);
98104
if (!$captchaModel->isCorrect($captchaString)) {
99105
$this->sessionManager->setUsername($username);
100-
return $this->returnJsonError(__('Incorrect CAPTCHA'));
106+
$captchaModel->logAttempt($username);
107+
return $this->returnJsonError(__('Incorrect CAPTCHA'), true);
101108
}
102109
}
110+
111+
$captchaModel->logAttempt($username);
103112
}
104113
}
105114
return $proceed();
106115
}
107116

108117
/**
118+
* Gets Json response.
109119
*
110120
* @param \Magento\Framework\Phrase $phrase
111-
* @return \Magento\Framework\Controller\Result\Json
121+
* @param bool $isCaptchaRequired
122+
* @return Json
112123
*/
113-
private function returnJsonError(\Magento\Framework\Phrase $phrase): \Magento\Framework\Controller\Result\Json
124+
private function returnJsonError(\Magento\Framework\Phrase $phrase, bool $isCaptchaRequired = false): Json
114125
{
115126
$resultJson = $this->resultJsonFactory->create();
116-
return $resultJson->setData(['errors' => true, 'message' => $phrase]);
127+
return $resultJson->setData(['errors' => true, 'message' => $phrase, 'captcha' => $isCaptchaRequired]);
117128
}
118129
}

app/code/Magento/Captcha/Test/Unit/Model/Customer/Plugin/AjaxLoginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function testAroundExecuteIncorrectCaptcha()
158158
$this->resultJsonMock
159159
->expects($this->once())
160160
->method('setData')
161-
->with(['errors' => true, 'message' => __('Incorrect CAPTCHA')])
161+
->with(['errors' => true, 'message' => __('Incorrect CAPTCHA'), 'captcha' => true])
162162
->will($this->returnSelf());
163163

164164
$closure = function () {

app/code/Magento/Captcha/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</arguments>
2828
</type>
2929
<type name="Magento\Customer\Controller\Ajax\Login">
30-
<plugin name="configurable_product" type="Magento\Captcha\Model\Customer\Plugin\AjaxLogin" sortOrder="50" />
30+
<plugin name="captcha_validation" type="Magento\Captcha\Model\Customer\Plugin\AjaxLogin" sortOrder="50" />
3131
</type>
3232
<type name="Magento\Captcha\Model\Customer\Plugin\AjaxLogin">
3333
<arguments>

app/code/Magento/Captcha/view/frontend/web/js/model/captcha.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ define([
1717
imageSource: ko.observable(captchaData.imageSrc),
1818
visibility: ko.observable(false),
1919
captchaValue: ko.observable(null),
20-
isRequired: captchaData.isRequired,
20+
isRequired: ko.observable(captchaData.isRequired),
2121
isCaseSensitive: captchaData.isCaseSensitive,
2222
imageHeight: captchaData.imageHeight,
2323
refreshUrl: captchaData.refreshUrl,
@@ -41,7 +41,7 @@ define([
4141
* @return {Boolean}
4242
*/
4343
getIsVisible: function () {
44-
return this.visibility;
44+
return this.visibility();
4545
},
4646

4747
/**
@@ -55,14 +55,14 @@ define([
5555
* @return {Boolean}
5656
*/
5757
getIsRequired: function () {
58-
return this.isRequired;
58+
return this.isRequired();
5959
},
6060

6161
/**
6262
* @param {Boolean} flag
6363
*/
6464
setIsRequired: function (flag) {
65-
this.isRequired = flag;
65+
this.isRequired(flag);
6666
},
6767

6868
/**

app/code/Magento/Captcha/view/frontend/web/js/view/checkout/defaultCaptcha.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ define([
8989
return this.currentCaptcha !== null ? this.currentCaptcha.getIsRequired() : false;
9090
},
9191

92+
/**
93+
* @param {Boolean} flag
94+
*/
95+
setIsRequired: function (flag) {
96+
this.currentCaptcha.setIsRequired(flag);
97+
},
98+
9299
/**
93100
* @return {Boolean}
94101
*/

app/code/Magento/Captcha/view/frontend/web/js/view/checkout/loginCaptcha.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,44 @@
44
*/
55

66
define([
7-
'Magento_Captcha/js/view/checkout/defaultCaptcha',
8-
'Magento_Captcha/js/model/captchaList',
9-
'Magento_Customer/js/action/login'
10-
],
11-
function (defaultCaptcha, captchaList, loginAction) {
12-
'use strict';
13-
14-
return defaultCaptcha.extend({
15-
/** @inheritdoc */
16-
initialize: function () {
17-
var self = this,
18-
currentCaptcha;
19-
20-
this._super();
21-
currentCaptcha = captchaList.getCaptchaByFormId(this.formId);
22-
23-
if (currentCaptcha != null) {
24-
currentCaptcha.setIsVisible(true);
25-
this.setCurrentCaptcha(currentCaptcha);
26-
27-
loginAction.registerLoginCallback(function (loginData) {
28-
if (loginData['captcha_form_id'] &&
29-
loginData['captcha_form_id'] == self.formId //eslint-disable-line eqeqeq
30-
) {
7+
'underscore',
8+
'Magento_Captcha/js/view/checkout/defaultCaptcha',
9+
'Magento_Captcha/js/model/captchaList',
10+
'Magento_Customer/js/action/login'
11+
],
12+
function (_, defaultCaptcha, captchaList, loginAction) {
13+
'use strict';
14+
15+
return defaultCaptcha.extend({
16+
/** @inheritdoc */
17+
initialize: function () {
18+
var self = this,
19+
currentCaptcha;
20+
21+
this._super();
22+
currentCaptcha = captchaList.getCaptchaByFormId(this.formId);
23+
24+
if (currentCaptcha != null) {
25+
currentCaptcha.setIsVisible(true);
26+
this.setCurrentCaptcha(currentCaptcha);
27+
28+
loginAction.registerLoginCallback(function (loginData, response) {
29+
if (!loginData['captcha_form_id'] || loginData['captcha_form_id'] !== self.formId) {
30+
return;
31+
}
32+
33+
if (_.isUndefined(response) || !response.errors) {
34+
return;
35+
}
36+
37+
// check if captcha should be required after login attempt
38+
if (!self.isRequired() && response.captcha && self.isRequired() !== response.captcha) {
39+
self.setIsRequired(response.captcha);
40+
}
41+
3142
self.refresh();
32-
}
33-
});
43+
});
44+
}
3445
}
35-
}
46+
});
3647
});
37-
});

app/code/Magento/Captcha/view/frontend/web/template/checkout/captcha.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
* See COPYING.txt for license details.
55
*/
66
-->
7+
<!-- ko if: (getIsVisible())-->
8+
<input name="captcha_form_id" type="hidden" data-bind="value: formId, attr: {'data-scope': dataScope}" />
9+
<!-- /ko -->
710
<!-- ko if: (isRequired() && getIsVisible())-->
811
<div class="field captcha required" data-bind="blockLoader: getIsLoading()">
912
<label data-bind="attr: {for: 'captcha_' + formId}" class="label"><span data-bind="i18n: 'Please type the letters and numbers below'"></span></label>
1013
<div class="control captcha">
11-
<input name="captcha_string" type="text" class="input-text required-entry" data-bind="value: captchaValue(), attr: {id: 'captcha_' + formId, 'data-scope': dataScope}" autocomplete="off"/>
12-
<input name="captcha_form_id" type="hidden" data-bind="value: formId, attr: {'data-scope': dataScope}" />
14+
<input name="captcha_string" type="text" class="input-text required-entry" data-bind="value: captchaValue(), attr: {'data-scope': dataScope}" autocomplete="off"/>
1315
<div class="nested">
1416
<div class="field captcha no-label">
1517
<div class="control captcha-image">

app/code/Magento/Customer/Controller/Ajax/Login.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public function __construct(
107107

108108
/**
109109
* Get account redirect.
110-
* For release backward compatibility.
111110
*
112111
* @deprecated 100.0.10
113112
* @return AccountRedirect
@@ -133,6 +132,8 @@ public function setAccountRedirect($value)
133132
}
134133

135134
/**
135+
* Initializes config dependency.
136+
*
136137
* @deprecated 100.0.10
137138
* @return ScopeConfigInterface
138139
*/
@@ -145,6 +146,8 @@ protected function getScopeConfig()
145146
}
146147

147148
/**
149+
* Sets config dependency.
150+
*
148151
* @deprecated 100.0.10
149152
* @param ScopeConfigInterface $value
150153
* @return void
@@ -199,25 +202,17 @@ public function execute()
199202
$response['redirectUrl'] = $this->_redirect->success($redirectRoute);
200203
$this->getAccountRedirect()->clearRedirectCookie();
201204
}
202-
} catch (EmailNotConfirmedException $e) {
203-
$response = [
204-
'errors' => true,
205-
'message' => $e->getMessage()
206-
];
207-
} catch (InvalidEmailOrPasswordException $e) {
208-
$response = [
209-
'errors' => true,
210-
'message' => $e->getMessage()
211-
];
212-
} catch (LocalizedException $e) {
205+
} catch (LocalizedException | InvalidEmailOrPasswordException | EmailNotConfirmedException $e) {
213206
$response = [
214207
'errors' => true,
215-
'message' => $e->getMessage()
208+
'message' => $e->getMessage(),
209+
'captcha' => $this->customerSession->getData('user_login_show_captcha')
216210
];
217211
} catch (\Exception $e) {
218212
$response = [
219213
'errors' => true,
220-
'message' => __('Invalid login or password.')
214+
'message' => __('Invalid login or password.'),
215+
'captcha' => $this->customerSession->getData('user_login_show_captcha')
221216
];
222217
}
223218
/** @var \Magento\Framework\Controller\Result\Json $resultJson */

0 commit comments

Comments
 (0)