Skip to content

Commit f286e17

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-93780' into 2.2-develop-pr11
2 parents 0be69c9 + e67d688 commit f286e17

File tree

12 files changed

+266
-215
lines changed

12 files changed

+266
-215
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
namespace Magento\Captcha\Model\Customer\Plugin;
88

99
use Magento\Captcha\Helper\Data as CaptchaHelper;
10-
use Magento\Framework\Session\SessionManagerInterface;
10+
use Magento\Customer\Controller\Ajax\Login;
11+
use Magento\Framework\Controller\Result\Json;
1112
use Magento\Framework\Controller\Result\JsonFactory;
13+
use Magento\Framework\Session\SessionManagerInterface;
1214

15+
/**
16+
* The plugin for ajax login controller.
17+
*/
1318
class AjaxLogin
1419
{
1520
/**
@@ -61,14 +66,14 @@ public function __construct(
6166
}
6267

6368
/**
64-
* @param \Magento\Customer\Controller\Ajax\Login $subject
69+
* Validates captcha during request execution.
70+
*
71+
* @param Login $subject
6572
* @param \Closure $proceed
6673
* @return $this
67-
* @SuppressWarnings(PHPMD.NPathComplexity)
68-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6974
*/
7075
public function aroundExecute(
71-
\Magento\Customer\Controller\Ajax\Login $subject,
76+
Login $subject,
7277
\Closure $proceed
7378
) {
7479
$captchaFormIdField = 'captcha_form_id';
@@ -93,26 +98,31 @@ public function aroundExecute(
9398
foreach ($this->formIds as $formId) {
9499
if ($formId === $loginFormId) {
95100
$captchaModel = $this->helper->getCaptcha($formId);
101+
96102
if ($captchaModel->isRequired($username)) {
97-
$captchaModel->logAttempt($username);
98103
if (!$captchaModel->isCorrect($captchaString)) {
99104
$this->sessionManager->setUsername($username);
100-
return $this->returnJsonError(__('Incorrect CAPTCHA'));
105+
$captchaModel->logAttempt($username);
106+
return $this->returnJsonError(__('Incorrect CAPTCHA'), true);
101107
}
102108
}
109+
110+
$captchaModel->logAttempt($username);
103111
}
104112
}
105113
return $proceed();
106114
}
107115

108116
/**
117+
* Gets Json response.
109118
*
110119
* @param \Magento\Framework\Phrase $phrase
111-
* @return \Magento\Framework\Controller\Result\Json
120+
* @param bool $isCaptchaRequired
121+
* @return Json
112122
*/
113-
private function returnJsonError(\Magento\Framework\Phrase $phrase): \Magento\Framework\Controller\Result\Json
123+
private function returnJsonError(\Magento\Framework\Phrase $phrase, bool $isCaptchaRequired = false): Json
114124
{
115125
$resultJson = $this->resultJsonFactory->create();
116-
return $resultJson->setData(['errors' => true, 'message' => $phrase]);
126+
return $resultJson->setData(['errors' => true, 'message' => $phrase, 'captcha' => $isCaptchaRequired]);
117127
}
118128
}

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
@@ -149,7 +149,7 @@ public function testAroundExecuteIncorrectCaptcha()
149149
$this->resultJsonMock
150150
->expects($this->once())
151151
->method('setData')
152-
->with(['errors' => true, 'message' => __('Incorrect CAPTCHA')])
152+
->with(['errors' => true, 'message' => __('Incorrect CAPTCHA'), 'captcha' => true])
153153
->will($this->returnSelf());
154154

155155
$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: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
namespace Magento\Customer\Controller\Ajax;
88

99
use Magento\Customer\Api\AccountManagementInterface;
10-
use Magento\Framework\Exception\EmailNotConfirmedException;
11-
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
1210
use Magento\Framework\App\ObjectManager;
1311
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
1412
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -70,6 +68,11 @@ class Login extends \Magento\Framework\App\Action\Action
7068
*/
7169
private $cookieMetadataFactory;
7270

71+
/**
72+
* @var \Magento\Customer\Model\Session
73+
*/
74+
private $customerSession;
75+
7376
/**
7477
* Initialize Login controller
7578
*
@@ -108,7 +111,6 @@ public function __construct(
108111

109112
/**
110113
* Get account redirect.
111-
* For release backward compatibility.
112114
*
113115
* @deprecated 100.0.10
114116
* @return AccountRedirect
@@ -134,6 +136,8 @@ public function setAccountRedirect($value)
134136
}
135137

136138
/**
139+
* Initializes config dependency.
140+
*
137141
* @deprecated 100.0.10
138142
* @return ScopeConfigInterface
139143
*/
@@ -146,6 +150,8 @@ protected function getScopeConfig()
146150
}
147151

148152
/**
153+
* Sets config dependency.
154+
*
149155
* @deprecated 100.0.10
150156
* @param ScopeConfigInterface $value
151157
* @return void
@@ -200,25 +206,17 @@ public function execute()
200206
$response['redirectUrl'] = $this->_redirect->success($redirectRoute);
201207
$this->getAccountRedirect()->clearRedirectCookie();
202208
}
203-
} catch (EmailNotConfirmedException $e) {
204-
$response = [
205-
'errors' => true,
206-
'message' => $e->getMessage()
207-
];
208-
} catch (InvalidEmailOrPasswordException $e) {
209-
$response = [
210-
'errors' => true,
211-
'message' => $e->getMessage()
212-
];
213209
} catch (LocalizedException $e) {
214210
$response = [
215211
'errors' => true,
216-
'message' => $e->getMessage()
212+
'message' => $e->getMessage(),
213+
'captcha' => $this->customerSession->getData('user_login_show_captcha')
217214
];
218215
} catch (\Exception $e) {
219216
$response = [
220217
'errors' => true,
221-
'message' => __('Invalid login or password.')
218+
'message' => __('Invalid login or password.'),
219+
'captcha' => $this->customerSession->getData('user_login_show_captcha')
222220
];
223221
}
224222
/** @var \Magento\Framework\Controller\Result\Json $resultJson */

0 commit comments

Comments
 (0)