-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from robinallezard/feat/add_recaptcha_create_form
feat: add recaptcha on create form
- Loading branch information
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
<language>en_US</language> | ||
<language>fr_FR</language> | ||
</languages> | ||
<version>2.0.0</version> | ||
<version>2.0.1</version> | ||
<author> | ||
<name>Julien Chanséaume</name> | ||
<email>[email protected]</email> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Comment\EventListener; | ||
|
||
use ReCaptcha\Event\ReCaptchaCheckEvent; | ||
use ReCaptcha\Event\ReCaptchaEvents; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Thelia\Model\Base\ModuleQuery; | ||
use Comment\Events\CommentEvents; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Comment\Events\CommentCreateEvent; | ||
|
||
class AddCommentListener implements EventSubscriberInterface | ||
{ | ||
/** @var EventDispatcherInterface */ | ||
protected $eventDispatcher; | ||
|
||
/** @var RequestStack */ | ||
protected $requestStack; | ||
|
||
public function __construct(EventDispatcherInterface $eventDispatcher, RequestStack $requestStack) | ||
{ | ||
$this->eventDispatcher = $eventDispatcher; | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function checkCaptcha(CommentCreateEvent $event) | ||
{ | ||
$currentRequest = $this->requestStack->getCurrentRequest(); | ||
if (!$currentRequest->request->has('admin_add_comment')) { | ||
return; | ||
} | ||
|
||
if (null !== ModuleQuery::create()->filterByCode("ReCaptcha")->filterByActivate(1)->findOne()) { | ||
$checkCaptchaEvent = new ReCaptchaCheckEvent(); | ||
$this->eventDispatcher->dispatch($checkCaptchaEvent, ReCaptchaEvents::CHECK_CAPTCHA_EVENT); | ||
if ($checkCaptchaEvent->isHuman() == false) { | ||
throw new \Exception('Invalid captcha'); | ||
} | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
CommentEvents::COMMENT_CREATE => ['checkCaptcha', 256], | ||
]; | ||
} | ||
} |