Skip to content

Commit

Permalink
Merge pull request #48 from mageplaza/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
haitv282 authored Apr 29, 2020
2 parents 65d186e + 650373e commit 4aac419
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 18 deletions.
91 changes: 91 additions & 0 deletions Console/Adminhtml/Command/Disable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_GoogleRecaptcha
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command;

use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
use Mageplaza\GoogleRecaptcha\Helper\Data as HelperData;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class Disable
* @package Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command
*/
class Disable extends Command
{
/**
* @var HelperData
*/
protected $helperData;

/**
* @var ConfigWriter
*/
protected $_configWriter;

/**
* Disable constructor.
*
* @param HelperData $helperData
* @param ConfigWriter $configWriter
* @param null $name
*/
public function __construct(
HelperData $helperData,
ConfigWriter $configWriter,
$name = null
) {
$this->helperData = $helperData;
$this->_configWriter = $configWriter;

parent::__construct($name);
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('mpgooglerecaptcha:backend:disable')
->setDescription(__('Disable backend captcha'));

parent::configure();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|void|null
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$this->helperData->isCaptchaBackend()) {
$output->writeln(__('The captcha is disable for your admin website.'));
} else {
$path = 'googlerecaptcha/backend/enabled';
$this->_configWriter->save($path, '0');
$output->writeln(__('The captcha backend has been successfully disabled. Please run the flush cache command again'));
}
}
}
91 changes: 91 additions & 0 deletions Console/Adminhtml/Command/Enable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_GoogleRecaptcha
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command;

use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
use Mageplaza\GoogleRecaptcha\Helper\Data as HelperData;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class Enable
* @package Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command
*/
class Enable extends Command
{
/**
* @var HelperData
*/
protected $helperData;

/**
* @var HelperData
*/
protected $_configWriter;

/**
* Enable constructor.
*
* @param HelperData $helperData
* @param ConfigWriter $configWriter
* @param null $name
*/
public function __construct(
HelperData $helperData,
ConfigWriter $configWriter,
$name = null
) {
$this->helperData = $helperData;
$this->_configWriter = $configWriter;

parent::__construct($name);
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('mpgooglerecaptcha:backend:enable')
->setDescription(__('Enable backend captcha'));

parent::configure();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|void|null
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this->helperData->isCaptchaBackend()) {
$output->writeln(__('The captcha is enabled for your admin website.'));
} else {
$path = 'googlerecaptcha/backend/enabled';
$this->_configWriter->save($path, '1');
$output->writeln(__('The captcha backend has been successfully enabled. Please run the flush cache command again'));
}
}
}
33 changes: 28 additions & 5 deletions Observer/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Event\Observer;
Expand Down Expand Up @@ -67,6 +68,11 @@ class Captcha implements ObserverInterface
*/
protected $redirect;

/**
* @var RequestInterface
*/
protected $requestInterface;

/**
* Captcha constructor.
*
Expand All @@ -76,37 +82,53 @@ class Captcha implements ObserverInterface
* @param ActionFlag $actionFlag
* @param ResponseInterface $responseInterface
* @param RedirectInterface $redirect
* @param RequestInterface $requestInterface
*/
public function __construct(
HelperData $helperData,
Http $request,
ManagerInterface $messageManager,
ActionFlag $actionFlag,
ResponseInterface $responseInterface,
RedirectInterface $redirect
RedirectInterface $redirect,
RequestInterface $requestInterface
) {
$this->_helperData = $helperData;
$this->_request = $request;
$this->messageManager = $messageManager;
$this->_actionFlag = $actionFlag;
$this->_responseInterface = $responseInterface;
$this->redirect = $redirect;
$this->requestInterface = $requestInterface;
}

/**
* @param Observer $observer
*
* @return array|void
*/
public function execute(Observer $observer)
{
if ($this->_helperData->isEnabled() && $this->_helperData->isCaptchaFrontend()) {
$checkResponse = 1;
$captcha = false;
if ($this->_request->getFullActionName() === 'wishlist_index_add') {
return;
}
foreach ($this->_helperData->getFormPostPaths() as $item) {
if ($item !== '' && strpos($this->_request->getRequestUri(), trim($item, ' ')) !== false) {
$checkResponse = 0;
if ($this->_request->getParam('g-recaptcha-response') !== null) {
$captcha = $this->_request->getParam('g-recaptcha-response');
// case ajax login
if ($item === 'customer/ajax/login' && $captcha === null && $this->_request->isAjax()) {
$formData = HelperData::jsonDecode($this->requestInterface->getContent());
if (array_key_exists('g-recaptcha-response', $formData)) {
$captcha = $formData['g-recaptcha-response'];
} else {
return $this->redirectUrlError(__('Missing required parameters recaptcha!'));
}
}
if ($captcha !== null) {
$type = $this->_helperData->getRecaptchaType();
$response = $this->_helperData->verifyResponse($type);
if (isset($response['success']) && !$response['success']) {
Expand All @@ -117,7 +139,9 @@ public function execute(Observer $observer)
}
}
}
if ($checkResponse === 1 && $this->_request->getParam('g-recaptcha-response') !== null) {

if ($checkResponse === 1 &&
($this->_request->getParam('g-recaptcha-response') !== null || $captcha !== false)) {
$this->redirectUrlError(__('Missing Url in "Form Post Paths" configuration field!'));
}
}
Expand Down Expand Up @@ -148,7 +172,6 @@ public function redirectUrlError($message)
$this->messageManager->getMessages(true);
$this->messageManager->addErrorMessage($message);
$this->_actionFlag->set('', Action::FLAG_NO_DISPATCH, true);

return $this->_responseInterface->setRedirect($this->redirect->getRefererUrl());
$this->_responseInterface->setRedirect($this->redirect->getRefererUrl());
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"google/recaptcha": "^1.2"
},
"type": "magento2-module",
"version": "1.1.2",
"version": "1.2.0",
"license": "proprietary",
"authors": [
{
"name": "Mageplaza",
"email": "[email protected]",
"homepage": "https://www.mageplaza.com",
"role": "Technical Support"
"role": "Technical Support "
}
],
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<source_model>Mageplaza\GoogleRecaptcha\Model\System\Config\Source\Language</source_model>
</field>
</group>
<group id="backend" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<group id="backend" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Backend Configuration</label>
<field id="enabled" translate="label comment" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable</label>
Expand Down Expand Up @@ -151,4 +151,4 @@
</group>
</section>
</system>
</config>
</config>
32 changes: 32 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<!--
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_GoogleRecaptcha
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="mpgooglerecaptcha_enable" xsi:type="object">Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command\Enable</item>
<item name="mpgooglerecaptcha_disable" xsi:type="object">Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command\Disable</item>
</argument>
</arguments>
</type>
</config>
7 changes: 7 additions & 0 deletions i18n/en_US.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"Disable backend captcha","Disable backend captcha"
"The captcha is disable for your admin website.","The captcha is disable for your admin website."
"The captcha backend has been successfully disabled. Please run the flush cache command again","The captcha backend has been successfully disabled. Please run the flush cache command again"
"Enable backend captcha","Enable backend captcha"
"The captcha is enabled for your admin website.","The captcha is enabled for your admin website."
"The captcha backend has been successfully enabled. Please run the flush cache command again","The captcha backend has been successfully enabled. Please run the flush cache command again"
"The response parameter is missing.","The response parameter is missing."
"The request is invalid or malformed.","The request is invalid or malformed."
"Admin Login","Admin Login"
Expand Down Expand Up @@ -91,6 +97,7 @@ Light,Light
message,message
"Missing required parameters recaptcha!","Missing required parameters recaptcha!"
"Missing Url in ""Form Post Paths"" configuration field!","Missing Url in ""Form Post Paths"" configuration field!"
"You need select captcha","You need select captcha"
"Google Recaptcha","Google Recaptcha"
General,General
Enable,Enable
Expand Down
13 changes: 4 additions & 9 deletions view/frontend/web/js/captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
*/

define([
'jquery'
], function ($) {
'jquery',
'mage/translate'
], function ($,$t) {
'use strict';

$.widget('mageplaza.captcha', {
Expand Down Expand Up @@ -109,7 +110,7 @@ define([
var divError = $('<div class="g-recaptcha-error"></div>');
var checkBox = $('<input type="checkbox" style="visibility: hidden" data-validate="{required:true}" class="mage-error">');

divError.text('You need select captcha').attr('style', 'display:none;color:red');
divError.text($t('You need select captcha')).attr('style', 'display:none;color:red');
divCaptcha.attr('id', 'mp' + '_recaptcha_' + number);
checkBox.attr('name', 'mp' + '_recaptcha_' + number);

Expand Down Expand Up @@ -208,12 +209,6 @@ define([

} else {
element.submit(function (event) {
if (element.attr('id') !== 'mpageverify-form') {
if (!element.valid()) {
return;
}
}

if (!self.stopSubmit) {
$.each(self.captchaForm, function (form, value) {
if (element.find('#' + value).length > 0) {
Expand Down

0 comments on commit 4aac419

Please sign in to comment.