Answer the question
In order to leave comments, you need to log in
Symfony2 access rights to site functionality. What mechanisms to use?
Good evening Toaster!
For example, you need to implement a captcha check for a regular user, but not for an admin, etc.
What is the best way to implement this, the captcha check logic: describe it directly in the controller or take the check somewhere outside? Where/how common?
I completed the captcha as a service. And how to issue when you need / do not need to use the captcha service?
UPD: I managed to find an opportunity in SF2 to create a field in type form not attached to an entity,
i.e. it is possible to check this field without using the entity received data
//namespace AppBundle\Form\Type;
//src: ...types/type.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name')->add('Поиск', 'submit');
if (1)//if from $options
{
$builder->add('recaptcha', 'ewz_recaptcha', array(
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
)
)); }
}
Answer the question
In order to leave comments, you need to log in
Checking the validity of the captcha value in the controller is a normal practice.
If standard forms are used, then in FormType you can pass an option like "show_capcha" and in buildForm(), based on the value of "show_capcha", show the captcha field or not.
Well, that's where you output and decide. And where you check ... Where it is convenient there and you can. Symphony has built-in checks a la isGranted
You can also do something like this:
1. Add a factory that will return one of 2 forms (with or without captcha), and the form with captcha will be inherited from the form without captcha.
2. This factory will already decide what to give to the user either based on security.authorization_checker->isGranted (which will require the creation of Voter), or based on the bundle configuration.
Thus, the controller will not contain unnecessary logic for it (rule 5-10-20 symfony.com/doc/current/best_practices/controllers...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question