2
2
3
3
namespace Fuzzyma \Contao \DatabaseCommandsBundle \Command ;
4
4
5
- use Contao \CoreBundle \Command \AbstractLockedCommand ;
5
+ use Contao \CoreBundle \Framework \ContaoFramework ;
6
+ use Contao \InstallationBundle \InstallTool ;
6
7
use Patchwork \Utf8 ;
8
+ use Symfony \Component \Console \Exception \RuntimeException ;
7
9
use Symfony \Component \Console \Input \InputInterface ;
8
10
use Symfony \Component \Console \Input \InputOption ;
9
11
use Symfony \Component \Console \Output \OutputInterface ;
10
12
use Symfony \Component \Console \Question \Question ;
11
13
12
- class DatabaseAddAdminCommand extends AbstractLockedCommand
14
+ class DatabaseAddAdminCommand extends BaseCommand
13
15
{
14
16
15
- private $ parameters = [];
16
- private $ abort = false ;
17
+ private $ locale ;
18
+
19
+ public function __construct (ContaoFramework $ framework , InstallTool $ installTool , $ locale )
20
+ {
21
+ parent ::__construct ($ framework , $ installTool );
22
+
23
+ $ this ->locale = $ locale ;
24
+ }
25
+
17
26
18
27
protected function configure ()
19
28
{
@@ -26,6 +35,7 @@ protected function configure()
26
35
new InputOption ('name ' , 'a ' , InputOption::VALUE_REQUIRED , 'Name ' ),
27
36
new InputOption ('email ' , 'm ' , InputOption::VALUE_REQUIRED , 'Email ' ),
28
37
new InputOption ('password ' , 'p ' , InputOption::VALUE_REQUIRED , 'Password ' ),
38
+ new InputOption ('force ' , 'f ' , InputOption::VALUE_NONE | InputOption::VALUE_OPTIONAL , 'Add admin even if there is already an admin in the table ' )
29
39
));
30
40
}
31
41
@@ -37,21 +47,17 @@ public function getQuestion($question, $default, $sep = ':')
37
47
protected function interact (InputInterface $ input , OutputInterface $ output )
38
48
{
39
49
40
- $ installTool = $ this ->getContainer ()->get ('contao.install_tool ' );
41
-
42
- if (!$ installTool ->hasTable ('tl_user ' )) {
43
- $ output ->writeln ('<error>Error: tl_user does not exist</error> ' );
44
- return null ;
50
+ if (!$ this ->installTool ->hasTable ('tl_user ' )) {
51
+ throw new RuntimeException ('<error>Error: tl_user does not exist</error> ' , 1 );
45
52
}
46
53
47
54
48
55
$ questionHelper = $ this ->getHelper ('question ' );
49
56
50
- if ($ installTool ->hasAdminUser ()) {
57
+ if ($ this -> installTool ->hasAdminUser () && ! $ input -> getOption ( ' force ' )) {
51
58
$ question = new Question ($ this ->getQuestion ('Admin entry already present in tl_user table. Add anyway? ' , 'no ' ), 'no ' );
52
- if ($ questionHelper ->ask ($ input , $ output , $ question ) == 'no ' ) {
53
- $ this ->abort = true ;
54
- return ;
59
+ if ($ questionHelper ->ask ($ input , $ output , $ question ) !== 'yes ' ) {
60
+ throw new RuntimeException ('<error>Aborted: Admin entry already present in tl_user table.</error> ' , 2 );
55
61
}
56
62
}
57
63
@@ -99,13 +105,13 @@ protected function interact(InputInterface $input, OutputInterface $output)
99
105
}
100
106
101
107
$ password = $ input ->getOption ('password ' );
102
- $ minLength = $ installTool ->getConfig ('minPasswordLength ' );
108
+ $ minLength = $ this -> installTool ->getConfig ('minPasswordLength ' );
103
109
104
- $ question = new Question ('<info>Enter a password</info> ' );
110
+ $ question = new Question ('<info>Enter a password: </info> ' );
105
111
$ question ->setValidator (function ($ answer ) use ($ minLength , $ username ) {
106
112
return $ this ->passwordValidator ($ answer , $ minLength , $ username );
107
113
});
108
- $ question ->setHidden (true );
114
+ // $question->setHidden(true); see symfony/symfony#19463
109
115
$ question ->setMaxAttempts (3 );
110
116
111
117
if (!$ password ) {
@@ -119,42 +125,37 @@ protected function interact(InputInterface $input, OutputInterface $output)
119
125
}
120
126
}
121
127
122
- $ this ->parameters = [
123
- 'username ' => $ username ,
124
- 'name ' => $ name ,
125
- 'email ' => $ email ,
126
- 'password ' => $ password
127
- ];
128
+ $ input ->setOption ('username ' , $ username );
129
+ $ input ->setOption ('name ' , $ name );
130
+ $ input ->setOption ('email ' , $ email );
131
+ $ input ->setOption ('password ' , $ password );
128
132
129
133
}
130
134
131
- protected function executeLocked (InputInterface $ input , OutputInterface $ output )
135
+ protected function execute (InputInterface $ input , OutputInterface $ output )
132
136
{
133
137
134
- if ( $ this ->abort ) return ;
138
+ $ this ->framework -> initialize () ;
135
139
136
- $ installTool = $ this ->getContainer ()->get ('contao.install_tool ' );
137
- $ this ->getContainer ()->get ('contao.framework ' )->initialize ();
138
-
139
- $ username = isset ($ this ->parameters ['username ' ]) ? $ this ->parameters ['username ' ] : $ input ->getOption ('username ' );
140
- $ name = isset ($ this ->parameters ['name ' ]) ? $ this ->parameters ['name ' ] : $ input ->getOption ('name ' );
141
- $ email = isset ($ this ->parameters ['email ' ]) ? $ this ->parameters ['email ' ] : $ input ->getOption ('email ' );
142
- $ password = isset ($ this ->parameters ['password ' ]) ? $ this ->parameters ['password ' ] : $ input ->getOption ('password ' );
140
+ $ username = $ input ->getOption ('username ' );
141
+ $ name = $ input ->getOption ('name ' );
142
+ $ email = $ input ->getOption ('email ' );
143
+ $ password = $ input ->getOption ('password ' );
143
144
144
- $ minLength = $ installTool ->getConfig ('minPasswordLength ' );
145
+ $ minLength = $ this -> installTool ->getConfig ('minPasswordLength ' );
145
146
146
147
$ this ->usernameValidator ($ username );
147
148
$ this ->emailValidator ($ email );
148
149
$ this ->passwordValidator ($ password , $ minLength , $ username );
149
150
150
- $ installTool ->persistConfig ('adminEmail ' , $ email );
151
+ $ this -> installTool ->persistConfig ('adminEmail ' , $ email );
151
152
152
- $ installTool ->persistAdminUser (
153
+ $ this -> installTool ->persistAdminUser (
153
154
$ username ,
154
155
$ name ,
155
156
$ email ,
156
157
$ password ,
157
- $ this ->getContainer ()-> getParameter ( ' locale ' )
158
+ $ this ->locale
158
159
);
159
160
160
161
$ output ->writeln ('<info>Success: Admin user added</info> ' );
0 commit comments