3 namespace BookStack\Console\Commands;
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;
12 class CreateAdmin 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}';
25 * The console command description.
29 protected $description = 'Add a new admin user to the system';
34 * Create a new command instance.
36 public function __construct(UserRepo $userRepo)
38 $this->userRepo = $userRepo;
39 parent::__construct();
43 * Execute the console command.
45 * @throws \BookStack\Exceptions\NotFoundException
49 public function handle()
51 $details = $this->options();
53 if (empty($details['email'])) {
54 $details['email'] = $this->ask('Please specify an email address for the new admin user');
56 if (empty($details['name'])) {
57 $details['name'] = $this->ask('Please specify a name for the new admin user');
59 if (empty($details['password'])) {
60 $details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
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()],
69 if ($validator->fails()) {
70 foreach ($validator->errors()->all() as $error) {
74 return SymfonyCommand::FAILURE;
77 $user = $this->userRepo->create($validator->validated());
78 $this->userRepo->attachSystemRole($user, 'admin');
79 $this->userRepo->downloadAndAssignUserAvatar($user);
80 $user->email_confirmed = true;
83 $this->info("Admin account with email \"{$user->email}\" successfully created!");
85 return SymfonyCommand::SUCCESS;