]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CreateAdmin.php
Started work on details/summary blocks
[bookstack] / app / Console / Commands / CreateAdmin.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Auth\UserRepo;
6 use Illuminate\Console\Command;
7 use Illuminate\Support\Facades\Validator;
8 use Illuminate\Validation\Rules\Password;
9 use Illuminate\Validation\Rules\Unique;
10 use Symfony\Component\Console\Command\Command as SymfonyCommand;
11
12 class CreateAdmin 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
24     /**
25      * The console command description.
26      *
27      * @var string
28      */
29     protected $description = 'Add a new admin user to the system';
30
31     protected $userRepo;
32
33     /**
34      * Create a new command instance.
35      */
36     public function __construct(UserRepo $userRepo)
37     {
38         $this->userRepo = $userRepo;
39         parent::__construct();
40     }
41
42     /**
43      * Execute the console command.
44      *
45      * @throws \BookStack\Exceptions\NotFoundException
46      *
47      * @return mixed
48      */
49     public function handle()
50     {
51         $details = $this->options();
52
53         if (empty($details['email'])) {
54             $details['email'] = $this->ask('Please specify an email address for the new admin user');
55         }
56         if (empty($details['name'])) {
57             $details['name'] = $this->ask('Please specify a name for the new admin user');
58         }
59         if (empty($details['password'])) {
60             $details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
61         }
62
63         $validator = Validator::make($details, [
64             'email'    => ['required', 'email', 'min:5', new Unique('users', 'email')],
65             'name'     => ['required', 'min:2'],
66             'password' => ['required', Password::default()],
67         ]);
68
69         if ($validator->fails()) {
70             foreach ($validator->errors()->all() as $error) {
71                 $this->error($error);
72             }
73
74             return SymfonyCommand::FAILURE;
75         }
76
77         $user = $this->userRepo->create($validator->validated());
78         $this->userRepo->attachSystemRole($user, 'admin');
79         $this->userRepo->downloadAndAssignUserAvatar($user);
80         $user->email_confirmed = true;
81         $user->save();
82
83         $this->info("Admin account with email \"{$user->email}\" successfully created!");
84
85         return SymfonyCommand::SUCCESS;
86     }
87 }