Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add password option to make:user command #11005

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
joshuablum marked this conversation as resolved.
Show resolved Hide resolved
$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()));
}
}
Loading