Skip to content

Commit 7c04057

Browse files
committed
Fix user creation when in Windows.
1 parent 8360995 commit 7c04057

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

src/NewCommand.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,109 @@ protected function makeSuperUser()
393393
return $this;
394394
}
395395

396+
// Since Windows cannot TTY, we'll capture their input here and make a user.
397+
if (PHP_OS_FAMILY === 'Windows') {
398+
return $this->makeSuperUserInWindows();
399+
}
400+
401+
// Otherwise, delegate to the `make:user` command with interactivity and let core handle the finer details.
396402
(new Please($this->output))
397403
->cwd($this->absolutePath)
398404
->run('make:user', '--super');
399405

400406
return $this;
401407
}
402408

409+
/**
410+
* Make super user in Windows.
411+
*
412+
* @return $this
413+
*/
414+
protected function makeSuperUserInWindows()
415+
{
416+
$please = (new Please($this->output))->cwd($this->absolutePath);
417+
418+
// Ask for email
419+
while (! isset($email) || ! $this->validateEmail($email)) {
420+
$email = $this->askForBasicInput('Email');
421+
}
422+
423+
// Ask for name
424+
$name = $this->askForBasicInput('Name');
425+
426+
// Ask for password
427+
while (! isset($password) || ! $this->validatePassword($password)) {
428+
$password = $this->askForBasicInput('Password (Your input will be hidden)', true);
429+
}
430+
431+
// Create super user and update with captured input.
432+
$please->run('make:user', '--super', $email);
433+
434+
$updateUser = '\Statamic\Facades\User::findByEmail('.escapeshellarg($email).')'
435+
. '->password('.escapeshellarg($password).')'
436+
. '->makeSuper()';
437+
438+
if ($name) {
439+
$updateUser .= '->set("name", '.escapeshellarg($name).')';
440+
}
441+
442+
$updateUser .= '->save();';
443+
444+
$please->run('tinker', '--execute', $updateUser);
445+
446+
return $this;
447+
}
448+
449+
/**
450+
* Ask for basic input.
451+
*
452+
* @param string $label
453+
* @param bool $hiddenInput
454+
* @return mixed
455+
*/
456+
protected function askForBasicInput($label, $hiddenInput = false)
457+
{
458+
return $this->getHelper('question')->ask(
459+
$this->input,
460+
new SymfonyStyle($this->input, $this->output),
461+
(new Question("{$label}: "))->setHidden($hiddenInput)
462+
);
463+
}
464+
465+
/**
466+
* Validate email address.
467+
*
468+
* @param string $email
469+
* @return bool
470+
*/
471+
protected function validateEmail($email)
472+
{
473+
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
474+
return true;
475+
}
476+
477+
$this->output->write("<error>Invalid email address.</error>".PHP_EOL);
478+
479+
return false;
480+
}
481+
482+
/**
483+
* Validate password.
484+
*
485+
* @param string $password
486+
* @return bool
487+
*/
488+
protected function validatePassword($password)
489+
{
490+
if (strlen($password) >= 8) {
491+
return true;
492+
}
493+
494+
$this->output->write("<error>The input must be at least 8 characters.</error>".PHP_EOL);
495+
496+
return false;
497+
}
498+
403499
/**
404500
* Show success message.
405501
*

0 commit comments

Comments
 (0)