3 namespace BookStack\Console\Commands;
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;
12 class CreateAdminCommand extends Command
15 * The name and signature of the console command.
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}';
28 * The console command description.
32 protected $description = 'Add a new admin user to the system';
35 * Execute the console command.
37 public function handle(UserRepo $userRepo): int
39 $initialAdminOnly = $this->option('initial');
40 $shouldGeneratePassword = $this->option('generate-password');
41 $details = $this->gatherDetails($shouldGeneratePassword, $initialAdminOnly);
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'],
50 if ($validator->fails()) {
51 foreach ($validator->errors()->all() as $error) {
58 $adminRole = Role::getSystemRole('admin');
60 if ($initialAdminOnly) {
61 $handled = $this->handleInitialAdminIfExists($userRepo, $details, $shouldGeneratePassword, $adminRole);
62 if ($handled !== null) {
63 return $handled ? 0 : 1;
67 $emailUsed = $userRepo->getByEmail($details['email']) !== null;
69 $this->error("Could not create admin account.");
70 $this->error("An account with the email address \"{$details['email']}\" already exists.");
74 $user = $userRepo->createWithoutActivity($validator->validated());
75 $user->attachRole($adminRole);
76 $user->email_confirmed = true;
79 if ($shouldGeneratePassword) {
80 $this->line($details['password']);
82 $this->info("Admin account with email \"{$user->email}\" successfully created!");
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.
92 protected function handleInitialAdminIfExists(UserRepo $userRepo, array $data, bool $generatePassword, Role $adminRole): bool|null
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.");
102 $userRepo->updateWithoutActivity($defaultAdmin, $data, true);
103 if ($generatePassword) {
104 $this->line($data['password']);
106 $this->info("The default admin user has been updated with the provided details!");
110 } else if ($adminRole->users()->count() > 0) {
111 $this->warn('Non-default admin user already exists. Skipping creation of new admin user.');
118 protected function gatherDetails(bool $generatePassword, bool $initialAdmin): array
120 $details = $this->snakeCaseOptions();
122 if (empty($details['email'])) {
126 $details['email'] = $this->ask('Please specify an email address for the new admin user');
130 if (empty($details['name'])) {
132 $details['name'] = 'Admin';
134 $details['name'] = $this->ask('Please specify a name for the new admin user');
138 if (empty($details['password'])) {
139 if (empty($details['external_auth_id'])) {
140 if ($generatePassword) {
141 $details['password'] = Str::random(32);
143 $details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
146 $details['password'] = Str::random(32);
153 protected function snakeCaseOptions(): array
156 foreach ($this->options() as $key => $value) {
157 $returnOpts[str_replace('-', '_', $key)] = $value;