From 4c2613ed6e9abd8c1535b030b2898d14484ac656 Mon Sep 17 00:00:00 2001 From: Ihor Vansach Date: Mon, 9 Nov 2020 14:02:30 +0200 Subject: [PATCH] ReCaptcha compatability with Magento 2.4.x --- Model/CaptchaResponseResolver.php | 63 ++++++++++ Model/ErrorProcessor.php | 63 ++++++++++ .../Provider/Failure/AjaxResponseFailure.php | 63 ---------- Observer/CommentFormObserver.php | 117 ++++++++++++++++++ Plugin/Model/LayoutSettingsPlugin.php | 45 ------- composer.json | 3 +- etc/adminhtml/system.xml | 16 +-- etc/config.xml | 10 +- etc/frontend/di.xml | 25 +--- etc/frontend/events.xml | 2 +- etc/module.xml | 2 +- view/frontend/layout/blog_post_view.xml | 25 ++-- 12 files changed, 279 insertions(+), 155 deletions(-) create mode 100644 Model/CaptchaResponseResolver.php create mode 100644 Model/ErrorProcessor.php delete mode 100644 Model/Provider/Failure/AjaxResponseFailure.php create mode 100644 Observer/CommentFormObserver.php delete mode 100644 Plugin/Model/LayoutSettingsPlugin.php diff --git a/Model/CaptchaResponseResolver.php b/Model/CaptchaResponseResolver.php new file mode 100644 index 0000000..509adee --- /dev/null +++ b/Model/CaptchaResponseResolver.php @@ -0,0 +1,63 @@ +serializer = $serializer; + } + + /** + * {@inheritdoc} + * + * @param RequestInterface|PlainTextRequestInterface $request + * @return string + * @throws InputException + */ + public function resolve(RequestInterface $request): string + { + $content = $request->getContent(); + + if (empty($content)) { + throw new InputException(__('Can not resolve reCAPTCHA response.')); + } + + try { + //$params = $this->serializer->unserialize($content); + $params = []; + parse_str($content, $params); + } catch (\InvalidArgumentException $e) { + throw new InputException(__('Can not resolve reCAPTCHA response.'), $e); + } + + if (empty($params[self::PARAM_RECAPTCHA])) { + throw new InputException(__('Can not resolve reCAPTCHA response.')); + } + + return $params[self::PARAM_RECAPTCHA]; + } +} diff --git a/Model/ErrorProcessor.php b/Model/ErrorProcessor.php new file mode 100644 index 0000000..623e0c6 --- /dev/null +++ b/Model/ErrorProcessor.php @@ -0,0 +1,63 @@ +actionFlag = $actionFlag; + $this->serializer = $serializer; + } + + /** + * Set "no dispatch" flag and error message to Response + * + * @param ResponseInterface $response + * @param string $message + * @return void + */ + public function processError(ResponseInterface $response, string $message): void + { + $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); + + $jsonPayload = $this->serializer->serialize( + [ + 'success' => false, + 'message' => $message, + ] + ); + $response->representJson($jsonPayload); + } +} diff --git a/Model/Provider/Failure/AjaxResponseFailure.php b/Model/Provider/Failure/AjaxResponseFailure.php deleted file mode 100644 index 0eaf05a..0000000 --- a/Model/Provider/Failure/AjaxResponseFailure.php +++ /dev/null @@ -1,63 +0,0 @@ -actionFlag = $actionFlag; - $this->encoder = $encoder; - $this->config = $config; - } - - /** - * Handle reCaptcha failure - * @param ResponseInterface $response - * @return void - */ - public function execute(ResponseInterface $response = null) - { - $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); - - $jsonPayload = $this->encoder->encode([ - 'success' => false, - 'message' => $this->config->getErrorDescription(), - ]); - $response->representJson($jsonPayload); - } -} diff --git a/Observer/CommentFormObserver.php b/Observer/CommentFormObserver.php new file mode 100644 index 0000000..1facead --- /dev/null +++ b/Observer/CommentFormObserver.php @@ -0,0 +1,117 @@ +captchaResponseResolver = $captchaResponseResolver; + $this->validationConfigResolver = $validationConfigResolver; + $this->captchaValidator = $captchaValidator; + $this->isCaptchaEnabled = $isCaptchaEnabled; + $this->logger = $logger; + $this->errorProcessor = $errorProcessor; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + $key = 'mfblog_comment'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + + $validationConfig = $this->validationConfigResolver->get($key); + + try { + $reCaptchaResponse = $this->captchaResponseResolver->resolve($request); + } catch (InputException $e) { + var_dump($e->getMessage()); + $this->logger->error($e); + $this->errorProcessor->processError( + $response, + $validationConfig->getValidationFailureMessage() + ); + return; + } + + $validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig); + if (false === $validationResult->isValid()) { + $this->errorProcessor->processError( + $response, + $validationConfig->getValidationFailureMessage() + ); + } + } + } +} diff --git a/Plugin/Model/LayoutSettingsPlugin.php b/Plugin/Model/LayoutSettingsPlugin.php deleted file mode 100644 index 43264f1..0000000 --- a/Plugin/Model/LayoutSettingsPlugin.php +++ /dev/null @@ -1,45 +0,0 @@ -scopeConfig = $scopeConfig; - } - - /** - * @param LayoutSettings $subject - * @param $result - * @return mixed - */ - public function afterGetCaptchaSettings(LayoutSettings $subject, $result) - { - $result['enabled']['mfblog_comments'] = (bool) $this->scopeConfig->getValue( - 'msp_securitysuite_recaptcha/frontend/enabled_mfblog_comments', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ); - - return $result; - } -} diff --git a/composer.json b/composer.json index c64c66d..48cb3cd 100644 --- a/composer.json +++ b/composer.json @@ -2,10 +2,11 @@ "name": "magefan/module-blog-comments-recaptcha", "description": "Add recaptcha to Magefan Blog Comments", "require": { + "magento/module-re-captcha-validation-api" : ">=1.0.0", "magefan/module-blog" : ">=2.9.7" }, "type": "magento2-module", - "version": "2.0.6", + "version": "2.1.0", "autoload": { "files": [ "registration.php" ], "psr-4": { diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 4fc9bc3..9932a23 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -8,16 +8,12 @@ -
- - - - Magento\Config\Model\Config\Source\Yesno - - 1 - +
+ + + + Magento\ReCaptchaAdminUi\Model\OptionSource\Type
diff --git a/etc/config.xml b/etc/config.xml index 6b6b81d..801b6ab 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + 0 + + diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 798e74a..22b9701 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -7,22 +7,9 @@ --> - - - - - - - msp_securitysuite_recaptcha/frontend/enabled_mfblog_comments - frontend - - - - - Magefan\BlogCommentsReCaptcha\Model\Provider\IsCheckRequired\Frontend\CommentForm - Magefan\BlogCommentsReCaptcha\Model\Provider\Failure\AjaxResponseFailure - - - \ No newline at end of file + + + Magefan\BlogCommentsReCaptcha\Model\CaptchaResponseResolver + + + diff --git a/etc/frontend/events.xml b/etc/frontend/events.xml index a42989d..af2a0c0 100644 --- a/etc/frontend/events.xml +++ b/etc/frontend/events.xml @@ -8,6 +8,6 @@ - + diff --git a/etc/module.xml b/etc/module.xml index d9bc900..4838c4e 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -6,7 +6,7 @@ */ --> - + diff --git a/view/frontend/layout/blog_post_view.xml b/view/frontend/layout/blog_post_view.xml index e708fe9..6843f20 100644 --- a/view/frontend/layout/blog_post_view.xml +++ b/view/frontend/layout/blog_post_view.xml @@ -9,14 +9,17 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> - + + + mfblog_comment - - MSP_ReCaptcha/js/reCaptcha - mfblog_comments + + Magento_ReCaptchaFrontendUi/js/reCaptcha @@ -24,14 +27,16 @@ - + + mfblog_comment - - MSP_ReCaptcha/js/reCaptcha - mfblog_comments + + Magento_ReCaptchaFrontendUi/js/reCaptcha