Skip to content

Commit ed83aad

Browse files
committed
security-package/issues/68: Introduce ReCaptchaReview module
1 parent ba1e04f commit ed83aad

File tree

18 files changed

+200
-40
lines changed

18 files changed

+200
-40
lines changed

ReCaptcha/Model/Config.php

-17
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class Config implements ConfigInterface
3939
public const XML_PATH_ENABLED_FRONTEND_FORGOT = 'recaptcha/frontend/enabled_forgot';
4040
public const XML_PATH_ENABLED_FRONTEND_CONTACT = 'recaptcha/frontend/enabled_contact';
4141
public const XML_PATH_ENABLED_FRONTEND_CREATE = 'recaptcha/frontend/enabled_create';
42-
public const XML_PATH_ENABLED_FRONTEND_REVIEW = 'recaptcha/frontend/enabled_review';
4342
public const XML_PATH_ENABLED_FRONTEND_NEWSLETTER = 'recaptcha/frontend/enabled_newsletter';
4443

4544
/**
@@ -179,22 +178,6 @@ public function isEnabledFrontendCreateUser(): bool
179178
);
180179
}
181180

182-
/**
183-
* Return true if enabled on frontend review
184-
* @return bool
185-
*/
186-
public function isEnabledFrontendReview(): bool
187-
{
188-
if (!$this->isEnabledFrontend()) {
189-
return false;
190-
}
191-
192-
return (bool) $this->scopeConfig->getValue(
193-
static::XML_PATH_ENABLED_FRONTEND_REVIEW,
194-
ScopeInterface::SCOPE_WEBSITE
195-
);
196-
}
197-
198181
/**
199182
* Return true if enabled on frontend newsletter
200183
* @return bool

ReCaptcha/Model/LayoutSettings.php

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public function getCaptchaSettings(): array
5555
'create' => $this->config->isEnabledFrontendCreateUser(),
5656
'forgot' => $this->config->isEnabledFrontendForgot(),
5757
'contact' => $this->config->isEnabledFrontendContact(),
58-
'review' => $this->config->isEnabledFrontendReview(),
5958
'newsletter' => $this->config->isEnabledFrontendNewsletter(),
6059
]
6160
];

ReCaptcha/etc/adminhtml/system.xml

-8
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,6 @@
164164
<field id="enabled">1</field>
165165
</depends>
166166
</field>
167-
<field id="enabled_review" translate="label" type="select" sortOrder="240" showInDefault="1"
168-
showInWebsite="1" showInStore="0" canRestore="1">
169-
<label>Use in Review</label>
170-
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
171-
<depends>
172-
<field id="enabled">1</field>
173-
</depends>
174-
</field>
175167
<field id="enabled_newsletter" translate="label" type="select" sortOrder="250" showInDefault="1"
176168
showInWebsite="1" showInStore="0" canRestore="1">
177169
<label>Use invisible ReCaptcha in newsletter</label>

ReCaptcha/etc/config.xml

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<enabled_forgot>1</enabled_forgot>
2929
<enabled_contact>1</enabled_contact>
3030
<enabled_create>1</enabled_create>
31-
<enabled_review>1</enabled_review>
3231
<enabled_newsletter>1</enabled_newsletter>
3332
<min_score>0.6</min_score>
3433
</frontend>

ReCaptcha/etc/frontend/events.xml

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
<event name="controller_action_predispatch_contact_index_post">
2424
<observer name="recaptcha_on_contact_form" instance="Magento\ReCaptcha\Observer\Frontend\ContactFormObserver" />
2525
</event>
26-
<event name="controller_action_predispatch_review_product_post">
27-
<observer name="recaptcha_on_review_form" instance="Magento\ReCaptcha\Observer\Frontend\ReviewFormObserver" />
28-
</event>
2926
<event name="controller_action_predispatch_newsletter_subscriber_new">
3027
<observer name="recaptcha_on_newsletter" instance="Magento\ReCaptcha\Observer\Frontend\NewsletterObserver" />
3128
</event>

ReCaptcha/view/frontend/web/css/source/_module.less

-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,3 @@
1717
width: 1px;
1818
height: 1px;
1919
}
20-
21-
.review-form {
22-
.field-recaptcha {
23-
margin-bottom: 10px;
24-
}
25-
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\ReCaptchaReview\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\ReCaptcha\Model\ConfigEnabledInterface;
12+
use Magento\ReCaptcha\Model\ConfigInterface;
13+
use Magento\Store\Model\ScopeInterface;
14+
15+
/**
16+
* Read configuration from store config
17+
*/
18+
class ConfigEnabled implements ConfigEnabledInterface
19+
{
20+
public const XML_PATH_ENABLED_FRONTEND_REVIEW = 'recaptcha/frontend/enabled_review';
21+
22+
/**
23+
* @var ConfigInterface
24+
*/
25+
private $reCaptchaConfig;
26+
27+
/**
28+
* @var ScopeConfigInterface
29+
*/
30+
private $scopeConfig;
31+
32+
/**
33+
* @param ConfigInterface $reCaptchaConfig
34+
* @param ScopeConfigInterface $scopeConfig
35+
*/
36+
public function __construct(
37+
ConfigInterface $reCaptchaConfig,
38+
ScopeConfigInterface $scopeConfig
39+
) {
40+
$this->reCaptchaConfig = $reCaptchaConfig;
41+
$this->scopeConfig = $scopeConfig;
42+
}
43+
44+
/**
45+
* Return true if enabled on frontend captcha for review
46+
* @return bool
47+
*/
48+
public function isEnabled(): bool
49+
{
50+
if (!$this->reCaptchaConfig->isEnabledFrontend()) {
51+
return false;
52+
}
53+
54+
return (bool)$this->scopeConfig->getValue(
55+
static::XML_PATH_ENABLED_FRONTEND_REVIEW,
56+
ScopeInterface::SCOPE_WEBSITE
57+
);
58+
}
59+
}

ReCaptcha/Observer/Frontend/ReviewFormObserver.php renamed to ReCaptchaReview/Observer/ReviewFormObserver.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Exception\LocalizedException;
1616
use Magento\ReCaptcha\Model\CaptchaRequestHandlerInterface;
1717
use Magento\ReCaptcha\Model\Config;
18+
use Magento\ReCaptcha\Model\ConfigEnabledInterface;
1819

1920
/**
2021
* ReviewFormObserver
@@ -27,7 +28,7 @@ class ReviewFormObserver implements ObserverInterface
2728
private $redirect;
2829

2930
/**
30-
* @var Config
31+
* @var ConfigEnabledInterface
3132
*/
3233
private $config;
3334

@@ -38,12 +39,12 @@ class ReviewFormObserver implements ObserverInterface
3839

3940
/**
4041
* @param RedirectInterface $redirect
41-
* @param Config $config
42+
* @param ConfigEnabledInterface $config
4243
* @param CaptchaRequestHandlerInterface $captchaRequestHandler
4344
*/
4445
public function __construct(
4546
RedirectInterface $redirect,
46-
Config $config,
47+
ConfigEnabledInterface $config,
4748
CaptchaRequestHandlerInterface $captchaRequestHandler
4849
) {
4950
$this->redirect = $redirect;
@@ -58,7 +59,7 @@ public function __construct(
5859
*/
5960
public function execute(Observer $observer): void
6061
{
61-
if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendReview()) {
62+
if ($this->config->isEnabled()) {
6263
/** @var Action $controller */
6364
$controller = $observer->getControllerAction();
6465
$request = $controller->getRequest();

ReCaptchaReview/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Please refer to: https://github.com/magento/security-package

ReCaptchaReview/composer.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "magento/module-re-captcha-review",
3+
"version": "1.0.0",
4+
"description": "Google reCaptcha integration for Magento2",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0||~7.3.0",
7+
"magento/framework": "102.0.*",
8+
"magento/module-re-captcha": "*"
9+
},
10+
"authors": [
11+
{
12+
"name": "Riccardo Tempesta",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"type": "magento2-module",
17+
"license": "OSL-3.0",
18+
"autoload": {
19+
"files": [
20+
"registration.php"
21+
],
22+
"psr-4": {
23+
"Magento\\ReCaptchaReview\\": ""
24+
}
25+
}
26+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
10+
<system>
11+
<section id="recaptcha">
12+
<group id="frontend">
13+
<field id="enabled_review" translate="label" type="select" sortOrder="240" showInDefault="1"
14+
showInWebsite="1" showInStore="0" canRestore="1">
15+
<label>Use in Review</label>
16+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
17+
<depends>
18+
<field id="enabled">1</field>
19+
</depends>
20+
</field>
21+
</group>
22+
</section>
23+
</system>
24+
</config>

ReCaptchaReview/etc/config.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
10+
<default>
11+
<recaptcha>
12+
<frontend>
13+
<enabled_review>1</enabled_review>
14+
</frontend>
15+
</recaptcha>
16+
</default>
17+
</config>

ReCaptchaReview/etc/di.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<type name="Magento\ReCaptchaReview\Observer\ReviewFormObserver">
11+
<arguments>
12+
<argument name="config" xsi:type="object">Magento\ReCaptchaReview\Model\ConfigEnabled</argument>
13+
</arguments>
14+
</type>
15+
16+
<type name="Magento\ReCaptcha\Model\LayoutSettings">
17+
<arguments>
18+
<argument name="configEnabledProviders" xsi:type="array">
19+
<item name="review" xsi:type="object">Magento\ReCaptchaReview\Model\ConfigEnabled</item>
20+
</argument>
21+
</arguments>
22+
</type>
23+
</config>
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
10+
<event name="controller_action_predispatch_review_product_post">
11+
<observer name="recaptcha_on_review_form" instance="Magento\ReCaptcha\Observer\Frontend\ReviewFormObserver" />
12+
</event>
13+
</config>

ReCaptchaReview/etc/module.xml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
10+
<module name="Magento_ReCaptchaReview"/>
11+
</config>

ReCaptchaReview/registration.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
\Magento\Framework\Component\ComponentRegistrar::register(
9+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
10+
'Magento_ReCaptchaReview',
11+
__DIR__
12+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
.review-form {
6+
.field-recaptcha {
7+
margin-bottom: 10px;
8+
}
9+
}

0 commit comments

Comments
 (0)