diff --git a/src/Console/Commands/MakeUser.php b/src/Console/Commands/MakeUser.php index 87adde9466..a3aa0f9f76 100644 --- a/src/Console/Commands/MakeUser.php +++ b/src/Console/Commands/MakeUser.php @@ -42,6 +42,13 @@ class MakeUser extends Command */ protected $email; + /** + * The user's password. + * + * @var string + */ + protected $password; + /** * The user's data. * @@ -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(); @@ -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(); @@ -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(); @@ -201,7 +217,7 @@ protected function emailValidationFails() protected function passwordValidationFails() { return $this->validationFails( - $this->data['password'], + $this->password, ['required', Password::default()] ); } @@ -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'], ]); } } diff --git a/tests/Console/Commands/MakeUserTest.php b/tests/Console/Commands/MakeUserTest.php index 8e31e8382c..6b70e5b45a 100644 --- a/tests/Console/Commands/MakeUserTest.php +++ b/tests/Console/Commands/MakeUserTest.php @@ -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' => 'duncan@likesteatime.com', '--password' => $password]) + ->expectsOutputToContain('User created successfully.'); + + $user = User::all()->first(); + + $this->assertNotEmpty($user->id()); + $this->assertEquals('duncan@likesteatime.com', $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', 'duncan@likesteatime.com') + ->expectsQuestion('Name', 'Duncan') + ->expectsQuestion('Super user?', false) + ->assertExitCode(0); + + $user = User::all()->first(); + + $this->assertNotEmpty($user->id()); + $this->assertEquals('duncan@likesteatime.com', $user->email()); + $this->assertNotEmpty($user->password()); + $this->assertTrue(password_verify($password, $user->password())); + } }