]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CreateAdminCommand.php
Commands: Added testing for initial admin changes
[bookstack] / app / Console / Commands / CreateAdminCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Users\Models\Role;
6 use BookStack\Users\UserRepo;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\Validator;
9 use Illuminate\Support\Str;
10 use Illuminate\Validation\Rules\Password;
11
12 class CreateAdminCommand extends Command
13 {
14     /**
15      * The name and signature of the console command.
16      *
17      * @var string
18      */
19     protected $signature = 'bookstack:create-admin
20                             {--email= : The email address for the new admin user}
21                             {--name= : The name of the new admin user}
22                             {--password= : The password to assign to the new admin user}
23                             {--external-auth-id= : The external authentication system id for the new admin user (SAML2/LDAP/OIDC)}
24                             {--generate-password : Generate a random password for the new admin user}
25                             {--initial : Indicate if this should set/update the details of the initial admin user}';
26
27     /**
28      * The console command description.
29      *
30      * @var string
31      */
32     protected $description = 'Add a new admin user to the system';
33
34     /**
35      * Execute the console command.
36      */
37     public function handle(UserRepo $userRepo): int
38     {
39         $initialAdminOnly = $this->option('initial');
40         $shouldGeneratePassword = $this->option('generate-password');
41         $details = $this->gatherDetails($shouldGeneratePassword, $initialAdminOnly);
42
43         $validator = Validator::make($details, [
44             'email'            => ['required', 'email', 'min:5'],
45             'name'             => ['required', 'min:2'],
46             'password'         => ['required_without:external_auth_id', Password::default()],
47             'external_auth_id' => ['required_without:password'],
48         ]);
49
50         if ($validator->fails()) {
51             foreach ($validator->errors()->all() as $error) {
52                 $this->error($error);
53             }
54
55             return 1;
56         }
57
58         $adminRole = Role::getSystemRole('admin');
59
60         if ($initialAdminOnly) {
61             $handled = $this->handleInitialAdminIfExists($userRepo, $details, $shouldGeneratePassword, $adminRole);
62             if ($handled !== null) {
63                 return $handled ? 0 : 1;
64             }
65         }
66
67         $emailUsed = $userRepo->getByEmail($details['email']) !== null;
68         if ($emailUsed) {
69             $this->error("Could not create admin account.");
70             $this->error("An account with the email address \"{$details['email']}\" already exists.");
71             return 1;
72         }
73
74         $user = $userRepo->createWithoutActivity($validator->validated());
75         $user->attachRole($adminRole);
76         $user->email_confirmed = true;
77         $user->save();
78
79         if ($shouldGeneratePassword) {
80             $this->line($details['password']);
81         } else {
82             $this->info("Admin account with email \"{$user->email}\" successfully created!");
83         }
84
85         return 0;
86     }
87
88     /**
89      * Handle updates to the original admin account if it exists.
90      * Returns true if it's been successfully handled, false if unsuccessful, or null if not handled.
91      */
92     protected function handleInitialAdminIfExists(UserRepo $userRepo, array $data, bool $generatePassword, Role $adminRole): bool|null
93     {
94         $defaultAdmin = $userRepo->getByEmail('[email protected]');
95         if ($defaultAdmin && $defaultAdmin->hasSystemRole('admin')) {
96             if ($defaultAdmin->email !== $data['email'] && $userRepo->getByEmail($data['email']) !== null) {
97                 $this->error("Could not create admin account.");
98                 $this->error("An account with the email address \"{$data['email']}\" already exists.");
99                 return false;
100             }
101
102             $userRepo->updateWithoutActivity($defaultAdmin, $data, true);
103             if ($generatePassword) {
104                 $this->line($data['password']);
105             } else {
106                 $this->info("The default admin user has been updated with the provided details!");
107             }
108
109             return true;
110         } else if ($adminRole->users()->count() > 0) {
111             $this->warn('Non-default admin user already exists. Skipping creation of new admin user.');
112             return true;
113         }
114
115         return null;
116     }
117
118     protected function gatherDetails(bool $generatePassword, bool $initialAdmin): array
119     {
120         $details = $this->snakeCaseOptions();
121
122         if (empty($details['email'])) {
123             if ($initialAdmin) {
124                 $details['email'] = '[email protected]';
125             } else {
126                 $details['email'] = $this->ask('Please specify an email address for the new admin user');
127             }
128         }
129
130         if (empty($details['name'])) {
131             if ($initialAdmin) {
132                 $details['name'] = 'Admin';
133             } else {
134                 $details['name'] = $this->ask('Please specify a name for the new admin user');
135             }
136         }
137
138         if (empty($details['password'])) {
139             if (empty($details['external_auth_id'])) {
140                 if ($generatePassword) {
141                     $details['password'] = Str::random(32);
142                 } else {
143                     $details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
144                 }
145             } else {
146                 $details['password'] = Str::random(32);
147             }
148         }
149
150         return $details;
151     }
152
153     protected function snakeCaseOptions(): array
154     {
155         $returnOpts = [];
156         foreach ($this->options() as $key => $value) {
157             $returnOpts[str_replace('-', '_', $key)] = $value;
158         }
159
160         return $returnOpts;
161     }
162 }