File size: 2,581 Bytes
e4f4821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

namespace Kanboard\Console;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class ResetPasswordCommand extends BaseCommand
{
    protected function configure()
    {
        $this
            ->setName('user:reset-password')
            ->setDescription('Change user password')
            ->addArgument('username', InputArgument::REQUIRED, 'Username')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $helper = $this->getHelper('question');
        $username = $input->getArgument('username');

        $passwordQuestion = new Question('What is the new password for '.$username.'? (characters are not printed)'.PHP_EOL);
        $passwordQuestion->setHidden(true);
        $passwordQuestion->setHiddenFallback(false);

        $password = $helper->ask($input, $output, $passwordQuestion);

        $confirmationQuestion = new Question('Confirmation:'.PHP_EOL);
        $confirmationQuestion->setHidden(true);
        $confirmationQuestion->setHiddenFallback(false);

        $confirmation = $helper->ask($input, $output, $confirmationQuestion);

        if ($this->validatePassword($output, $password, $confirmation)) {
            $this->resetPassword($output, $username, $password);
        }
        return 0;
    }

    private function validatePassword(OutputInterface $output, $password, $confirmation)
    {
        list($valid, $errors) = $this->passwordResetValidator->validateModification(array(
            'password' => $password,
            'confirmation' => $confirmation,
        ));

        if (!$valid) {
            foreach ($errors as $error_list) {
                foreach ($error_list as $error) {
                    $output->writeln('<error>'.$error.'</error>');
                }
            }
        }

        return $valid;
    }

    private function resetPassword(OutputInterface $output, $username, $password)
    {
        $userId = $this->userModel->getIdByUsername($username);

        if (empty($userId)) {
            $output->writeln('<error>User not found</error>');
            return false;
        }

        if (!$this->userModel->update(array('id' => $userId, 'password' => $password))) {
            $output->writeln('<error>Unable to update password</error>');
            return false;
        }

        $output->writeln('<info>Password updated successfully</info>');

        return true;
    }
}