UserDisable2faCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 56
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 5 1
A execute() 0 34 3
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\Command;
20
21
use App\Entity\User;
22
use App\Services\TFA\BackupCodeManager;
23
use Doctrine\ORM\EntityManagerInterface;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Style\SymfonyStyle;
29
30
class UserDisable2faCommand extends Command
31
{
32
    protected static $defaultName = 'app:user-disable-2fa';
33
34
    protected $entityManager;
35
    protected $backupCodeManager;
36
37
    public function __construct(EntityManagerInterface $entityManager, BackupCodeManager $backupCodeManager)
38
    {
39
        parent::__construct(self::$defaultName);
40
        $this->entityManager = $entityManager;
41
        $this->backupCodeManager = $backupCodeManager;
42
    }
43
44
    protected function configure()
45
    {
46
        $this
47
            ->setDescription('Disable all Two-Factor Authentication methods for the given user.')
48
            ->addArgument('username', InputArgument::REQUIRED, 'The username of the new user.')
49
        ;
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        $io = new SymfonyStyle($input, $output);
55
        $username = $input->getArgument('username');
56
57
        $repo = $this->entityManager->getRepository(User::class);
58
        $user = $repo->findOneBy([
59
            'username' => $username,
60
        ]);
61
62
        if (!$user) {
63
            $io->error('User not found!');
64
65
            return self::FAILURE;
66
        }
67
68
        $io->warning('You are about to remove all Two-Factor-Authentication methods of following user: '.$user->getUsername());
69
        $io->warning('Only continue if you are sure about the identity of the person that asked you to do this!');
70
71
        $continue = false;
72
        while (!$continue) {
73
            $continue = $io->confirm('Continue?', false);
74
        }
75
76
        //Disable google authenticator
77
        $user->setGoogleAuthenticatorSecret(null);
78
        //Disable backup codes
79
        $this->backupCodeManager->disableBackupCodesIfUnused($user);
80
81
        $this->entityManager->flush();
82
83
        $io->success('Two-Factor-Authentication disabled. The user should now be able to login again.');
84
85
        return 0;
86
    }
87
}
88