Passed
Push — master ( 10f39b...f31336 )
by Jan
03:26
created

SetPasswordCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Command;
4
5
use App\Entity\User;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
14
15
class SetPasswordCommand extends Command
16
{
17
    protected static $defaultName = 'app:set-password';
18
19
    protected $entityManager;
20
    protected $encoder;
21
22
    public function __construct(EntityManagerInterface $entityManager, UserPasswordEncoderInterface $passwordEncoder)
23
    {
24
        $this->entityManager = $entityManager;
25
        $this->encoder = $passwordEncoder;
26
27
28
        parent::__construct();
29
    }
30
31
32
    protected function configure()
33
    {
34
        $this
35
            ->setDescription('Sets the password of a user')
36
            ->setHelp('This password allows you to set the password of a user, without knowing the old password.')
37
            ->addArgument('user', InputArgument::REQUIRED, 'The name of the user')
38
        ;
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $io = new SymfonyStyle($input, $output);
44
        $user_name = $input->getArgument('user');
45
46
        /**
47
         * @var User $user
48
         */
49
        $users = $this->entityManager->getRepository(User::class)->findBy(['name' => $user_name]);
50
        $user = $users[0];
51
52
53
        if($user == null)
54
        {
55
            $io->error(sprintf('No user with the given username %s found in the database!', $user_name));
0 ignored issues
show
Bug introduced by
It seems like $user_name can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            $io->error(sprintf('No user with the given username %s found in the database!', /** @scrutinizer ignore-type */ $user_name));
Loading history...
56
            return;
57
        }
58
59
        $io->note('User found!');
60
61
        $proceed = $io->confirm(
62
            sprintf('You are going to change the password of %s with ID %d. Proceed?',
63
                $user->getFullName(true), $user->getID()));
64
65
        if(!$proceed)
66
        {
67
            return;
68
        }
69
70
        $success = false;
71
        $new_password = "";
72
73
        while(!$success) {
74
            $pw1 = $io->askHidden("Please enter new password:");
75
            $pw2 = $io->askHidden('Please confirm:');
76
            if($pw1 !== $pw2) {
77
                $io->error('The entered password did not match! Please try again.');
78
            } else {
79
                //Exit loop
80
                $success = true;
81
                $new_password = $pw1;
82
            }
83
        }
84
85
        //Encode password
86
        $hash = $this->encoder->encodePassword($user, $new_password);
87
        $user->setPassword($hash);
88
89
        //And save it to databae
90
        $this->entityManager->persist($user);
91
        $this->entityManager->flush();
92
93
        $io->success('Password was set successful! You can now log in using the new password.');
94
    }
95
}
96