TFAGoogleSettingsType::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Form\TFA;
20
21
use App\Entity\User;
22
use App\Validator\ValidGoogleAuthCode;
23
use Symfony\Component\Form\AbstractType;
24
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
25
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
26
use Symfony\Component\Form\Extension\Core\Type\TextType;
27
use Symfony\Component\Form\FormBuilderInterface;
28
use Symfony\Component\Form\FormEvent;
29
use Symfony\Component\Form\FormEvents;
30
use Symfony\Component\OptionsResolver\OptionsResolver;
31
32
class TFAGoogleSettingsType extends AbstractType
33
{
34
    protected $translator;
35
36
    public function __construct()
37
    {
38
    }
39
40
    public function buildForm(FormBuilderInterface $builder, array $options): void
41
    {
42
        $builder->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event): void {
43
            $form = $event->getForm();
44
            /** @var User $user */
45
            $user = $event->getData();
46
47
            //Only show setup fields, when google authenticator is not enabled
48
            if (!$user->isGoogleAuthenticatorEnabled()) {
49
                $form->add(
50
                    'google_confirmation',
51
                    TextType::class,
52
                    [
53
                        'mapped' => false,
54
                        'label' => 'tfa_google.code',
55
                        'attr' => [
56
                            'maxlength' => '6',
57
                            'minlength' => '6',
58
                            'pattern' => '\d*',
59
                            'autocomplete' => 'off',
60
                        ],
61
                        'constraints' => [new ValidGoogleAuthCode()],
62
                    ]
63
                );
64
65
                $form->add(
66
                    'googleAuthenticatorSecret',
67
                    HiddenType::class,
68
                    [
69
                        'disabled' => false,
70
                    ]
71
                );
72
73
                $form->add('submit', SubmitType::class, [
74
                    'label' => 'tfa_google.enable',
75
                    'attr' => [
76
                        'class' => 'btn-danger',
77
                    ],
78
                ]);
79
            } else {
80
                $form->add('submit', SubmitType::class, [
81
                    'label' => 'tfa_google.disable',
82
                    'attr' => [
83
                        'class' => 'btn-danger',
84
                    ],
85
                ]);
86
            }
87
        });
88
    }
89
90
    public function configureOptions(OptionsResolver $resolver): void
91
    {
92
        $resolver->setDefaults([
93
            'data_class' => User::class,
94
        ]);
95
    }
96
}
97