Skip to content

Commit

Permalink
Initial commit of Bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisaligent committed Jun 18, 2019
0 parents commit ab2b344
Show file tree
Hide file tree
Showing 14 changed files with 489 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2019-present FriendsOfOro, Aligent Consulting, and other contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Oro ReCAPTCHA Bundle
==============================
This bundle adds [Google ReCAPTCHA](https://developers.google.com/recaptcha/) protection for various Oro features.

Features which can currently be protected are:
* Registration Form
* Contact Us Form

Extends the Symfony [EWZRecaptchaBundle by excelwebzone](https://github.com/excelwebzone/EWZRecaptchaBundle)

Requirements
-------------------
* Oro Platform 3.X

Installation and Usage
-------------------
**NOTE: Adjust instructions as needed for your local environment**

1. Install via Composer:
```bash
composer require friendsoforo/oro-recaptcha-bundle
```
1. Update your config.yml:
```yaml
# app/config/config.yml
ewz_recaptcha:
public_key: here_is_your_public_key
private_key: here_is_your_private_key
# Not needed as "%kernel.default_locale%" is the default value for the locale key
locale_key: %kernel.default_locale%
```
1. Purge Oro cache:
```bash
php bin/console cache:clear --env=prod
```
1. Login to Oro Admin
1. Navigate to **System Configuration => Integrations => ReCAPTCHA**
1. Configure the ReCAPTCHA widget and enabled/disable Protected Features
1. Save the configuration and verify that it is now appearing on the frontend website

Testing in Development
-------------------
Copy the `config.yml` values into `config_dev.yml` and replace the public/private keys with the test keys provided by Google:
https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha-what-should-i-do

The widget should render on the forms, but will be overlaid with the text:

_"This reCAPTCHA is for testing purposes only. Please report to the site admin if you are seeing this.
"_

Adding to new Form Types
-------------------
1. Create a new Form Type Extension which extends `HackOro\RecaptchaBundle\Form\Extension\AbstractRecaptchaTypeExtension`
```php
<?php
namespace Acme\CustomBundle\Form\Extension;
use Acme\CustomBundle\Form\Type\CustomPageType;
class CustomPageTypeExtension extends AbstractRecaptchaTypeExtension
{
public function getExtendedType()
{
// The Form Type we are extending
return CustomPageType::class;
}
/**
* Protect the Custom Page Form?
* @return boolean
*/
public function isProtected()
{
// Replace this with a configuration option if needed
return true;
}
}
```
1. Register the Form Type Extension via `services.yml`:
```yaml
hack_oro_recaptcha.form.registration_form_type_extension:
class: Acme\CustomBundle\Form\Extension\CustomPageTypeExtension
calls:
- [setConfigManager, ['@oro_config.user']]
tags:
- { name: form.type_extension, extended_type: Acme\CustomBundle\Form\Type\CustomPageType }
```
Roadmap / Remaining Tasks
-------------------
- [ ] Add support for ["Invisible" ReCAPTCHA v2](https://developers.google.com/recaptcha/docs/invisible)
- [ ] Add support for [ReCAPTCHA v3](https://developers.google.com/recaptcha/docs/v3)
- [ ] Add ability to customize ReCAPTCHA v3 score threshold on a per-feature basis
- [ ] Add ability to set public/private keys via Oro Configuration instead of YAML files
Licence
-------------------
[MIT - MIT License](./LICENSE)
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "friendsoforo/oro-recaptcha-bundle",
"license": "MIT",
"description": "Oro Bundle to inject ReCAPTCHA into public forms",
"type": "project",
"require": {
"oro/platform": "3.*",
"excelwebzone/recaptcha-bundle": "^1.5"
},
"autoload": {
"psr-4": { "HackOro\\RecaptchaBundle\\": "./src/" }
}
}
58 changes: 58 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* @category HackOro
* @package ReCaptchaBundle
* @author Chris Rossi <chris.rossi@aligent.com.au>
* @copyright 2019 Aligent Consulting & Friends of Oro
* @license http://opensource.org/licenses/MIT MIT
*/

namespace HackOro\RecaptchaBundle\DependencyInjection;

use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\ConfigBundle\DependencyInjection\SettingsBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
const PUBLIC_KEY = 'public_key';
const PRIVATE_KEY = 'private_key';
const THEME = 'theme';
const SIZE = 'size';
const PROTECT_REGISTRATION = 'protect_registration';
const PROTECT_CONTACT_FORM = 'protect_contact_form';

/**
* Generates the configuration tree builder.
*
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root(HackOroRecaptchaExtension::ALIAS);

SettingsBuilder::append(
$rootNode,
[
self::PUBLIC_KEY => ['type' => 'text', 'value' => ''],
self::PRIVATE_KEY => ['type' => 'text', 'value' => ''],
self::THEME => ['type' => 'scalar', 'value' => 'light'], // light/dark (default light)
self::SIZE => ['type' => 'scalar', 'value' => 'normal'], // normal/compact (default normal)
self::PROTECT_REGISTRATION => ['type' => 'boolean', 'value' => true],
self::PROTECT_CONTACT_FORM=> ['type' => 'boolean', 'value' => true],
]
);
return $treeBuilder;
}

/**
* @param string $key
* @return string
*/
public static function getConfigKeyByName($key)
{
return implode(ConfigManager::SECTION_MODEL_SEPARATOR, [HackOroRecaptchaExtension::ALIAS, $key]);
}
}
33 changes: 33 additions & 0 deletions src/DependencyInjection/HackOroRecaptchaExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @category HackOro
* @package ReCaptchaBundle
* @author Chris Rossi <chris.rossi@aligent.com.au>
* @copyright 2019 Aligent Consulting & Friends of Oro
* @license http://opensource.org/licenses/MIT MIT
*/

namespace HackOro\RecaptchaBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class HackOroRecaptchaExtension extends Extension
{
const ALIAS = 'hack_oro_recaptcha';

/**
* @inheritdoc
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->prependExtensionConfig($this->getAlias(), $config);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
}
94 changes: 94 additions & 0 deletions src/Form/Extension/AbstractRecaptchaTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* @category HackOro
* @package ReCaptchaBundle
* @author Chris Rossi <chris.rossi@aligent.com.au>
* @copyright 2019 Aligent Consulting & Friends of Oro
* @license http://opensource.org/licenses/MIT MIT
*/

namespace HackOro\RecaptchaBundle\Form\Extension;

use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;
use HackOro\RecaptchaBundle\DependencyInjection\Configuration;
use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

abstract class AbstractRecaptchaTypeExtension extends AbstractTypeExtension
{
const DEFAULT_THEME = 'light';
const DEFAULT_SIZE = 'normal';

/** @var ConfigManager */
protected $configManager;

public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($this->isProtected()) {

$builder->add('recaptcha', EWZRecaptchaType::class, [
'attr' => [
'options' => [
'theme' => $this->getTheme(),
'type' => 'image',
'size' => $this->getSize(),
]
],
'mapped' => false,
'constraints' => [
new RecaptchaTrue(),
],
]);
}
}

/**
* @param ConfigManager $configManager
*/
public function setConfigManager(ConfigManager $configManager)
{
$this->configManager = $configManager;
}

/**
* @return mixed
*/
protected function getTheme()
{
return $this->getConfiguration(Configuration::THEME, self::DEFAULT_THEME);
}

/**
* @return mixed
*/
protected function getSize()
{
return $this->getConfiguration(Configuration::SIZE, self::DEFAULT_SIZE);
}

/**
* Get configuration option
* @param string $key
* @param $default
* @return mixed
*/
protected function getConfiguration(string $key, $default)
{
return $this->configManager->get(Configuration::getConfigKeyByName($key), $default);
}

/**
* @return boolean
*/
abstract public function isProtected();

/**
* Returns the name of the type being extended.
*
* @return string The name of the type being extended
*/
abstract public function getExtendedType();

}
30 changes: 30 additions & 0 deletions src/Form/Extension/ContactUsTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @category HackOro
* @package ReCaptchaBundle
* @author Chris Rossi <chris.rossi@aligent.com.au>
* @copyright 2019 Aligent Consulting & Friends of Oro
* @license http://opensource.org/licenses/MIT MIT
*/

namespace HackOro\RecaptchaBundle\Form\Extension;

use HackOro\RecaptchaBundle\DependencyInjection\Configuration;
use Oro\Bundle\ContactUsBundle\Form\Type\ContactRequestType;

class ContactUsTypeExtension extends AbstractRecaptchaTypeExtension
{
public function getExtendedType()
{
return ContactRequestType::class;
}

/**
* Protect the Contact Us Form?
* @return boolean
*/
public function isProtected()
{
return $this->getConfiguration(Configuration::PROTECT_CONTACT_FORM, false);
}
}
30 changes: 30 additions & 0 deletions src/Form/Extension/RegistrationTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @category HackOro
* @package ReCaptchaBundle
* @author Chris Rossi <chris.rossi@aligent.com.au>
* @copyright 2019 Aligent Consulting & Friends of Oro
* @license http://opensource.org/licenses/MIT MIT
*/

namespace HackOro\RecaptchaBundle\Form\Extension;

use HackOro\RecaptchaBundle\DependencyInjection\Configuration;
use Oro\Bundle\CustomerBundle\Form\Type\FrontendCustomerUserRegistrationType;

class RegistrationTypeExtension extends AbstractRecaptchaTypeExtension
{
public function getExtendedType()
{
return FrontendCustomerUserRegistrationType::class;
}

/**
* Protect the Registration Form?
* @return boolean
*/
public function isProtected()
{
return $this->getConfiguration(Configuration::PROTECT_REGISTRATION, false);
}
}
Loading

0 comments on commit ab2b344

Please sign in to comment.