Skip to content

Commit 083d3b6

Browse files
authored
Merge pull request #155 from /issues/154
security-package/issues/154: Add reCAPTCHA support for customer account edit.
2 parents 89c7e9b + 3e798f9 commit 083d3b6

File tree

6 files changed

+115
-1
lines changed

6 files changed

+115
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\ReCaptchaCustomer\Observer;
9+
10+
use Magento\Framework\App\Action\Action;
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\Framework\UrlInterface;
14+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
15+
use Magento\ReCaptchaUi\Model\RequestHandlerInterface;
16+
17+
/**
18+
* NewsletterObserver
19+
*
20+
* Process the response during customer account editing
21+
*/
22+
class EditCustomerObserver implements ObserverInterface
23+
{
24+
/**
25+
* @var UrlInterface
26+
*/
27+
private $url;
28+
29+
/**
30+
* @var IsCaptchaEnabledInterface
31+
*/
32+
private $isCaptchaEnabled;
33+
34+
/**
35+
* @var RequestHandlerInterface
36+
*/
37+
private $requestHandler;
38+
39+
/**
40+
* @param UrlInterface $url
41+
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
42+
* @param RequestHandlerInterface $requestHandler
43+
*/
44+
public function __construct(
45+
UrlInterface $url,
46+
IsCaptchaEnabledInterface $isCaptchaEnabled,
47+
RequestHandlerInterface $requestHandler
48+
) {
49+
$this->url = $url;
50+
$this->isCaptchaEnabled = $isCaptchaEnabled;
51+
$this->requestHandler = $requestHandler;
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*
57+
* @param Observer $observer
58+
* @return void
59+
* @throws \Magento\Framework\Exception\LocalizedException
60+
*/
61+
public function execute(Observer $observer): void
62+
{
63+
$key = 'customer_edit';
64+
if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) {
65+
/** @var Action $controller */
66+
$controller = $observer->getControllerAction();
67+
$request = $controller->getRequest();
68+
$response = $controller->getResponse();
69+
$redirectOnFailureUrl = $this->url->getUrl('*/*/edit', ['_secure' => true]);
70+
71+
$this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl);
72+
}
73+
}
74+
}

ReCaptchaCustomer/etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<label>Enable for Create New Customer Account</label>
2626
<source_model>Magento\ReCaptchaAdminUi\Model\OptionSource\Type</source_model>
2727
</field>
28+
<field id="customer_edit" translate="label" type="select" sortOrder="135" showInDefault="1"
29+
showInWebsite="1" showInStore="0" canRestore="1">
30+
<label>Enable for Edit Customer Account</label>
31+
<source_model>Magento\ReCaptchaAdminUi\Model\OptionSource\Type</source_model>
32+
</field>
2833
</group>
2934
</section>
3035
</system>

ReCaptchaCustomer/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<customer_login/>
1414
<customer_forgot_password/>
1515
<customer_create/>
16+
<customer_edit/>
1617
</type_for>
1718
</recaptcha_frontend>
1819
</default>

ReCaptchaCustomer/etc/frontend/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<event name="controller_action_predispatch_customer_account_createpost">
1717
<observer name="recaptcha_on_create_user" instance="Magento\ReCaptchaCustomer\Observer\CreateCustomerObserver"/>
1818
</event>
19+
<event name="controller_action_predispatch_customer_account_editpost">
20+
<observer name="recaptcha_on_edit_customer" instance="Magento\ReCaptchaCustomer\Observer\EditCustomerObserver"/>
21+
</event>
1922
<event name="controller_action_predispatch_customer_account_forgotpasswordpost">
2023
<observer name="recaptcha_on_forgot_password"
2124
instance="Magento\ReCaptchaCustomer\Observer\ForgotPasswordObserver"/>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<body>
10+
<referenceContainer name="form.additional.info">
11+
<block class="Magento\ReCaptchaUi\Block\ReCaptcha"
12+
name="recaptcha"
13+
after="-"
14+
template="Magento_ReCaptchaFrontendUi::recaptcha.phtml"
15+
ifconfig="recaptcha_frontend/type_for/customer_edit">
16+
<arguments>
17+
<argument name="recaptcha_for" xsi:type="string">customer_edit</argument>
18+
<argument name="jsLayout" xsi:type="array">
19+
<item name="components" xsi:type="array">
20+
<item name="recaptcha" xsi:type="array">
21+
<item name="component" xsi:type="string">Magento_ReCaptchaFrontendUi/js/reCaptcha</item>
22+
</item>
23+
</item>
24+
</argument>
25+
</arguments>
26+
</block>
27+
</referenceContainer>
28+
</body>
29+
</page>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5-
.login-container, .form-login {
5+
.login-container,
6+
.form-login,
7+
.form-edit-account {
68
.g-recaptcha {
79
margin-bottom: 10px !important;
810
}

0 commit comments

Comments
 (0)