3 namespace BookStack\Console\Commands;
5 use BookStack\Auth\UserRepo;
6 use Illuminate\Console\Command;
7 use Symfony\Component\Console\Command\Command as SymfonyCommand;
9 class CreateAdmin extends Command
12 * The name and signature of the console command.
16 protected $signature = 'bookstack:create-admin
17 {--email= : The email address for the new admin user}
18 {--name= : The name of the new admin user}
19 {--password= : The password to assign to the new admin user}';
22 * The console command description.
26 protected $description = 'Add a new admin user to the system';
31 * Create a new command instance.
33 public function __construct(UserRepo $userRepo)
35 $this->userRepo = $userRepo;
36 parent::__construct();
40 * Execute the console command.
42 * @throws \BookStack\Exceptions\NotFoundException
46 public function handle()
48 $email = trim($this->option('email'));
50 $email = $this->ask('Please specify an email address for the new admin user');
52 if (mb_strlen($email) < 5 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
53 $this->error('Invalid email address provided');
55 return SymfonyCommand::FAILURE;
58 if ($this->userRepo->getByEmail($email) !== null) {
59 $this->error('A user with the provided email already exists!');
61 return SymfonyCommand::FAILURE;
64 $name = trim($this->option('name'));
66 $name = $this->ask('Please specify an name for the new admin user');
68 if (mb_strlen($name) < 2) {
69 $this->error('Invalid name provided');
71 return SymfonyCommand::FAILURE;
74 $password = trim($this->option('password'));
75 if (empty($password)) {
76 $password = $this->secret('Please specify a password for the new admin user');
78 if (mb_strlen($password) < 5) {
79 $this->error('Invalid password provided, Must be at least 5 characters');
81 return SymfonyCommand::FAILURE;
84 $user = $this->userRepo->create(['email' => $email, 'name' => $name, 'password' => $password]);
85 $this->userRepo->attachSystemRole($user, 'admin');
86 $this->userRepo->downloadAndAssignUserAvatar($user);
87 $user->email_confirmed = true;
90 $this->info("Admin account with email \"{$user->email}\" successfully created!");
92 return SymfonyCommand::SUCCESS;