-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSetUserPasswordCommand.php
More file actions
135 lines (111 loc) · 4.2 KB
/
SetUserPasswordCommand.php
File metadata and controls
135 lines (111 loc) · 4.2 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php declare(strict_types=1);
namespace App\Command;
use LinkORB\OrgSync\DTO\User;
use LinkORB\OrgSync\SynchronizationMediator\SynchronizationMediatorInterface;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Yaml\Yaml;
class SetUserPasswordCommand extends Command
{
/** @var SynchronizationMediatorInterface */
private $synchronizationMediator;
/** @var string */
private $configBasePath;
public function __construct(SynchronizationMediatorInterface $synchronizationMediator, string $configBasePath)
{
$this->synchronizationMediator = $synchronizationMediator;
$this->configBasePath = $configBasePath;
parent::__construct();
}
public function configure()
{
$this->setName('linkorb:user:set-password')
->setDescription('Set password for passed user')
->addArgument(
'username',
null,
InputArgument::REQUIRED,
'The name of user'
)
->addOption(
'organization',
null,
InputOption::VALUE_OPTIONAL,
'The name of organization config',
SyncOrganizationCommand::DEFAULT_ORGANIZATION_CONFIG
)
->addOption(
'targets',
null,
InputOption::VALUE_OPTIONAL,
'The name of targets config',
SyncOrganizationCommand::DEFAULT_TARGETS_CONFIG
);
}
public function execute(InputInterface $input, OutputInterface $output)
{
$organization = $this->synchronizationMediator->initialize(
$this->getFromYaml($this->configBasePath . '/' . $input->getOption('targets')),
$this->getFromYaml($this->configBasePath . '/' . $input->getOption('organization'))
);
$userToSetPassword = $this->findUserToSetPassword($input->getArgument('username'), $organization->getUsers());
if (!$userToSetPassword) {
$output->writeln(
sprintf('<error>No user with username "%s" found!</error>', $input->getArgument('username'))
);
return 1;
}
$password = $this->getPassword($input, $output, 'New password: ');
if ($password !== $this->getPassword($input, $output, 'Repeat new password: ')) {
$output->writeln('<error>Passwords don`t match!</error>');
return 1;
}
$userToSetPassword->setPassword($password);
$userToSetPassword->setPreviousPassword(
$this->getPassword($input, $output, 'Previous password (if you have): ', false)
);
$this->synchronizationMediator->setPassword($userToSetPassword);
$output->writeln('Password changed successfully');
}
protected function getFromYaml(string $path): array
{
return Yaml::parseFile($path);
}
private function findUserToSetPassword(string $usernameToSetPassword, array $users): ?User
{
foreach ($users as $user) {
if ($user->getUsername() === $usernameToSetPassword) {
return $user;
}
}
return null;
}
private function getPassword(
InputInterface $input,
OutputInterface $output,
string $message,
bool $isRequired = true
): ?string
{
$helper = $this->getHelper('question');
$question = new Question($message);
$question->setHidden(true);
$question->setHiddenFallback(false);
if ($isRequired) {
$question->setValidator(function ($password) {
if (!is_string($password) || empty($password)) {
throw new RuntimeException(
'Password should be non empty string'
);
}
return $password;
});
}
return $helper->ask($input, $output, $question);
}
}