From 5740d06636f516d66b9ae468ae4a30cdfbc90211 Mon Sep 17 00:00:00 2001 From: Joshua Blum Date: Thu, 24 Oct 2024 15:58:00 +0200 Subject: [PATCH 1/4] Add `password` option to `make:user` command --- src/Console/Commands/MakeUser.php | 8 ++++++++ tests/Console/Commands/MakeUserTest.php | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Console/Commands/MakeUser.php b/src/Console/Commands/MakeUser.php index 87adde9466..6bf6766339 100644 --- a/src/Console/Commands/MakeUser.php +++ b/src/Console/Commands/MakeUser.php @@ -42,6 +42,12 @@ class MakeUser extends Command */ protected $email; + /** + * The user's password. + * @var string + */ + protected $password; + /** * The user's data. * @@ -172,6 +178,7 @@ protected function createUser() $user = User::make() ->email($this->email) + ->password($this->option('password')) ->data($this->data); if ($this->super || $this->option('super')) { @@ -241,6 +248,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..798493566e 100644 --- a/tests/Console/Commands/MakeUserTest.php +++ b/tests/Console/Commands/MakeUserTest.php @@ -106,4 +106,21 @@ 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->withoutMockingConsoleOutput(); + + $this->assertEmpty(User::all()); + + $this->artisan('statamic:make:user', ['email' => 'duncan@likesteatime.com', '--password' => 'PacManMoonwalk#84']) + ->expectsOutputToContain('User created successfully.'); + + $user = User::all()->first(); + + $this->assertNotEmpty($user->id()); + $this->assertEquals('duncan@likesteatime.com', $user->email()); + $this->assertNotEmpty($user->password()); + } } From 4f445e0e89b0f2c493613ade88ea49af00d7ed1a Mon Sep 17 00:00:00 2001 From: joshuablum Date: Thu, 24 Oct 2024 13:59:44 +0000 Subject: [PATCH 2/4] Fix styling --- src/Console/Commands/MakeUser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Console/Commands/MakeUser.php b/src/Console/Commands/MakeUser.php index 6bf6766339..b4350fdb52 100644 --- a/src/Console/Commands/MakeUser.php +++ b/src/Console/Commands/MakeUser.php @@ -44,6 +44,7 @@ class MakeUser extends Command /** * The user's password. + * * @var string */ protected $password; From b59d7aaff5e7879ff351c626486242d793668edc Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 24 Oct 2024 10:14:40 -0400 Subject: [PATCH 3/4] fix and adjust test --- tests/Console/Commands/MakeUserTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Console/Commands/MakeUserTest.php b/tests/Console/Commands/MakeUserTest.php index 798493566e..4ad679046a 100644 --- a/tests/Console/Commands/MakeUserTest.php +++ b/tests/Console/Commands/MakeUserTest.php @@ -110,11 +110,11 @@ public function it_generates_with_and_without_super_option() #[Test] public function it_can_make_a_user_with_password_option() { - $this->withoutMockingConsoleOutput(); - $this->assertEmpty(User::all()); - $this->artisan('statamic:make:user', ['email' => 'duncan@likesteatime.com', '--password' => 'PacManMoonwalk#84']) + $password = 'PacManMoonwalk#84'; + + $this->artisan('statamic:make:user', ['email' => 'duncan@likesteatime.com', '--password' => $password]) ->expectsOutputToContain('User created successfully.'); $user = User::all()->first(); @@ -122,5 +122,6 @@ public function it_can_make_a_user_with_password_option() $this->assertNotEmpty($user->id()); $this->assertEquals('duncan@likesteatime.com', $user->email()); $this->assertNotEmpty($user->password()); + $this->assertTrue(password_verify($password, $user->password())); } } From a77de9b5b711182e8f627242ca8afaab302a8c05 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 24 Oct 2024 10:30:04 -0400 Subject: [PATCH 4/4] prevent prompting for password if you use the option --- src/Console/Commands/MakeUser.php | 18 +++++++++++++----- tests/Console/Commands/MakeUserTest.php | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/MakeUser.php b/src/Console/Commands/MakeUser.php index b4350fdb52..a3aa0f9f76 100644 --- a/src/Console/Commands/MakeUser.php +++ b/src/Console/Commands/MakeUser.php @@ -43,7 +43,7 @@ class MakeUser extends Command protected $email; /** - * The user's password. + * The user's password. * * @var string */ @@ -74,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(); @@ -140,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(); @@ -179,8 +187,8 @@ protected function createUser() $user = User::make() ->email($this->email) - ->password($this->option('password')) - ->data($this->data); + ->data($this->data) + ->password($this->password); if ($this->super || $this->option('super')) { $user->makeSuper(); @@ -209,7 +217,7 @@ protected function emailValidationFails() protected function passwordValidationFails() { return $this->validationFails( - $this->data['password'], + $this->password, ['required', Password::default()] ); } diff --git a/tests/Console/Commands/MakeUserTest.php b/tests/Console/Commands/MakeUserTest.php index 4ad679046a..6b70e5b45a 100644 --- a/tests/Console/Commands/MakeUserTest.php +++ b/tests/Console/Commands/MakeUserTest.php @@ -124,4 +124,25 @@ public function it_can_make_a_user_with_password_option() $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())); + } }