Skip to content

Commit e77653d

Browse files
author
Gabriel Galvao da Gama
committed
Merge branch '2.4.1-develop' of github.com:magento/magento2ce into 2.4.1-develop-MC-36552
2 parents d550f6e + 8e105c7 commit e77653d

File tree

58 files changed

+2055
-277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2055
-277
lines changed

app/code/Magento/Authorization/Model/CompositeUserContext.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ protected function add(UserContextInterface $userContext)
5656
}
5757

5858
/**
59-
* {@inheritdoc}
59+
* @inheritDoc
6060
*/
6161
public function getUserId()
6262
{
6363
return $this->getUserContext() ? $this->getUserContext()->getUserId() : null;
6464
}
6565

6666
/**
67-
* {@inheritdoc}
67+
* @inheritDoc
6868
*/
6969
public function getUserType()
7070
{
@@ -78,7 +78,7 @@ public function getUserType()
7878
*/
7979
protected function getUserContext()
8080
{
81-
if ($this->chosenUserContext === null) {
81+
if (!$this->chosenUserContext) {
8282
/** @var UserContextInterface $userContext */
8383
foreach ($this->userContexts as $userContext) {
8484
if ($userContext->getUserType() && $userContext->getUserId() !== null) {

app/code/Magento/Captcha/Model/DefaultModel.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Magento\Captcha\Model;
99

10+
use Magento\Authorization\Model\UserContextInterface;
1011
use Magento\Captcha\Helper\Data;
12+
use Magento\Framework\App\ObjectManager;
1113
use Magento\Framework\Math\Random;
1214

1315
/**
@@ -93,27 +95,35 @@ class DefaultModel extends \Laminas\Captcha\Image implements \Magento\Captcha\Mo
9395
*/
9496
private $randomMath;
9597

98+
/**
99+
* @var UserContextInterface
100+
*/
101+
private $userContext;
102+
96103
/**
97104
* @param \Magento\Framework\Session\SessionManagerInterface $session
98105
* @param \Magento\Captcha\Helper\Data $captchaData
99106
* @param ResourceModel\LogFactory $resLogFactory
100107
* @param string $formId
101108
* @param Random $randomMath
109+
* @param UserContextInterface|null $userContext
102110
* @throws \Laminas\Captcha\Exception\ExtensionNotLoadedException
103111
*/
104112
public function __construct(
105113
\Magento\Framework\Session\SessionManagerInterface $session,
106114
\Magento\Captcha\Helper\Data $captchaData,
107115
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
108116
$formId,
109-
Random $randomMath = null
117+
Random $randomMath = null,
118+
?UserContextInterface $userContext = null
110119
) {
111120
parent::__construct();
112121
$this->session = $session;
113122
$this->captchaData = $captchaData;
114123
$this->resLogFactory = $resLogFactory;
115124
$this->formId = $formId;
116-
$this->randomMath = $randomMath ?? \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class);
125+
$this->randomMath = $randomMath ?? ObjectManager::getInstance()->get(Random::class);
126+
$this->userContext = $userContext ?? ObjectManager::getInstance()->get(UserContextInterface::class);
117127
}
118128

119129
/**
@@ -152,6 +162,7 @@ public function isRequired($login = null)
152162
$this->formId,
153163
$this->getTargetForms()
154164
)
165+
|| $this->userContext->getUserType() === UserContextInterface::USER_TYPE_INTEGRATION
155166
) {
156167
return false;
157168
}
@@ -241,7 +252,7 @@ private function isOverLimitLoginAttempts($login)
241252
*/
242253
private function isUserAuth()
243254
{
244-
return $this->session->isLoggedIn();
255+
return $this->session->isLoggedIn() || $this->userContext->getUserId();
245256
}
246257

247258
/**
@@ -427,7 +438,7 @@ public function getWordLen()
427438
$to = self::DEFAULT_WORD_LENGTH_TO;
428439
}
429440

430-
return \Magento\Framework\Math\Random::getRandomNumber($from, $to);
441+
return Random::getRandomNumber($from, $to);
431442
}
432443

433444
/**
@@ -549,7 +560,7 @@ private function clearWord()
549560
*/
550561
protected function randomSize()
551562
{
552-
return \Magento\Framework\Math\Random::getRandomNumber(280, 300) / 100;
563+
return Random::getRandomNumber(280, 300) / 100;
553564
}
554565

555566
/**

app/code/Magento/Captcha/Observer/CaptchaStringResolver.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Captcha\Observer;
710

811
use Magento\Framework\App\RequestInterface;
912
use Magento\Framework\App\Request\Http as HttpRequest;
13+
use Magento\Captcha\Helper\Data as CaptchaHelper;
1014

1115
/**
1216
* Extract given captcha word.
@@ -22,12 +26,13 @@ class CaptchaStringResolver
2226
*/
2327
public function resolve(RequestInterface $request, $formId)
2428
{
25-
$captchaParams = $request->getPost(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE);
29+
$value = '';
30+
$captchaParams = $request->getPost(CaptchaHelper::INPUT_NAME_FIELD_VALUE);
2631
if (!empty($captchaParams) && !empty($captchaParams[$formId])) {
2732
$value = $captchaParams[$formId];
28-
} else {
29-
//For Web APIs
30-
$value = $request->getHeader('X-Captcha');
33+
} elseif ($headerValue = $request->getHeader('X-Captcha')) {
34+
//CAPTCHA was provided via header for this XHR/web API request.
35+
$value = $headerValue;
3136
}
3237

3338
return $value;

app/code/Magento/Captcha/Test/Unit/Model/DefaultTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Captcha\Test\Unit\Model;
99

10+
use Magento\Authorization\Model\UserContextInterface;
1011
use Magento\Captcha\Block\Captcha\DefaultCaptcha;
1112
use Magento\Captcha\Helper\Data;
1213
use Magento\Captcha\Model\DefaultModel;
@@ -93,10 +94,15 @@ class DefaultTest extends TestCase
9394
protected $session;
9495

9596
/**
96-
* @var MockObject
97+
* @var MockObject|LogFactory
9798
*/
9899
protected $_resLogFactory;
99100

101+
/**
102+
* @var UserContextInterface|MockObject
103+
*/
104+
private $userContextMock;
105+
100106
/**
101107
* Sets up the fixture, for example, opens a network connection.
102108
* This method is called before a test is executed.
@@ -139,11 +145,18 @@ protected function setUp(): void
139145
$this->_getResourceModelStub()
140146
);
141147

148+
$randomMock = $this->createMock(Random::class);
149+
$randomMock->method('getRandomString')->willReturn('random-string');
150+
151+
$this->userContextMock = $this->getMockForAbstractClass(UserContextInterface::class);
152+
142153
$this->_object = new DefaultModel(
143154
$this->session,
144155
$this->_getHelperStub(),
145156
$this->_resLogFactory,
146-
'user_create'
157+
'user_create',
158+
$randomMock,
159+
$this->userContextMock
147160
);
148161
}
149162

@@ -163,6 +176,19 @@ public function testIsRequired()
163176
$this->assertTrue($this->_object->isRequired());
164177
}
165178

179+
/**
180+
* Validate that CAPTCHA is disabled for integrations.
181+
*
182+
* @return void
183+
*/
184+
public function testIsRequiredForIntegration(): void
185+
{
186+
$this->userContextMock->method('getUserType')->willReturn(UserContextInterface::USER_TYPE_INTEGRATION);
187+
$this->userContextMock->method('getUserId')->willReturn(1);
188+
189+
$this->assertFalse($this->_object->isRequired());
190+
}
191+
166192
/**
167193
* @covers \Magento\Captcha\Model\DefaultModel::isCaseSensitive
168194
*/

app/code/Magento/Captcha/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"magento/module-checkout": "*",
1212
"magento/module-customer": "*",
1313
"magento/module-store": "*",
14+
"magento/module-authorization": "*",
1415
"laminas/laminas-captcha": "^2.7.1",
1516
"laminas/laminas-db": "^2.8.2",
1617
"laminas/laminas-session": "^2.7.3"

app/code/Magento/Captcha/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Always,Always
99
"Reload captcha","Reload captcha"
1010
"Please type the letters and numbers below","Please type the letters and numbers below"
1111
"Attention: Captcha is case sensitive.","Attention: Captcha is case sensitive."
12+
"Please provide CAPTCHA code and try again","Please provide CAPTCHA code and try again"
1213
CAPTCHA,CAPTCHA
1314
"Enable CAPTCHA in Admin","Enable CAPTCHA in Admin"
1415
Font,Font

app/code/Magento/Catalog/Model/ResourceModel/Attribute.php

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel;
77

8-
use Magento\Catalog\Api\Data\ProductInterface;
98
use Magento\Catalog\Model\Attribute\LockValidatorInterface;
9+
use Magento\Catalog\Model\ResourceModel\Attribute\RemoveProductAttributeData;
10+
use Magento\Framework\App\ObjectManager;
1011

1112
/**
1213
* Catalog attribute resource model
13-
*
14-
* @author Magento Core Team <core@magentocommerce.com>
1514
*/
1615
class Attribute extends \Magento\Eav\Model\ResourceModel\Entity\Attribute
1716
{
@@ -28,28 +27,33 @@ class Attribute extends \Magento\Eav\Model\ResourceModel\Entity\Attribute
2827
protected $attrLockValidator;
2928

3029
/**
31-
* @var \Magento\Framework\EntityManager\MetadataPool
30+
* @var RemoveProductAttributeData|null
3231
*/
33-
protected $metadataPool;
32+
private $removeProductAttributeData;
3433

3534
/**
3635
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
3736
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3837
* @param \Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType
3938
* @param \Magento\Eav\Model\Config $eavConfig
4039
* @param LockValidatorInterface $lockValidator
41-
* @param string $connectionName
40+
* @param string|null $connectionName
41+
* @param RemoveProductAttributeData|null $removeProductAttributeData
4242
*/
4343
public function __construct(
4444
\Magento\Framework\Model\ResourceModel\Db\Context $context,
4545
\Magento\Store\Model\StoreManagerInterface $storeManager,
4646
\Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType,
4747
\Magento\Eav\Model\Config $eavConfig,
4848
LockValidatorInterface $lockValidator,
49-
$connectionName = null
49+
$connectionName = null,
50+
RemoveProductAttributeData $removeProductAttributeData = null
5051
) {
5152
$this->attrLockValidator = $lockValidator;
5253
$this->_eavConfig = $eavConfig;
54+
$this->removeProductAttributeData = $removeProductAttributeData ?? ObjectManager::getInstance()
55+
->get(RemoveProductAttributeData::class);
56+
5357
parent::__construct($context, $storeManager, $eavEntityType, $connectionName);
5458
}
5559

@@ -135,41 +139,12 @@ public function deleteEntity(\Magento\Framework\Model\AbstractModel $object)
135139
);
136140
}
137141

138-
$backendTable = $attribute->getBackend()->getTable();
139-
if ($backendTable) {
140-
$linkField = $this->getMetadataPool()
141-
->getMetadata(ProductInterface::class)
142-
->getLinkField();
143-
144-
$backendLinkField = $attribute->getBackend()->getEntityIdField();
145-
146-
$select = $this->getConnection()->select()
147-
->from(['b' => $backendTable])
148-
->join(
149-
['e' => $attribute->getEntity()->getEntityTable()],
150-
"b.$backendLinkField = e.$linkField"
151-
)->where('b.attribute_id = ?', $attribute->getId())
152-
->where('e.attribute_set_id = ?', $result['attribute_set_id']);
153-
154-
$this->getConnection()->query($select->deleteFromSelect('b'));
155-
}
142+
$this->removeProductAttributeData->removeData($object, (int)$result['attribute_set_id']);
156143
}
157144

158145
$condition = ['entity_attribute_id = ?' => $object->getEntityAttributeId()];
159146
$this->getConnection()->delete($this->getTable('eav_entity_attribute'), $condition);
160147

161148
return $this;
162149
}
163-
164-
/**
165-
* @return \Magento\Framework\EntityManager\MetadataPool
166-
*/
167-
private function getMetadataPool()
168-
{
169-
if (null === $this->metadataPool) {
170-
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
171-
->get(\Magento\Framework\EntityManager\MetadataPool::class);
172-
}
173-
return $this->metadataPool;
174-
}
175150
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\ResourceModel\Attribute;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\EntityManager\MetadataPool;
13+
use Magento\Framework\Model\AbstractModel;
14+
15+
/**
16+
* Class for deleting data from attribute additional table by attribute set id.
17+
*/
18+
class RemoveProductAttributeData
19+
{
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $resourceConnection;
24+
25+
/**
26+
* @var MetadataPool
27+
*/
28+
private $metadataPool;
29+
30+
/**
31+
* @param ResourceConnection $resourceConnection
32+
* @param MetadataPool $metadataPool
33+
*/
34+
public function __construct(
35+
ResourceConnection $resourceConnection,
36+
MetadataPool $metadataPool
37+
) {
38+
$this->resourceConnection = $resourceConnection;
39+
$this->metadataPool = $metadataPool;
40+
}
41+
42+
/**
43+
* Deletes data from attribute table by attribute set id.
44+
*
45+
* @param AbstractModel $object
46+
* @param int $attributeSetId
47+
* @return void
48+
*/
49+
public function removeData(AbstractModel $object, int $attributeSetId): void
50+
{
51+
$backendTable = $object->getBackend()->getTable();
52+
if ($backendTable) {
53+
$linkField = $this->metadataPool
54+
->getMetadata(ProductInterface::class)
55+
->getLinkField();
56+
57+
$backendLinkField = $object->getBackend()->getEntityIdField();
58+
59+
$select = $this->resourceConnection->getConnection()->select()
60+
->from(['b' => $backendTable])
61+
->join(
62+
['e' => $object->getEntity()->getEntityTable()],
63+
"b.$backendLinkField = e.$linkField"
64+
)->where('b.attribute_id = ?', $object->getId())
65+
->where('e.attribute_set_id = ?', $attributeSetId);
66+
67+
$this->resourceConnection->getConnection()->query($select->deleteFromSelect('b'));
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)