3 namespace BookStack\Console\Commands;
5 use BookStack\Auth\UserRepo;
6 use Illuminate\Console\Command;
8 class CreateAdmin extends Command
11 * The name and signature of the console command.
15 protected $signature = 'bookstack:create-admin
16 {--email= : The email address for the new admin user}
17 {--name= : The name of the new admin user}
18 {--password= : The password to assign to the new admin user}';
21 * The console command description.
25 protected $description = 'Add a new admin user to the system';
30 * Create a new command instance.
32 public function __construct(UserRepo $userRepo)
34 $this->userRepo = $userRepo;
35 parent::__construct();
39 * Execute the console command.
42 * @throws \BookStack\Exceptions\NotFoundException
44 public function handle()
46 $email = trim($this->option('email'));
48 $email = $this->ask('Please specify an email address for the new admin user');
50 if (mb_strlen($email) < 5 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
51 return $this->error('Invalid email address provided');
54 if ($this->userRepo->getByEmail($email) !== null) {
55 return $this->error('A user with the provided email already exists!');
58 $name = trim($this->option('name'));
60 $name = $this->ask('Please specify an name for the new admin user');
62 if (mb_strlen($name) < 2) {
63 return $this->error('Invalid name provided');
66 $password = trim($this->option('password'));
67 if (empty($password)) {
68 $password = $this->secret('Please specify a password for the new admin user');
70 if (mb_strlen($password) < 5) {
71 return $this->error('Invalid password provided, Must be at least 5 characters');
75 $user = $this->userRepo->create(['email' => $email, 'name' => $name, 'password' => $password]);
76 $this->userRepo->attachSystemRole($user, 'admin');
77 $this->userRepo->downloadAndAssignUserAvatar($user);
78 $user->email_confirmed = true;
81 $this->info("Admin account with email \"{$user->email}\" successfully created!");