Skip to content

Commit

Permalink
[5.x] Add password option to make:user command (#11005)
Browse files Browse the repository at this point in the history
Co-authored-by: joshuablum <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent 58b912f commit 3258725
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Console/Commands/MakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class MakeUser extends Command
*/
protected $email;

/**
* The user's password.
*
* @var string
*/
protected $password;

/**
* The user's data.
*
Expand All @@ -67,6 +74,10 @@ public function handle()
return error(__('Statamic Pro is required.'));
}

if ($password = $this->option('password')) {
$this->password = $password;
}

// If email argument exists, non-interactively create user.
if ($this->email = $this->argument('email')) {
return $this->createUser();
Expand Down Expand Up @@ -133,7 +144,11 @@ protected function promptSeparateNameFields()
*/
protected function promptPassword()
{
$this->data['password'] = password(label: 'Password', required: true);
if ($this->password) {
return $this;
}

$this->password = password(label: 'Password', required: true);

if ($this->passwordValidationFails()) {
return $this->promptPassword();
Expand Down Expand Up @@ -172,7 +187,8 @@ protected function createUser()

$user = User::make()
->email($this->email)
->data($this->data);
->data($this->data)
->password($this->password);

if ($this->super || $this->option('super')) {
$user->makeSuper();
Expand Down Expand Up @@ -201,7 +217,7 @@ protected function emailValidationFails()
protected function passwordValidationFails()
{
return $this->validationFails(
$this->data['password'],
$this->password,
['required', Password::default()]
);
}
Expand Down Expand Up @@ -241,6 +257,7 @@ protected function getOptions()
{
return array_merge(parent::getOptions(), [
['super', '', InputOption::VALUE_NONE, 'Generate a super user with permission to do everything'],
['password', '', InputOption::VALUE_OPTIONAL, 'Generate a user with given password'],
]);
}
}
39 changes: 39 additions & 0 deletions tests/Console/Commands/MakeUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,43 @@ public function it_generates_with_and_without_super_option()
$this->assertTrue($jason->isSuper());
$this->assertFalse($girl->isSuper());
}

#[Test]
public function it_can_make_a_user_with_password_option()
{
$this->assertEmpty(User::all());

$password = 'PacManMoonwalk#84';

$this->artisan('statamic:make:user', ['email' => '[email protected]', '--password' => $password])
->expectsOutputToContain('User created successfully.');

$user = User::all()->first();

$this->assertNotEmpty($user->id());
$this->assertEquals('[email protected]', $user->email());
$this->assertNotEmpty($user->password());
$this->assertTrue(password_verify($password, $user->password()));
}

#[Test]
public function if_password_option_is_passed_it_will_not_prompt_for_password()
{
$this->assertEmpty(User::all());

$password = 'PacManMoonwalk#84';

$this->artisan('statamic:make:user', ['--password' => $password])
->expectsQuestion('Email', '[email protected]')
->expectsQuestion('Name', 'Duncan')
->expectsQuestion('Super user?', false)
->assertExitCode(0);

$user = User::all()->first();

$this->assertNotEmpty($user->id());
$this->assertEquals('[email protected]', $user->email());
$this->assertNotEmpty($user->password());
$this->assertTrue(password_verify($password, $user->password()));
}
}

0 comments on commit 3258725

Please sign in to comment.