]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CreateAdminCommand.php
Opensearch: Fixed XML declaration when php short tags enabled
[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
26     /**
27      * The console command description.
28      *
29      * @var string
30      */
31     protected $description = 'Add a new admin user to the system';
32
33     /**
34      * Execute the console command.
35      */
36     public function handle(UserRepo $userRepo): int
37     {
38         $details = $this->snakeCaseOptions();
39
40         if (empty($details['email'])) {
41             $details['email'] = $this->ask('Please specify an email address for the new admin user');
42         }
43
44         if (empty($details['name'])) {
45             $details['name'] = $this->ask('Please specify a name for the new admin user');
46         }
47
48         if (empty($details['password'])) {
49             if (empty($details['external_auth_id'])) {
50                 $details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
51             } else {
52                 $details['password'] = Str::random(32);
53             }
54         }
55
56         $validator = Validator::make($details, [
57             'email'            => ['required', 'email', 'min:5', new Unique('users', 'email')],
58             'name'             => ['required', 'min:2'],
59             'password'         => ['required_without:external_auth_id', Password::default()],
60             'external_auth_id' => ['required_without:password'],
61         ]);
62
63         if ($validator->fails()) {
64             foreach ($validator->errors()->all() as $error) {
65                 $this->error($error);
66             }
67
68             return 1;
69         }
70
71         $user = $userRepo->createWithoutActivity($validator->validated());
72         $user->attachRole(Role::getSystemRole('admin'));
73         $user->email_confirmed = true;
74         $user->save();
75
76         $this->info("Admin account with email \"{$user->email}\" successfully created!");
77
78         return 0;
79     }
80
81     protected function snakeCaseOptions(): array
82     {
83         $returnOpts = [];
84         foreach ($this->options() as $key => $value) {
85             $returnOpts[str_replace('-', '_', $key)] = $value;
86         }
87
88         return $returnOpts;
89     }
90 }