Skip to content

Commit

Permalink
Merge pull request #192 from julienpillias/master
Browse files Browse the repository at this point in the history
add trusted roles
  • Loading branch information
excelwebzone authored Feb 22, 2018
2 parents 6cd55f8 + 04c582d commit d41d646
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getConfigTreeBuilder()
->booleanNode('ajax')->defaultFalse()->end()
->scalarNode('locale_key')->defaultValue('%kernel.default_locale%')->end()
->booleanNode('locale_from_request')->defaultFalse()->end()
->arrayNode('trusted_roles')->prototype('scalar')->end()
->end()
;

Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ services:
- '@request_stack'
- '%ewz_recaptcha.http_proxy%'
- '%ewz_recaptcha.verify_host%'
- '@security.authorization_checker'
- '%ewz_recaptcha.trusted_roles%'
tags:
- { name: validator.constraint_validator, alias: 'ewz_recaptcha.true' }
24 changes: 23 additions & 1 deletion src/Validator/Constraints/IsTrueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EWZ\Bundle\RecaptchaBundle\Validator\Constraints;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ValidatorException;
Expand Down Expand Up @@ -44,6 +45,20 @@ class IsTrueValidator extends ConstraintValidator
*/
protected $verifyHost;

/**
* Authorization Checker
*
* @var AuthorizationChecker
*/
protected $authorizationChecker;

/**
* Trusted Roles
*
* @var Array
*/
protected $trusted_roles;

/**
* The reCAPTCHA server URL's.
*/
Expand All @@ -56,13 +71,15 @@ class IsTrueValidator extends ConstraintValidator
* @param array $httpProxy
* @param bool $verifyHost
*/
public function __construct($enabled, $privateKey, RequestStack $requestStack, array $httpProxy, $verifyHost)
public function __construct($enabled, $privateKey, RequestStack $requestStack, array $httpProxy, $verifyHost, AuthorizationCheckerInterface $authorizationChecker, $trusted_roles)
{
$this->enabled = $enabled;
$this->privateKey = $privateKey;
$this->requestStack = $requestStack;
$this->httpProxy = $httpProxy;
$this->verifyHost = $verifyHost;
$this->authorizationChecker = $authorizationChecker;
$this->trusted_roles = $trusted_roles;
}

/**
Expand All @@ -75,6 +92,11 @@ public function validate($value, Constraint $constraint)
return;
}

// if we have an authorized role
if ($this->authorizationChecker->isGranted($this->trusted_roles)) {
return true;
}

// define variable for recaptcha check answer
$masterRequest = $this->requestStack->getMasterRequest();
$remoteip = $masterRequest->getClientIp();
Expand Down

0 comments on commit d41d646

Please sign in to comment.