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