Skip to content

32987 Don't load the captcha files on the storefront if it is disabled in the configurations #33200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Captcha\Block\Captcha\Checkout;

use Magento\Captcha\Helper\Data;
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;

class DisableCaptchaProcessor implements LayoutProcessorInterface
{
/**
* @var Data
*/
private $helper;

/**
* @param Data $helper
*/
public function __construct(
Data $helper
) {
$this->helper = $helper;
}

/**
* {@inheritdoc}
*/
public function process($jsLayout)
{
if (!(bool)$this->helper->getConfig('enable')) {
$jsLayout['components']['checkout']['children']['authentication']['children']['captcha']['config']['componentDisabled'] = true;
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']
['customer-email']['children']['additional-login-form-fields']['children']['captcha']['config']['componentDisabled'] = true;
}
return $jsLayout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Captcha\Block\Captcha\Customer;

use Magento\Captcha\Helper\Data;
use Magento\Customer\Block\Account\LayoutProcessorInterface;

class DisableCaptchaProcessor implements LayoutProcessorInterface
{
/**
* @var Data
*/
private $helper;

/**
* @param Data $helper
*/
public function __construct(
Data $helper
) {
$this->helper = $helper;
}

/**
* {@inheritdoc}
*/
public function process($jsLayout)
{
if (!(bool)$this->helper->getConfig('enable')) {
$jsLayout['components']['authenticationPopup']['children']['captcha']['config']['componentDisabled'] = true;
}
return $jsLayout;
}
}
14 changes: 14 additions & 0 deletions app/code/Magento/Captcha/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
</argument>
</arguments>
</type>
<type name="Magento\Customer\Block\Account\AuthenticationPopup">
<arguments>
<argument name="layoutProcessors" xsi:type="array">
<item name="captcha" xsi:type="object">Magento\Captcha\Block\Captcha\Customer\DisableCaptchaProcessor</item>
</argument>
</arguments>
</type>
<type name="Magento\Checkout\Block\Onepage">
<arguments>
<argument name="layoutProcessors" xsi:type="array">
<item name="captcha" xsi:type="object">Magento\Captcha\Block\Captcha\Checkout\DisableCaptchaProcessor</item>
</argument>
</arguments>
</type>
<type name="Magento\Sales\Api\OrderManagementInterface">
<plugin name="reset_payment_attempts_after_order_is_placed_plugin" type="Magento\Captcha\Plugin\ResetPaymentAttemptsAfterOrderIsPlacedPlugin"/>
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false" ifconfig="customer/captcha/enable">
<action method="setFormId">
<argument name="formId" xsi:type="string">contact_us</argument>
</action>
Expand All @@ -21,7 +21,8 @@
</block>
</referenceContainer>
<referenceBlock name="head.components">
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"
ifconfig="customer/captcha/enable"/>
</referenceBlock>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false" ifconfig="customer/captcha/enable">
<action method="setFormId">
<argument name="formId" xsi:type="string">user_create</argument>
</action>
Expand All @@ -21,7 +21,8 @@
</block>
</referenceContainer>
<referenceBlock name="head.components">
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"
ifconfig="customer/captcha/enable"/>
</referenceBlock>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false" ifconfig="customer/captcha/enable">
<action method="setFormId">
<argument name="formId" xsi:type="string">user_edit</argument>
</action>
Expand All @@ -21,7 +21,8 @@
</block>
</referenceContainer>
<referenceBlock name="head.components">
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"
ifconfig="customer/captcha/enable"/>
</referenceBlock>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false" ifconfig="customer/captcha/enable">
<action method="setFormId">
<argument name="formId" xsi:type="string">user_forgotpassword</argument>
</action>
Expand All @@ -21,7 +21,8 @@
</block>
</referenceContainer>
<referenceBlock name="head.components">
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"
ifconfig="customer/captcha/enable"/>
</referenceBlock>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false" ifconfig="customer/captcha/enable">
<action method="setFormId">
<argument name="formId" xsi:type="string">user_login</argument>
</action>
Expand All @@ -21,7 +21,8 @@
</block>
</referenceContainer>
<referenceBlock name="head.components">
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"
ifconfig="customer/captcha/enable"/>
</referenceBlock>
</body>
</page>
43 changes: 43 additions & 0 deletions app/code/Magento/Checkout/Plugin/DisableCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Checkout\Plugin;

use Magento\Captcha\Block\Captcha\Checkout\DisableCaptchaProcessor;
use Magento\Captcha\Helper\Data;

class DisableCaptcha
{
/**
* @var Data
*/
private $helper;

/**
* @param Data $helper
*/
public function __construct(
Data $helper
) {
$this->helper = $helper;
}

/**
* @param DisableCaptchaProcessor $subject
* @param array $jsLayout
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterProcess(DisableCaptchaProcessor $subject, array $jsLayout): array
{
if (!(bool)$this->helper->getConfig('enable')) {
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']
['place-order-captcha']['config']['componentDisabled'] = true;
}
return $jsLayout;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Checkout/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<type name="Magento\Quote\Model\Quote">
<plugin name="clear_addresses_after_product_delete" type="Magento\Checkout\Plugin\Model\Quote\ResetQuoteAddresses"/>
</type>
<type name="Magento\Captcha\Block\Captcha\Checkout\DisableCaptchaProcessor">
<plugin name="disable_place_order_captcha" type="Magento\Checkout\Plugin\DisableCaptcha"/>
</type>
<type name="Magento\Captcha\CustomerData\Captcha">
<arguments>
<argument name="formIds" xsi:type="array">
Expand Down
11 changes: 11 additions & 0 deletions app/code/Magento/Customer/Block/Account/AuthenticationPopup.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,27 @@ class AuthenticationPopup extends \Magento\Framework\View\Element\Template
*/
private $serializer;

/**
* @var array|LayoutProcessorInterface[]
*/
protected $layoutProcessors;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array|LayoutProcessorInterface[] $layoutProcessors
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $layoutProcessors = [],
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
parent::__construct($context, $data);
$this->jsLayout = isset($data['jsLayout']) && is_array($data['jsLayout']) ? $data['jsLayout'] : [];
$this->layoutProcessors = $layoutProcessors;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}
Expand All @@ -46,6 +54,9 @@ public function __construct(
*/
public function getJsLayout()
{
foreach ($this->layoutProcessors as $processor) {
$this->jsLayout = $processor->process($this->jsLayout);
}
return $this->serializer->serialize($this->jsLayout);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Account;

/**
* Layout processor interface.
*
* Can be used to provide a custom logic for customer authentication popup JS layout preparation.
*
* @see \Magento\Customer\Block\Account\AuthenticationPopup
*
* @api
* @since 100.0.2
*/
interface LayoutProcessorInterface
{
/**
* Process js Layout of block
*
* @param array $jsLayout
* @return array
*/
public function process($jsLayout);
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Customer\Test\Unit\Block\Account;

use Magento\Customer\Block\Account\AuthenticationPopup;
use Magento\Customer\Block\Account\LayoutProcessorInterface;
use Magento\Customer\Model\Form;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Escaper;
Expand Down Expand Up @@ -41,6 +42,11 @@ class AuthenticationPopupTest extends TestCase
/** @var Json|MockObject */
private $serializerMock;

/**
* @var MockObject
*/
protected $layoutProcessorMock;

protected function setUp(): void
{
$this->contextMock = $this->getMockBuilder(Context::class)
Expand Down Expand Up @@ -85,8 +91,13 @@ function ($string) {
$this->serializerMock = $this->getMockBuilder(Json::class)
->getMock();

$this->layoutProcessorMock = $this->createMock(
LayoutProcessorInterface::class
);

$this->model = new AuthenticationPopup(
$this->contextMock,
[$this->layoutProcessorMock],
[],
$this->serializerMock
);
Expand Down Expand Up @@ -238,4 +249,13 @@ public function testGetSerializedConfig($isAutocomplete, $baseUrl, $registerUrl,

$this->assertEquals(json_encode($result), $this->model->getSerializedConfig());
}
public function testGetJsLayout()
{
$processedLayout = ['layout' => ['processed' => true]];
$jsonLayout = '{"layout":{"processed":true}}';
$this->layoutProcessorMock->expects($this->once())->method('process')->with([])->willReturn($processedLayout);
$this->serializerMock->expects($this->once())->method('serialize')->willReturn($jsonLayout);

$this->assertEquals($jsonLayout, $this->model->getJsLayout());
}
}
Loading