33namespace BookStack \Console \Commands ;
44
55use BookStack \Auth \UserRepo ;
6+ use BookStack \Exceptions \NotFoundException ;
67use Illuminate \Console \Command ;
78use Illuminate \Support \Facades \Validator ;
9+ use Illuminate \Support \Str ;
810use Illuminate \Validation \Rules \Password ;
911use Illuminate \Validation \Rules \Unique ;
1012use Symfony \Component \Console \Command \Command as SymfonyCommand ;
@@ -19,7 +21,8 @@ class CreateAdmin extends Command
1921 protected $ signature = 'bookstack:create-admin
2022 {--email= : The email address for the new admin user}
2123 {--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)} ' ;
2326
2427 /**
2528 * The console command description.
@@ -42,28 +45,35 @@ public function __construct(UserRepo $userRepo)
4245 /**
4346 * Execute the console command.
4447 *
45- * @throws \BookStack\Exceptions\ NotFoundException
48+ * @throws NotFoundException
4649 *
4750 * @return mixed
4851 */
4952 public function handle ()
5053 {
51- $ details = $ this ->options ();
54+ $ details = $ this ->snakeCaseOptions ();
5255
5356 if (empty ($ details ['email ' ])) {
5457 $ details ['email ' ] = $ this ->ask ('Please specify an email address for the new admin user ' );
5558 }
59+
5660 if (empty ($ details ['name ' ])) {
5761 $ details ['name ' ] = $ this ->ask ('Please specify a name for the new admin user ' );
5862 }
63+
5964 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+ }
6170 }
6271
6372 $ 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 ' ],
6777 ]);
6878
6979 if ($ validator ->fails ()) {
@@ -84,4 +94,14 @@ public function handle()
8494
8595 return SymfonyCommand::SUCCESS ;
8696 }
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+ }
87107}
0 commit comments