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 * @param UserRepo $userRepo
34 public function __construct(UserRepo $userRepo)
36 $this->userRepo = $userRepo;
37 parent::__construct();
41 * Execute the console command.
44 * @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 (strlen($email) < 5 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
53 return $this->error('Invalid email address provided');
56 if ($this->userRepo->getByEmail($email) !== null) {
57 return $this->error('A user with the provided email already exists!');
60 $name = trim($this->option('name'));
62 $name = $this->ask('Please specify an name for the new admin user');
64 if (strlen($name) < 2) {
65 return $this->error('Invalid name provided');
68 $password = trim($this->option('password'));
69 if (empty($password)) {
70 $password = $this->secret('Please specify a password for the new admin user');
72 if (strlen($password) < 5) {
73 return $this->error('Invalid password provided, Must be at least 5 characters');
77 $user = $this->userRepo->create(['email' => $email, 'name' => $name, 'password' => $password]);
78 $this->userRepo->attachSystemRole($user, 'admin');
79 $this->userRepo->downloadGravatarToUserAvatar($user);
80 $user->email_confirmed = true;
83 $this->info("Admin account with email \"{$user->email}\" successfully created!");