1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the GitControlBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Paul Schweppe <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace VersionControl\GitControlBundle\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Console\Question\Question; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Command to create a new admin user for the version control application. |
22
|
|
|
* |
23
|
|
|
* @author Paul Schweppe<[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class CreateAdminCommand extends ContainerAwareCommand |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @see Command |
29
|
|
|
*/ |
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->setName('version:admin:create') |
34
|
|
|
->setDescription('Create a new admin user.') |
35
|
|
|
->setDefinition(array( |
36
|
|
|
new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
37
|
|
|
new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
38
|
|
|
new InputArgument('password', InputArgument::REQUIRED, 'The password'), |
39
|
|
|
new InputArgument('name', InputArgument::REQUIRED, 'The display name'), |
40
|
|
|
)) |
41
|
|
|
->setHelp(<<<'EOT' |
42
|
|
|
The <info>version:admin:create</info> command creates an administrator user: |
43
|
|
|
|
44
|
|
|
<info>php app/console version:admin:create adminname</info> |
45
|
|
|
|
46
|
|
|
This interactive shell will ask you for an email, display name and then a password. |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
EOT |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @see Command |
55
|
|
|
*/ |
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
|
|
$username = $input->getArgument('username'); |
59
|
|
|
$email = $input->getArgument('email'); |
60
|
|
|
$password = $input->getArgument('password'); |
61
|
|
|
$name = $input->getArgument('name'); |
62
|
|
|
|
63
|
|
|
$this->createuser($username, $password, $email, $name); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$output->writeln(sprintf('Created user <comment>%s</comment>', $username)); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @see Command |
70
|
|
|
*/ |
71
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
72
|
|
|
{ |
73
|
|
|
if (!$this->getHelperSet()->has('question')) { |
74
|
|
|
$this->legacyInteract($input, $output); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$questions = array(); |
80
|
|
|
|
81
|
|
|
if (!$input->getArgument('username')) { |
82
|
|
|
$question = new Question('Please choose a username:'); |
83
|
|
|
$question->setValidator(function ($username) { |
84
|
|
|
if (empty($username)) { |
85
|
|
|
throw new \Exception('Username can not be empty'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $username; |
89
|
|
|
}); |
90
|
|
|
$questions['username'] = $question; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!$input->getArgument('email')) { |
94
|
|
|
$question = new Question('Please choose an email:'); |
95
|
|
|
$question->setValidator(function ($email) { |
96
|
|
|
if (empty($email)) { |
97
|
|
|
throw new \Exception('Email can not be empty'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $email; |
101
|
|
|
}); |
102
|
|
|
$questions['email'] = $question; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!$input->getArgument('password')) { |
106
|
|
|
$question = new Question('Please choose a password:'); |
107
|
|
|
$question->setValidator(function ($password) { |
108
|
|
|
if (empty($password)) { |
109
|
|
|
throw new \Exception('Password can not be empty'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $password; |
113
|
|
|
}); |
114
|
|
|
$question->setHidden(true); |
115
|
|
|
$questions['password'] = $question; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!$input->getArgument('name')) { |
119
|
|
|
$question = new Question('Please choose a display name:'); |
120
|
|
|
$question->setValidator(function ($name) { |
121
|
|
|
if (empty($name)) { |
122
|
|
|
throw new \Exception('Display name can not be empty'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $name; |
126
|
|
|
}); |
127
|
|
|
$questions['name'] = $question; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
foreach ($questions as $name => $question) { |
131
|
|
|
$answer = $this->getHelper('question')->ask($input, $output, $question); |
132
|
|
|
$input->setArgument($name, $answer); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Creates a new admin user. |
138
|
|
|
* |
139
|
|
|
* @param string $username |
140
|
|
|
* @param string $password |
141
|
|
|
* @param string $email |
142
|
|
|
* @param string $name |
143
|
|
|
* |
144
|
|
|
* @return \FOS\UserBundle\Model\UserInterface |
145
|
|
|
*/ |
146
|
|
|
protected function createUser($username, $password, $email, $name) |
147
|
|
|
{ |
148
|
|
|
$userManager = $this->getContainer()->get('fos_user.user_manager'); |
149
|
|
|
|
150
|
|
|
$user = $userManager->createUser(); |
151
|
|
|
$user->setUsername($username); |
152
|
|
|
$user->setEmail($email); |
153
|
|
|
$user->setPlainPassword($password); |
154
|
|
|
$user->setName($name); |
155
|
|
|
|
156
|
|
|
$user->setEnabled((bool) true); |
157
|
|
|
$user->addRole('ROLE_ADMIN'); |
158
|
|
|
//$user->setSuperAdmin((Boolean) true); |
159
|
|
|
|
160
|
|
|
$userManager->updateUser($user); |
161
|
|
|
|
162
|
|
|
return $user; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|