3
3
namespace BookStack \Console \Commands ;
4
4
5
5
use BookStack \Auth \UserRepo ;
6
+ use BookStack \Exceptions \NotFoundException ;
6
7
use Illuminate \Console \Command ;
7
8
use Illuminate \Support \Facades \Validator ;
9
+ use Illuminate \Support \Str ;
8
10
use Illuminate \Validation \Rules \Password ;
9
11
use Illuminate \Validation \Rules \Unique ;
10
12
use Symfony \Component \Console \Command \Command as SymfonyCommand ;
@@ -19,7 +21,8 @@ class CreateAdmin extends Command
19
21
protected $ signature = 'bookstack:create-admin
20
22
{--email= : The email address for the new admin user}
21
23
{--name= : The name of the new admin user}
22
- {--password= : The password to assign to the new admin user} ' ;
24
+ {--password= : The password to assign to the new admin user}
25
+ {--external-auth-id= : The external authentication system id for the new admin user (SAML2/LDAP/OIDC)} ' ;
23
26
24
27
/**
25
28
* The console command description.
@@ -42,28 +45,35 @@ public function __construct(UserRepo $userRepo)
42
45
/**
43
46
* Execute the console command.
44
47
*
45
- * @throws \BookStack\Exceptions\ NotFoundException
48
+ * @throws NotFoundException
46
49
*
47
50
* @return mixed
48
51
*/
49
52
public function handle ()
50
53
{
51
- $ details = $ this ->options ();
54
+ $ details = $ this ->snakeCaseOptions ();
52
55
53
56
if (empty ($ details ['email ' ])) {
54
57
$ details ['email ' ] = $ this ->ask ('Please specify an email address for the new admin user ' );
55
58
}
59
+
56
60
if (empty ($ details ['name ' ])) {
57
61
$ details ['name ' ] = $ this ->ask ('Please specify a name for the new admin user ' );
58
62
}
63
+
59
64
if (empty ($ details ['password ' ])) {
60
- $ details ['password ' ] = $ this ->ask ('Please specify a password for the new admin user (8 characters min) ' );
65
+ if (empty ($ details ['external_auth_id ' ])) {
66
+ $ details ['password ' ] = $ this ->ask ('Please specify a password for the new admin user (8 characters min) ' );
67
+ } else {
68
+ $ details ['password ' ] = Str::random (32 );
69
+ }
61
70
}
62
71
63
72
$ validator = Validator::make ($ details , [
64
- 'email ' => ['required ' , 'email ' , 'min:5 ' , new Unique ('users ' , 'email ' )],
65
- 'name ' => ['required ' , 'min:2 ' ],
66
- 'password ' => ['required ' , Password::default ()],
73
+ 'email ' => ['required ' , 'email ' , 'min:5 ' , new Unique ('users ' , 'email ' )],
74
+ 'name ' => ['required ' , 'min:2 ' ],
75
+ 'password ' => ['required_without:external_auth_id ' , Password::default ()],
76
+ 'external_auth_id ' => ['required_without:password ' ],
67
77
]);
68
78
69
79
if ($ validator ->fails ()) {
@@ -84,4 +94,14 @@ public function handle()
84
94
85
95
return SymfonyCommand::SUCCESS ;
86
96
}
97
+
98
+ protected function snakeCaseOptions (): array
99
+ {
100
+ $ returnOpts = [];
101
+ foreach ($ this ->options () as $ key => $ value ) {
102
+ $ returnOpts [str_replace ('- ' , '_ ' , $ key )] = $ value ;
103
+ }
104
+
105
+ return $ returnOpts ;
106
+ }
87
107
}
0 commit comments