|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| - declare(strict_types=1); |
| 3 | +declare(strict_types=1); |
4 | 4 |
|
5 | 5 | namespace Bolt\Form;
|
6 | 6 |
|
7 |
| - use Bolt\Form\FieldTypes\PasswordWithPreviewType; |
8 |
| - use Symfony\Component\Form\AbstractType; |
9 |
| - use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
10 |
| - use Symfony\Component\Form\Extension\Core\Type\TextType; |
11 |
| - use Symfony\Component\Form\FormBuilderInterface; |
12 |
| - use Symfony\Component\OptionsResolver\OptionsResolver; |
13 |
| - use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
14 |
| - use Symfony\Component\Validator\Constraints\NotBlank; |
15 |
| - use Symfony\Contracts\Translation\TranslatorInterface; |
| 7 | +use Bolt\Form\FieldTypes\PasswordWithPreviewType; |
| 8 | +use Symfony\Component\Form\AbstractType; |
| 9 | +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
| 10 | +use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
| 11 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 12 | +use Symfony\Component\Form\FormBuilderInterface; |
| 13 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 14 | +use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
| 15 | +use Symfony\Component\Validator\Constraints\NotBlank; |
| 16 | +use Symfony\Contracts\Translation\TranslatorInterface; |
16 | 17 |
|
17 |
| - class LoginType extends AbstractType |
18 |
| - { |
19 |
| - /** @var AuthenticationUtils */ |
20 |
| - private $authenticationUtils; |
| 18 | +class LoginType extends AbstractType |
| 19 | +{ |
| 20 | + /** @var AuthenticationUtils */ |
| 21 | + private $authenticationUtils; |
21 | 22 |
|
22 |
| - /** @var TranslatorInterface */ |
23 |
| - private $translator; |
| 23 | + /** @var TranslatorInterface */ |
| 24 | + private $translator; |
24 | 25 |
|
25 |
| - public function __construct(AuthenticationUtils $authenticationUtils, TranslatorInterface $translator) |
26 |
| - { |
27 |
| - $this->authenticationUtils = $authenticationUtils; |
28 |
| - $this->translator = $translator; |
29 |
| - } |
| 26 | + /** @var int */ |
| 27 | + private $rememberLifetime; |
30 | 28 |
|
31 |
| - public function buildForm(FormBuilderInterface $builder, array $options): void |
32 |
| - { |
33 |
| - $builder |
34 |
| - ->add('username', TextType::class, [ |
35 |
| - 'label' => 'label.username_or_email', |
36 |
| - 'constraints' => [ |
37 |
| - new NotBlank([ |
38 |
| - 'message' => $this->translator->trans('form.empty_username_email'), |
39 |
| - ]), |
40 |
| - ], |
41 |
| - 'attr' => [ |
42 |
| - 'placeholder' => 'placeholder.username_or_email', |
43 |
| - ], |
44 |
| - 'data' => $this->authenticationUtils->getLastUsername(), |
45 |
| - ]) |
46 |
| - ->add('password', PasswordWithPreviewType::class, [ |
47 |
| - 'label' => 'label.password', |
48 |
| - 'constraints' => [ |
49 |
| - new NotBlank([ |
50 |
| - 'message' => $this->translator->trans('form.empty_password'), |
51 |
| - ]), |
52 |
| - ], |
53 |
| - // do not show the * red star |
54 |
| - 'required' => false, |
55 |
| - 'attr' => [ |
56 |
| - 'placeholder' => 'placeholder.password', |
57 |
| - ], |
58 |
| - ])->add('remember_me', CheckboxType::class, [ |
59 |
| - 'label' => 'label.rememberme', |
60 |
| - 'required' => false, |
61 |
| - ]); |
62 |
| - } |
| 29 | + public function __construct(AuthenticationUtils $authenticationUtils, TranslatorInterface $translator, int $rememberLifetime = 2592000) |
| 30 | + { |
| 31 | + $this->authenticationUtils = $authenticationUtils; |
| 32 | + $this->translator = $translator; |
| 33 | + |
| 34 | + // Defaults to 2592000, 30 days in seconds |
| 35 | + $this->rememberLifetime = $rememberLifetime; |
| 36 | + } |
63 | 37 |
|
64 |
| - // https://symfony.com/doc/current/security/csrf.html#csrf-protection-in-symfony-forms |
65 |
| - public function configureOptions(OptionsResolver $resolver): void |
66 |
| - { |
67 |
| - $resolver->setDefaults([ |
68 |
| - // enable/disable CSRF protection for this form |
69 |
| - 'csrf_protection' => true, |
70 |
| - // the name of the hidden HTML field that stores the token |
71 |
| - 'csrf_field_name' => '_token', |
72 |
| - // an arbitrary string used to generate the value of the token |
73 |
| - // using a different string for each form improves its security |
74 |
| - 'csrf_token_id' => 'login_csrf_token', |
| 38 | + public function buildForm(FormBuilderInterface $builder, array $options): void |
| 39 | + { |
| 40 | + $builder |
| 41 | + ->add('username', TextType::class, [ |
| 42 | + 'label' => 'label.username_or_email', |
| 43 | + 'constraints' => [ |
| 44 | + new NotBlank([ |
| 45 | + 'message' => $this->translator->trans('form.empty_username_email'), |
| 46 | + ]), |
| 47 | + ], |
| 48 | + 'attr' => [ |
| 49 | + 'placeholder' => 'placeholder.username_or_email', |
| 50 | + ], |
| 51 | + 'data' => $this->authenticationUtils->getLastUsername(), |
| 52 | + ]) |
| 53 | + ->add('password', PasswordWithPreviewType::class, [ |
| 54 | + 'label' => 'label.password', |
| 55 | + 'constraints' => [ |
| 56 | + new NotBlank([ |
| 57 | + 'message' => $this->translator->trans('form.empty_password'), |
| 58 | + ]), |
| 59 | + ], |
| 60 | + // do not show the * red star |
| 61 | + 'required' => false, |
| 62 | + 'attr' => [ |
| 63 | + 'placeholder' => 'placeholder.password', |
| 64 | + ], |
| 65 | + ]); |
| 66 | + |
| 67 | + if ($this->rememberLifetime > 0) { |
| 68 | + $builder->add('remember_me', CheckboxType::class, [ |
| 69 | + 'label' => 'label.remembermeduration', |
| 70 | + 'label_translation_parameters' => [ |
| 71 | + '%duration%' => 0 + sprintf('%0.1f', $this->rememberLifetime / 3600 / 24), |
| 72 | + ], |
| 73 | + 'required' => false, |
| 74 | + 'attr' => [ |
| 75 | + 'checked' => 'checked', |
| 76 | + ], |
75 | 77 | ]);
|
| 78 | + } else { |
| 79 | + $builder->add('remember_me', HiddenType::class); |
76 | 80 | }
|
77 | 81 | }
|
| 82 | + |
| 83 | + // https://symfony.com/doc/current/security/csrf.html#csrf-protection-in-symfony-forms |
| 84 | + public function configureOptions(OptionsResolver $resolver): void |
| 85 | + { |
| 86 | + $resolver->setDefaults([ |
| 87 | + // enable/disable CSRF protection for this form |
| 88 | + 'csrf_protection' => true, |
| 89 | + // the name of the hidden HTML field that stores the token |
| 90 | + 'csrf_field_name' => '_token', |
| 91 | + // an arbitrary string used to generate the value of the token |
| 92 | + // using a different string for each form improves its security |
| 93 | + 'csrf_token_id' => 'login_csrf_token', |
| 94 | + ]); |
| 95 | + } |
| 96 | +} |
0 commit comments