-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
104 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 |
---|---|---|
|
@@ -35,6 +35,8 @@ [email protected] | |
SUPPORT_EMAIL=[email protected] | ||
BASE_URI=https://localhost:8000 | ||
|
||
NEED_CAPTCHA=0 | ||
|
||
# determines wether an accounts is allowed to login or not, and wheter it can create / join other construction sites. | ||
# Possible values are: | ||
# - default_allow_self_association (by default, users can create / join construction sites) | ||
|
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
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
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,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the baupen project. | ||
* | ||
* (c) Florian Moser <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Form; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\NumberType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormError; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class CaptchaType extends AbstractType | ||
{ | ||
public const CAPTCHA_CHALLENGE_SESSION_KEY = '_captcha_challenge'; | ||
|
||
public function __construct(private readonly RequestStack $requestStack, private readonly TranslatorInterface $translator) | ||
{ | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this, 'validate']); | ||
} | ||
|
||
public function buildView(FormView $view, FormInterface $form, array $options): void | ||
{ | ||
$challenge = rand(1, 9); | ||
|
||
$session = $this->requestStack->getSession(); | ||
$session->set(self::CAPTCHA_CHALLENGE_SESSION_KEY, $challenge); | ||
|
||
$view->vars = array_merge($view->vars, ['challenge' => $challenge]); | ||
} | ||
|
||
public function validate(FormEvent $event): void | ||
{ | ||
$session = $this->requestStack->getSession(); | ||
$expectedChallenge = $session->get(self::CAPTCHA_CHALLENGE_SESSION_KEY, 0); | ||
|
||
$form = $event->getForm(); | ||
$actualChallenge = $form->getData(); | ||
|
||
// see translation files: user always asked to sum up $expectedChallenge + 2 | ||
if (intval($actualChallenge) - 2 !== $expectedChallenge) { | ||
$error = $this->translator->trans('captcha.invalid', [], 'security'); | ||
$form->addError(new FormError($error)); | ||
} | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults(['mapped' => false]); | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return NumberType::class; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->getBlockPrefix(); | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'captcha'; | ||
} | ||
} |
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
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
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