Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 1dcba87

Browse files
authored
Merge pull request #3439 from magento-tsg-csl3/2.2-develop-pr9
[TSG-CSL3] 2.2-develop (pr9)
2 parents 0fa19b7 + 28ac31a commit 1dcba87

File tree

22 files changed

+630
-355
lines changed

22 files changed

+630
-355
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/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Category;
77

88
use Magento\Catalog\Model\Category;
9+
use Magento\Catalog\Model\CategoryFactory;
910
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1011
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
12+
use Magento\Store\Model\Store;
1113

14+
/**
15+
* Perform url updating for children categories.
16+
*/
1217
class Move
1318
{
1419
/**
@@ -21,16 +26,24 @@ class Move
2126
*/
2227
private $childrenCategoriesProvider;
2328

29+
/**
30+
* @var CategoryFactory
31+
*/
32+
private $categoryFactory;
33+
2434
/**
2535
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
2636
* @param ChildrenCategoriesProvider $childrenCategoriesProvider
37+
* @param CategoryFactory $categoryFactory
2738
*/
2839
public function __construct(
2940
CategoryUrlPathGenerator $categoryUrlPathGenerator,
30-
ChildrenCategoriesProvider $childrenCategoriesProvider
41+
ChildrenCategoriesProvider $childrenCategoriesProvider,
42+
CategoryFactory $categoryFactory
3143
) {
3244
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
3345
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
46+
$this->categoryFactory = $categoryFactory;
3447
}
3548

3649
/**
@@ -51,20 +64,57 @@ public function afterChangeParent(
5164
Category $newParent,
5265
$afterCategoryId
5366
) {
54-
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
55-
$category->getResource()->saveAttribute($category, 'url_path');
56-
$this->updateUrlPathForChildren($category);
67+
$categoryStoreId = $category->getStoreId();
68+
foreach ($category->getStoreIds() as $storeId) {
69+
$category->setStoreId($storeId);
70+
if (!$this->isGlobalScope($storeId)) {
71+
$this->updateCategoryUrlKeyForStore($category);
72+
$category->unsUrlPath();
73+
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
74+
$category->getResource()->saveAttribute($category, 'url_path');
75+
$this->updateUrlPathForChildren($category);
76+
}
77+
}
78+
$category->setStoreId($categoryStoreId);
5779

5880
return $result;
5981
}
6082

6183
/**
84+
* Set category url_key according to current category store id.
85+
*
86+
* @param Category $category
87+
* @return void
88+
*/
89+
private function updateCategoryUrlKeyForStore(Category $category)
90+
{
91+
$item = $this->categoryFactory->create();
92+
$item->setStoreId($category->getStoreId());
93+
$item->load($category->getId());
94+
$category->setUrlKey($item->getUrlKey());
95+
}
96+
97+
/**
98+
* Check is global scope.
99+
*
100+
* @param int|null $storeId
101+
* @return bool
102+
*/
103+
private function isGlobalScope($storeId)
104+
{
105+
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
106+
}
107+
108+
/**
109+
* Updates url_path for child categories.
110+
*
62111
* @param Category $category
63112
* @return void
64113
*/
65-
protected function updateUrlPathForChildren($category)
114+
private function updateUrlPathForChildren($category)
66115
{
67116
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
117+
$childCategory->setStoreId($category->getStoreId());
68118
$childCategory->unsUrlPath();
69119
$childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory));
70120
$childCategory->getResource()->saveAttribute($childCategory, 'url_path');

app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Magento\Framework\App\ObjectManager;
1616
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
1717

18+
/**
19+
* Generate list of urls.
20+
*/
1821
class CategoryUrlRewriteGenerator
1922
{
2023
/** Entity type code */
@@ -84,6 +87,8 @@ public function __construct(
8487
}
8588

8689
/**
90+
* Generate list of urls.
91+
*
8792
* @param \Magento\Catalog\Model\Category $category
8893
* @param bool $overrideStoreUrls
8994
* @param int|null $rootCategoryId
@@ -119,6 +124,7 @@ protected function generateForGlobalScope(
119124
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
120125
$categoryId = $category->getId();
121126
foreach ($category->getStoreIds() as $storeId) {
127+
$category->setStoreId($storeId);
122128
if (!$this->isGlobalScope($storeId)
123129
&& $this->isOverrideUrlsForStore($storeId, $categoryId, $overrideStoreUrls)
124130
) {
@@ -131,6 +137,8 @@ protected function generateForGlobalScope(
131137
}
132138

133139
/**
140+
* Checks if urls should be overridden for store.
141+
*
134142
* @param int $storeId
135143
* @param int $categoryId
136144
* @param bool $overrideStoreUrls

0 commit comments

Comments
 (0)