Skip to content

Commit

Permalink
add email and password options
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Oct 24, 2024
1 parent 4e59c96 commit 2746779
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ protected function configure()
->addOption('branch', null, InputOption::VALUE_REQUIRED, 'The branch that should be created for a new repository')
->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub', false)
->addOption('repo', null, InputOption::VALUE_REQUIRED, 'Optionally specify the name of the GitHub repository')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force install even if the directory already exists');
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force install even if the directory already exists')
->addOption('email', null, InputOption::VALUE_OPTIONAL, 'Creates a super user with this email address')
->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password for the super user');
}

protected function initialize(InputInterface $input, OutputInterface $output)
Expand Down Expand Up @@ -694,6 +696,12 @@ protected function installSsg()

protected function askToMakeSuperUser()
{
if ($this->input->getOption('email')) {
$this->makeUser = true;

return $this;
}

if (! $this->input->isInteractive()) {
return $this;
}
Expand Down Expand Up @@ -722,18 +730,35 @@ protected function makeSuperUser()
return $this;
}

$this->output->write(PHP_EOL.PHP_EOL);
intro("Let's create your super user account.");
$email = $this->input->getOption('email');
$password = $this->input->getOption('password') ?? 'password';

if (! $email) {
$this->output->write(PHP_EOL.PHP_EOL);
intro("Let's create your super user account.");
}

// Since Windows cannot TTY, we'll capture their input here and make a user.
if (PHP_OS_FAMILY === 'Windows') {
if ($this->input->isInteractive() && PHP_OS_FAMILY === 'Windows') {
return $this->makeSuperUserInWindows();
}

// Otherwise, delegate to the `make:user` command with interactivity and let core handle the finer details.
$command = ['make:user'];

if ($email) {
$command = [...$command, $email, '--password='.$password];
}

$command[] = '--super';

if (! $this->input->isInteractive()) {
$command[] = '--no-interaction';
}

// Otherwise, delegate to the `make:user` command and let core handle the finer details.
(new Please($this->output))
->cwd($this->absolutePath)
->run('make:user', '--super');
->run(...$command);

return $this;
}
Expand All @@ -752,28 +777,13 @@ protected function makeSuperUserInWindows()
$email = $this->askForBasicInput('Email');
}

// Ask for name
$name = $this->askForBasicInput('Name');

// Ask for password
while (! isset($password) || ! $this->validatePassword($password)) {
$password = $this->askForBasicInput('Password (Your input will be hidden)', true);
}

// Create super user and update with captured input.
$please->run('make:user', '--super', $email);

$updateUser = '\Statamic\Facades\User::findByEmail('.escapeshellarg($email).')'
.'->password('.escapeshellarg($password).')'
.'->makeSuper()';

if ($name) {
$updateUser .= '->set("name", '.escapeshellarg($name).')';
}

$updateUser .= '->save();';

$please->run('tinker', '--execute', $updateUser);
$please->run('make:user', $email, '--password='.$password, '--super');

return $this;
}
Expand Down

0 comments on commit 2746779

Please sign in to comment.