Skip to content

Commit

Permalink
Merge pull request #46 from statamic/fix/make-user-windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite authored Mar 3, 2022
2 parents 8360995 + 7c04057 commit 72cf37e
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,109 @@ protected function makeSuperUser()
return $this;
}

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

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

return $this;
}

/**
* Make super user in Windows.
*
* @return $this
*/
protected function makeSuperUserInWindows()
{
$please = (new Please($this->output))->cwd($this->absolutePath);

// Ask for email
while (! isset($email) || ! $this->validateEmail($email)) {
$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);

return $this;
}

/**
* Ask for basic input.
*
* @param string $label
* @param bool $hiddenInput
* @return mixed
*/
protected function askForBasicInput($label, $hiddenInput = false)
{
return $this->getHelper('question')->ask(
$this->input,
new SymfonyStyle($this->input, $this->output),
(new Question("{$label}: "))->setHidden($hiddenInput)
);
}

/**
* Validate email address.
*
* @param string $email
* @return bool
*/
protected function validateEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return true;
}

$this->output->write("<error>Invalid email address.</error>".PHP_EOL);

return false;
}

/**
* Validate password.
*
* @param string $password
* @return bool
*/
protected function validatePassword($password)
{
if (strlen($password) >= 8) {
return true;
}

$this->output->write("<error>The input must be at least 8 characters.</error>".PHP_EOL);

return false;
}

/**
* Show success message.
*
Expand Down

0 comments on commit 72cf37e

Please sign in to comment.