Skip to content

Commit

Permalink
Merge pull request #2 from sitegeist/feasture/storeCredentialsInSettings
Browse files Browse the repository at this point in the history
FEATURE: Allow to store `sitekey` and `secret` in Settings.yaml
  • Loading branch information
gerdemann authored Apr 25, 2018
2 parents 2c50689 + f11816e commit 0938387
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
22 changes: 22 additions & 0 deletions Classes/Gerdemann/ReCAPTCHA/FormElements/ReCAPTCHAFormElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Gerdemann\ReCAPTCHA\FormElements;

use Neos\Flow\Annotations as Flow;
use Neos\Form\Core\Model\AbstractFormElement;

class ReCAPTCHAFormElement extends AbstractFormElement
{
/**
* @var string
* @Flow\InjectConfiguration(package="Gerdemann.ReCAPTCHA", path="sitekey")
*/
protected $sitekey;

/**
* @return string|null the site key from the settings
*/
public function getSiteKey()
{
return $this->sitekey;
}
}
11 changes: 9 additions & 2 deletions Classes/Gerdemann/ReCAPTCHA/Validation/ReCAPTCHAValidator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Gerdemann\ReCAPTCHA\Validation;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Client\Browser;
use Neos\Flow\Http\Client\CurlEngine;
use Neos\Flow\Validation\Validator\AbstractValidator;
Expand All @@ -10,11 +11,17 @@
*/
class ReCAPTCHAValidator extends AbstractValidator
{
/**
* @var string
* @Flow\InjectConfiguration(package="Gerdemann.ReCAPTCHA", path="secret")
*/
protected $secret;

/**
* @var array
*/
protected $supportedOptions = array(
'secret' => array('', ' The shared key between your site and reCAPTCHA', 'string', true)
'secret' => array('', ' The shared key between your site and reCAPTCHA', 'string', false)
);

/**
Expand All @@ -27,7 +34,7 @@ protected function isValid($value)
$browser = new Browser();
$browser->setRequestEngine(new CurlEngine());
$arguments = array(
'secret' => $this->getOptions()['secret'],
'secret' => $this->getOptions()['secret'] ? $this->getOptions()['secret'] : $this->secret,
'response' => $value
);
$response = $browser->request('https://www.google.com/recaptcha/api/siteverify', 'POST', $arguments);
Expand Down
6 changes: 6 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Neos:
default:
formElementTypes:
'Gerdemann.ReCAPTCHA:ReCAPTCHA':
implementationClassName: Gerdemann\ReCAPTCHA\FormElements\ReCAPTCHAFormElement
superTypes:
'Neos.Form:FormElement': true
renderingOptions:
Expand All @@ -15,3 +16,8 @@ Neos:
fusion:
autoInclude:
Gerdemann.ReCAPTCHA: true

Gerdemann:
ReCAPTCHA:
sitekey: ~
secret: ~
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ How-To:
secret: 'ENTER_HERE_YOUR_SHARED_SECRET
```

Settings:
-----------

You can predefine default values for `secret` and `sitekey` in
`Settings.yaml`. If no specific values are given in the form the
captcha-element will fallback to those values.

```
Gerdemann:
ReCAPTCHA:
sitekey: ~
secret: ~
```


Hint:
-------

Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Form/ReCAPTCHA.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="g-recaptcha"
data-sitekey="{element.properties.sitekey}"
data-sitekey="{f:if(condition: element.properties.sitekey, then: element.properties.sitekey, else: element.sitekey)}"
data-callback="onReCAPTCHASubmit"
data-size="invisible">
</div>
Expand Down

0 comments on commit 0938387

Please sign in to comment.