]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CreateAdmin.php
Revamped workings of WYSIWYG code blocks
[bookstack] / app / Console / Commands / CreateAdmin.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Auth\UserRepo;
6 use BookStack\Exceptions\NotFoundException;
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 use Symfony\Component\Console\Command\Command as SymfonyCommand;
13
14 class CreateAdmin extends Command
15 {
16     /**
17      * The name and signature of the console command.
18      *
19      * @var string
20      */
21     protected $signature = 'bookstack:create-admin
22                             {--email= : The email address for the new admin user}
23                             {--name= : The name of 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)}';
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     protected $userRepo;
35
36     /**
37      * Create a new command instance.
38      */
39     public function __construct(UserRepo $userRepo)
40     {
41         $this->userRepo = $userRepo;
42         parent::__construct();
43     }
44
45     /**
46      * Execute the console command.
47      *
48      * @throws NotFoundException
49      *
50      * @return mixed
51      */
52     public function handle()
53     {
54         $details = $this->snakeCaseOptions();
55
56         if (empty($details['email'])) {
57             $details['email'] = $this->ask('Please specify an email address for the new admin user');
58         }
59
60         if (empty($details['name'])) {
61             $details['name'] = $this->ask('Please specify a name for the new admin user');
62         }
63
64         if (empty($details['password'])) {
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             }
70         }
71
72         $validator = Validator::make($details, [
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'],
77         ]);
78
79         if ($validator->fails()) {
80             foreach ($validator->errors()->all() as $error) {
81                 $this->error($error);
82             }
83
84             return SymfonyCommand::FAILURE;
85         }
86
87         $user = $this->userRepo->createWithoutActivity($validator->validated());
88         $this->userRepo->attachSystemRole($user, 'admin');
89         $user->email_confirmed = true;
90         $user->save();
91
92         $this->info("Admin account with email \"{$user->email}\" successfully created!");
93
94         return SymfonyCommand::SUCCESS;
95     }
96
97     protected function snakeCaseOptions(): array
98     {
99         $returnOpts = [];
100         foreach ($this->options() as $key => $value) {
101             $returnOpts[str_replace('-', '_', $key)] = $value;
102         }
103
104         return $returnOpts;
105     }
106 }