Skip to content

Commit 1cc2dfc

Browse files
#222: Add validation for error message.
1 parent 89c7e9b commit 1cc2dfc

File tree

21 files changed

+474
-49
lines changed

21 files changed

+474
-49
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\ReCaptchaAdminUi\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\ReCaptchaUi\Model\ErrorMessageConfigInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
class ErrorMessageConfig implements ErrorMessageConfigInterface
18+
{
19+
private const XML_PATH_VALIDATION = 'recaptcha_backend/failure_messages/validation_failure_message';
20+
private const XML_PATH_TECHNICAL = 'recaptcha_backend/failure_messages/technical_failure_message';
21+
22+
/**
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* @param ScopeConfigInterface $scopeConfig
29+
*/
30+
public function __construct(
31+
ScopeConfigInterface $scopeConfig
32+
) {
33+
$this->scopeConfig = $scopeConfig;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function getTechnicalFailureMessage(): string
40+
{
41+
return $this->scopeConfig->getValue(
42+
self::XML_PATH_TECHNICAL,
43+
ScopeInterface::SCOPE_STORE
44+
);
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function getValidationFailureMessage(): string
51+
{
52+
return $this->scopeConfig->getValue(
53+
self::XML_PATH_VALIDATION,
54+
ScopeInterface::SCOPE_STORE
55+
);
56+
}
57+
}

ReCaptchaAdminUi/etc/adminhtml/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
1010
<preference for="Magento\ReCaptchaUi\Model\CaptchaTypeResolverInterface"
1111
type="Magento\ReCaptchaAdminUi\Model\CaptchaTypeResolver"/>
12+
<preference for="Magento\ReCaptchaUi\Model\ErrorMessageConfigInterface"
13+
type="Magento\ReCaptchaAdminUi\Model\ErrorMessageConfig"/>
1214
</config>

ReCaptchaAdminUi/etc/adminhtml/system.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
<frontend_model>Magento\ReCaptchaAdminUi\Block\Adminhtml\System\Config\Form\Field\Notice</frontend_model>
2828
</field>
2929
</group>
30+
31+
<group id="failure_messages" translate="label" type="text" sortOrder="950" showInDefault="1" showInWebsite="1"
32+
showInStore="1">
33+
<label>reCAPTCHA Failure Messages</label>
34+
35+
<field id="validation_failure_message" translate="label" type="textarea" sortOrder="10" showInDefault="1"
36+
showInWebsite="1" showInStore="1" canRestore="1">
37+
<label>reCAPTCHA Validation Failure Message</label>
38+
</field>
39+
40+
<field id="technical_failure_message" translate="label" type="textarea" sortOrder="20" showInDefault="1"
41+
showInWebsite="1" showInStore="1" canRestore="1">
42+
<label>reCAPTCHA Technical Failure Message</label>
43+
</field>
44+
</group>
3045
</section>
3146

3247
<section id="recaptcha_frontend" translate="label" type="text" sortOrder="700" showInDefault="1"

ReCaptchaAdminUi/etc/config.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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_backend>
12+
<failure_messages>
13+
<validation_failure_message>reCAPTCHA verification failed.</validation_failure_message>
14+
<technical_failure_message>Something went wrong with reCAPTCHA, please contact Store owner.
15+
</technical_failure_message>
16+
</failure_messages>
17+
</recaptcha_backend>
18+
</default>
19+
</config>

ReCaptchaCustomer/Model/AjaxLogin/ErrorProcessor.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
use Magento\Framework\App\Action\Action;
1111
use Magento\Framework\App\ActionFlag;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\App\ResponseInterface;
1314
use Magento\Framework\Serialize\SerializerInterface;
15+
use Magento\ReCaptchaUi\Model\ErrorMessageConfigInterface;
16+
use Magento\ReCaptchaValidationApi\Model\ValidationErrorMessagesProvider;
17+
use Psr\Log\LoggerInterface;
1418

1519
/**
1620
* Process error during ajax login
@@ -29,27 +33,72 @@ class ErrorProcessor
2933
*/
3034
private $serializer;
3135

36+
/**
37+
* @var LoggerInterface
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @var ErrorMessageConfigInterface
43+
*/
44+
private $errorMessageConfig;
45+
46+
/**
47+
* @var ValidationErrorMessagesProvider
48+
*/
49+
private $validationErrorMessagesProvider;
50+
3251
/**
3352
* @param ActionFlag $actionFlag
3453
* @param SerializerInterface $serializer
54+
* @param ErrorMessageConfigInterface|null $errorMessageConfig
55+
* @param ValidationErrorMessagesProvider|null $validationErrorMessagesProvider
3556
*/
3657
public function __construct(
3758
ActionFlag $actionFlag,
38-
SerializerInterface $serializer
59+
SerializerInterface $serializer,
60+
?LoggerInterface $logger = null,
61+
?ErrorMessageConfigInterface $errorMessageConfig = null,
62+
?ValidationErrorMessagesProvider $validationErrorMessagesProvider = null
3963
) {
4064
$this->actionFlag = $actionFlag;
4165
$this->serializer = $serializer;
66+
$this->logger = $logger
67+
?? ObjectManager::getInstance()->get(LoggerInterface::class);
68+
$this->errorMessageConfig = $errorMessageConfig
69+
?? ObjectManager::getInstance()->get(ErrorMessageConfigInterface::class);
70+
$this->validationErrorMessagesProvider = $validationErrorMessagesProvider
71+
?? ObjectManager::getInstance()->get(ValidationErrorMessagesProvider::class);
4272
}
4373

4474
/**
4575
* Set "no dispatch" flag and error message to Response
4676
*
4777
* @param ResponseInterface $response
48-
* @param string $message
78+
* @param array $errorMessages
79+
* @param string $sourceKey
4980
* @return void
5081
*/
51-
public function processError(ResponseInterface $response, string $message): void
82+
public function processError(ResponseInterface $response, array $errorMessages, string $sourceKey): void
5283
{
84+
$validationErrorText = $this->errorMessageConfig->getValidationFailureMessage();
85+
$technicalErrorText = $this->errorMessageConfig->getTechnicalFailureMessage();
86+
87+
$message = $errorMessages ? $validationErrorText : $technicalErrorText;
88+
89+
foreach ($errorMessages as $errorMessageCode => $errorMessageText) {
90+
if (!$this->isValidationError($errorMessageCode)) {
91+
$message = $technicalErrorText;
92+
$this->logger->error(
93+
__(
94+
'reCAPTCHA \'%1\' form error: %2',
95+
$sourceKey,
96+
$errorMessageText
97+
)
98+
);
99+
}
100+
}
101+
53102
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
54103

55104
$jsonPayload = $this->serializer->serialize([
@@ -58,4 +107,15 @@ public function processError(ResponseInterface $response, string $message): void
58107
]);
59108
$response->representJson($jsonPayload);
60109
}
110+
111+
/**
112+
* Check if error code present in validation errors list.
113+
*
114+
* @param string $errorMessageCode
115+
* @return bool
116+
*/
117+
private function isValidationError(string $errorMessageCode): bool
118+
{
119+
return $errorMessageCode !== $this->validationErrorMessagesProvider->getErrorMessage($errorMessageCode);
120+
}
61121
}

ReCaptchaCustomer/Observer/AjaxLoginObserver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public function execute(Observer $observer): void
100100
$this->logger->error($e);
101101
$this->errorProcessor->processError(
102102
$response,
103-
$validationConfig->getValidationFailureMessage()
103+
[],
104+
$key
104105
);
105106
return;
106107
}
@@ -109,7 +110,8 @@ public function execute(Observer $observer): void
109110
if (false === $validationResult->isValid()) {
110111
$this->errorProcessor->processError(
111112
$response,
112-
$validationConfig->getValidationFailureMessage()
113+
$validationResult->getErrors(),
114+
$key
113115
);
114116
}
115117
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\ReCaptchaFrontendUi\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\ReCaptchaUi\Model\ErrorMessageConfigInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
class ErrorMessageConfig implements ErrorMessageConfigInterface
18+
{
19+
private const XML_PATH_VALIDATION = 'recaptcha_frontend/failure_messages/validation_failure_message';
20+
private const XML_PATH_TECHNICAL = 'recaptcha_frontend/failure_messages/technical_failure_message';
21+
22+
/**
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* @param ScopeConfigInterface $scopeConfig
29+
*/
30+
public function __construct(
31+
ScopeConfigInterface $scopeConfig
32+
) {
33+
$this->scopeConfig = $scopeConfig;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function getTechnicalFailureMessage(): string
40+
{
41+
return $this->scopeConfig->getValue(
42+
self::XML_PATH_TECHNICAL,
43+
ScopeInterface::SCOPE_STORE
44+
);
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function getValidationFailureMessage(): string
51+
{
52+
return $this->scopeConfig->getValue(
53+
self::XML_PATH_VALIDATION,
54+
ScopeInterface::SCOPE_STORE
55+
);
56+
}
57+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_frontend">
12+
<group id="failure_messages" translate="label" type="text" sortOrder="950" showInDefault="1" showInWebsite="1"
13+
showInStore="1">
14+
<label>reCAPTCHA Failure Messages</label>
15+
16+
<field id="validation_failure_message" translate="label" type="textarea" sortOrder="10" showInDefault="1"
17+
showInWebsite="1" showInStore="1" canRestore="1">
18+
<label>reCAPTCHA Validation Failure Message</label>
19+
</field>
20+
21+
<field id="technical_failure_message" translate="label" type="textarea" sortOrder="20" showInDefault="1"
22+
showInWebsite="1" showInStore="1" canRestore="1">
23+
<label>reCAPTCHA Technical Failure Message</label>
24+
</field>
25+
</group>
26+
</section>
27+
</system>
28+
</config>

ReCaptchaFrontendUi/etc/config.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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_frontend>
12+
<failure_messages>
13+
<validation_failure_message>reCAPTCHA verification failed.</validation_failure_message>
14+
<technical_failure_message>Something went wrong with reCAPTCHA, please contact Store owner.
15+
</technical_failure_message>
16+
</failure_messages>
17+
</recaptcha_frontend>
18+
</default>
19+
</config>

ReCaptchaFrontendUi/etc/frontend/di.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
1010
<preference for="Magento\ReCaptchaUi\Model\CaptchaTypeResolverInterface"
1111
type="Magento\ReCaptchaFrontendUi\Model\CaptchaTypeResolver"/>
12-
12+
<preference for="Magento\ReCaptchaUi\Model\ErrorMessageConfigInterface"
13+
type="Magento\ReCaptchaFrontendUi\Model\ErrorMessageConfig"/>
1314
<type name="Magento\Framework\View\Asset\Minification">
1415
<plugin name="exclude-recaptcha-from-minification"
1516
type="Magento\ReCaptchaFrontendUi\Plugin\ExcludeFromMinification"/>

0 commit comments

Comments
 (0)