Completed
Push — master ( 22f213...24f6ce )
by Guilherme
17:25
created

TwoFactorAuthenticationController::enable2FA()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Controller;
12
13
use LoginCidadao\CoreBundle\Security\TwoFactorAuthenticationService;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
18
use LoginCidadao\CoreBundle\Form\Type\TwoFactorAuthenticationFormType;
19
use LoginCidadao\CoreBundle\Form\Type\TwoFactorAuthenticationDisableFormType;
20
use LoginCidadao\CoreBundle\Form\Type\TwoFactorAuthenticationBackupCodeGenerationFormType;
21
use Symfony\Component\Form\FormError;
22
23
/**
24
 * @Route("/two-factor")
25
 */
26
class TwoFactorAuthenticationController extends Controller
27
{
28
29
    /**
30
     * @Route("/enable", name="2fa_enable")
31
     * @Template()
32
     */
33
    public function enableAction(Request $request)
34
    {
35
        /** @var TwoFactorAuthenticationService $twoFactor */
36
        $twoFactor = $this->get('lc.two_factor');
37
38
        $translator = $this->get('translator');
39
        $person = $this->getUser();
40
        $person->setGoogleAuthenticatorSecret($twoFactor->generateSecret());
41
42
        $form = $this->createForm(new TwoFactorAuthenticationFormType(), $person);
43
        $form->handleRequest($request);
44
45
        if ($form->isValid()) {
46
            $verificationCode = $form->get('verification')->getData();
47
48
            try {
49
                $twoFactor->enable($form->getData(), $verificationCode);
50
51
                $message = $translator->trans('Two-Factor Authentication enabled.');
52
                $this->get('session')->getFlashBag()->add('success', $message);
53
54
                return $this->redirect($this->generateUrl("fos_user_change_password"));
55
            } catch (\InvalidArgumentException $e) {
56
                $message = $translator->trans($e->getMessage());
57
                $form->get('verification')->addError(new FormError($message));
58
            }
59
        }
60
61
        return ['form' => $form->createView(), 'secretUrl' => $twoFactor->getSecretUrl($person)];
0 ignored issues
show
Documentation introduced by
$person is of type null|object, but the function expects a object<LoginCidadao\Core...\Model\PersonInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
64
    /**
65
     * @Route("/disable", name="2fa_disable")
66
     * @Template()
67
     */
68
    public function disableAction(Request $request)
69
    {
70
        /** @var TwoFactorAuthenticationService $twoFactor */
71
        $twoFactor = $this->get('lc.two_factor');
72
73
        $person = $this->getUser();
74
        $form = $this->createForm(new TwoFactorAuthenticationDisableFormType(), $person);
75
        $form->handleRequest($request);
76
77
        if ($form->isValid()) {
78
            $translator = $this->get('translator');
79
            $twoFactor->disable($person);
0 ignored issues
show
Documentation introduced by
$person is of type null|object, but the function expects a object<LoginCidadao\Core...\Model\PersonInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
            $message = $translator->trans('Two-Factor Authentication disabled.');
81
            $this->get('session')->getFlashBag()->add('success', $message);
82
83
            return $this->redirect($this->generateUrl("fos_user_change_password"));
84
        }
85
86
        return ['form' => $form->createView()];
87
    }
88
89
    /**
90
     * @Route("/backup-codes/generate", name="2fa_backup_codes_generate")
91
     * @Template()
92
     */
93
    public function generateBackupCodesAction(Request $request)
94
    {
95
        /** @var TwoFactorAuthenticationService $twoFactor */
96
        $twoFactor = $this->get('lc.two_factor');
97
98
        $person = $this->getUser();
99
        $form = $this->createForm(new TwoFactorAuthenticationBackupCodeGenerationFormType(), $person);
100
        $form->handleRequest($request);
101
102
        if ($form->isValid()) {
103
            $twoFactor->removeBackupCodes($person);
0 ignored issues
show
Documentation introduced by
$person is of type null|object, but the function expects a object<LoginCidadao\Core...\Model\PersonInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
            $twoFactor->generateBackupCodes($person);
0 ignored issues
show
Documentation introduced by
$person is of type null|object, but the function expects a object<LoginCidadao\Core...\Model\PersonInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
106
            $message = $this->get('translator')
107
                ->trans('New Backup Codes generated. Don\'t forget to copy and store them safely.');
108
            $this->get('session')->getFlashBag()->add('success', $message);
109
110
            return $this->redirect($this->generateUrl("fos_user_change_password"));
111
        }
112
113
        return ['form' => $form->createView()];
114
    }
115
116
    /**
117
     * @Route("/teste")
118
     * @Template()
119
     */
120
    public function formAction()
121
    {
122
        return [];
123
    }
124
}
125