]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CreateAdmin.php
Fixed occurances of altered titles in search results
[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 Symfony\Component\Console\Command\Command as SymfonyCommand;
8
9 class CreateAdmin extends Command
10 {
11     /**
12      * The name and signature of the console command.
13      *
14      * @var string
15      */
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}';
20
21     /**
22      * The console command description.
23      *
24      * @var string
25      */
26     protected $description = 'Add a new admin user to the system';
27
28     protected $userRepo;
29
30     /**
31      * Create a new command instance.
32      */
33     public function __construct(UserRepo $userRepo)
34     {
35         $this->userRepo = $userRepo;
36         parent::__construct();
37     }
38
39     /**
40      * Execute the console command.
41      *
42      * @throws \BookStack\Exceptions\NotFoundException
43      *
44      * @return mixed
45      */
46     public function handle()
47     {
48         $email = trim($this->option('email'));
49         if (empty($email)) {
50             $email = $this->ask('Please specify an email address for the new admin user');
51         }
52         if (mb_strlen($email) < 5 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
53             $this->error('Invalid email address provided');
54
55             return SymfonyCommand::FAILURE;
56         }
57
58         if ($this->userRepo->getByEmail($email) !== null) {
59             $this->error('A user with the provided email already exists!');
60
61             return SymfonyCommand::FAILURE;
62         }
63
64         $name = trim($this->option('name'));
65         if (empty($name)) {
66             $name = $this->ask('Please specify an name for the new admin user');
67         }
68         if (mb_strlen($name) < 2) {
69             $this->error('Invalid name provided');
70
71             return SymfonyCommand::FAILURE;
72         }
73
74         $password = trim($this->option('password'));
75         if (empty($password)) {
76             $password = $this->secret('Please specify a password for the new admin user');
77         }
78         if (mb_strlen($password) < 5) {
79             $this->error('Invalid password provided, Must be at least 5 characters');
80
81             return SymfonyCommand::FAILURE;
82         }
83
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;
88         $user->save();
89
90         $this->info("Admin account with email \"{$user->email}\" successfully created!");
91
92         return SymfonyCommand::SUCCESS;
93     }
94 }