Skip to content

Commit

Permalink
Merge pull request #21 from robinallezard/feat/add_recaptcha_create_form
Browse files Browse the repository at this point in the history
feat: add recaptcha on create form
  • Loading branch information
NicolasBarbey authored Aug 1, 2024
2 parents 791aa30 + 67cbcb5 commit ed44dea
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
50 changes: 50 additions & 0 deletions EventListener/AddCommentListener.php
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],
];
}
}

0 comments on commit ed44dea

Please sign in to comment.