Skip to content

Commit c8ad867

Browse files
security-package/issues/154: Add reCAPTCHA support for customer acccount edit.
1 parent 466636c commit c8ad867

File tree

6 files changed

+109
-1
lines changed

6 files changed

+109
-1
lines changed
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\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+
* EditCustomerObserver
19+
*/
20+
class EditCustomerObserver implements ObserverInterface
21+
{
22+
/**
23+
* @var UrlInterface
24+
*/
25+
private $url;
26+
27+
/**
28+
* @var IsCaptchaEnabledInterface
29+
*/
30+
private $isCaptchaEnabled;
31+
32+
/**
33+
* @var RequestHandlerInterface
34+
*/
35+
private $requestHandler;
36+
37+
/**
38+
* @param UrlInterface $url
39+
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
40+
* @param RequestHandlerInterface $requestHandler
41+
*/
42+
public function __construct(
43+
UrlInterface $url,
44+
IsCaptchaEnabledInterface $isCaptchaEnabled,
45+
RequestHandlerInterface $requestHandler
46+
) {
47+
$this->url = $url;
48+
$this->isCaptchaEnabled = $isCaptchaEnabled;
49+
$this->requestHandler = $requestHandler;
50+
}
51+
52+
/**
53+
* @param Observer $observer
54+
* @return void
55+
* @throws \Magento\Framework\Exception\LocalizedException
56+
*/
57+
public function execute(Observer $observer): void
58+
{
59+
$key = 'customer_edit';
60+
if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) {
61+
/** @var Action $controller */
62+
$controller = $observer->getControllerAction();
63+
$request = $controller->getRequest();
64+
$response = $controller->getResponse();
65+
$redirectOnFailureUrl = $this->url->getUrl('*/*/edit', ['_secure' => true]);
66+
67+
$this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl);
68+
}
69+
}
70+
}

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\ReCaptchaApi\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\ReCaptchaApi\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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5-
.login-container, .form-login {
5+
.login-container, .form-login, .form-edit-account {
66
.g-recaptcha {
77
margin-bottom: 10px !important;
88
}

0 commit comments

Comments
 (0)