From ca979c45ccb69c010bbbd6d4eee9df1ff4c839da Mon Sep 17 00:00:00 2001 From: "David E. Smith" Date: Thu, 18 May 2023 05:23:46 -0500 Subject: [PATCH] Add --show-password flag to user reset-password (#394) * First draft of password reset flag to show new pass * Correct indentation on feature test * Add --porcelain flag for user reset-password * Test for --porcelain * PHPCS corrections * PHPCS assignment alignment fix * Fix feature test formatting * Update README.md --------- Co-authored-by: David E. Smith <109608083+dsmith4-godaddy@users.noreply.github.com> Co-authored-by: Daniel Bachhuber --- README.md | 12 +++++++- features/user-reset-password.feature | 37 +++++++++++++++++++++++ src/User_Command.php | 45 ++++++++++++++++++++-------- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 0ed6c0a67..ef448d6a5 100644 --- a/README.md +++ b/README.md @@ -5300,7 +5300,7 @@ wp user remove-role [] Resets the password for one or more users. ~~~ -wp user reset-password ... [--skip-email] +wp user reset-password ... [--skip-email] [--show-password] [--porcelain] ~~~ **OPTIONS** @@ -5311,6 +5311,12 @@ wp user reset-password ... [--skip-email] [--skip-email] Don't send an email notification to the affected user(s). + [--show-password] + Show the new password(s). + + [--porcelain] + Output only the new password(s). + **EXAMPLES** # Reset the password for two users and send them the change email. @@ -5319,6 +5325,10 @@ wp user reset-password ... [--skip-email] Reset password for editor. Success: Passwords reset for 2 users. + # Reset the password for one user, displaying only the new password, and not sending the change email. + $ wp user reset-password admin --skip-email --porcelain + yV6BP*!d70wg + ### wp user session diff --git a/features/user-reset-password.feature b/features/user-reset-password.feature index 8d694ee38..b252a4a42 100644 --- a/features/user-reset-password.feature +++ b/features/user-reset-password.feature @@ -41,3 +41,40 @@ Feature: Reset passwords for one or more WordPress users. """ {ORIGINAL_PASSWORD} """ + + @require-wp-4.3 + Scenario: Reset the password of a WordPress user, and show the new password + Given a WP installation + + When I run `wp user get 1 --field=user_pass` + Then save STDOUT as {ORIGINAL_PASSWORD} + + When I run `wp user reset-password 1 --skip-email --show-password` + Then STDOUT should contain: + """ + Password: + """ + And an email should not be sent + + When I run `wp user get 1 --field=user_pass` + Then STDOUT should not contain: + """ + {ORIGINAL_PASSWORD} + """ + + @require-wp-4.3 + Scenario: Reset the password of a WordPress user, and show only the new password + Given a WP installation + + When I run `wp user get 1 --field=user_pass` + Then save STDOUT as {ORIGINAL_PASSWORD} + + When I run `wp user reset-password 1 --skip-email --porcelain` + Then STDOUT should not be empty + And an email should not be sent + + When I run `wp user get 1 --field=user_pass` + Then STDOUT should not contain: + """ + {ORIGINAL_PASSWORD} + """ diff --git a/src/User_Command.php b/src/User_Command.php index 25beb2ad4..25777c917 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1092,6 +1092,12 @@ public function import_csv( $args, $assoc_args ) { * [--skip-email] * : Don't send an email notification to the affected user(s). * + * [--show-password] + * : Show the new password(s). + * + * [--porcelain] + * : Output only the new password(s). + * * ## EXAMPLES * * # Reset the password for two users and send them the change email. @@ -1100,35 +1106,50 @@ public function import_csv( $args, $assoc_args ) { * Reset password for editor. * Success: Passwords reset for 2 users. * + * # Reset the password for one user, displaying only the new password, and not sending the change email. + * $ wp user reset-password admin --skip-email --porcelain + * yV6BP*!d70wg + * * @subcommand reset-password */ public function reset_password( $args, $assoc_args ) { - $skip_email = Utils\get_flag_value( $assoc_args, 'skip-email' ); + $porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' ); + $skip_email = Utils\get_flag_value( $assoc_args, 'skip-email' ); + $show_new_pass = Utils\get_flag_value( $assoc_args, 'show-password' ); + if ( $skip_email ) { add_filter( 'send_password_change_email', '__return_false' ); } $fetcher = new UserFetcher(); $users = $fetcher->get_many( $args ); foreach ( $users as $user ) { + $new_pass = wp_generate_password( 24 ); wp_update_user( [ 'ID' => $user->ID, - 'user_pass' => wp_generate_password( 24 ), + 'user_pass' => $new_pass, ] ); - WP_CLI::log( "Reset password for {$user->user_login}." ); - } - if ( $skip_email ) { - remove_filter( 'send_password_change_email', '__return_false' ); + if ( $porcelain ) { + WP_CLI::line( "$new_pass" ); + } else { + WP_CLI::log( "Reset password for {$user->user_login}." ); + if ( $show_new_pass ) { + WP_CLI::line( "Password: $new_pass" ); + } + } } $reset_user_count = count( $users ); - if ( 1 === $reset_user_count ) { - WP_CLI::success( "Password reset for {$reset_user_count} user." ); - } elseif ( $reset_user_count > 1 ) { - WP_CLI::success( "Passwords reset for {$reset_user_count} users." ); - } else { - WP_CLI::error( 'No user found to reset password.' ); + + if ( ! $porcelain ) { + if ( 1 === $reset_user_count ) { + WP_CLI::success( "Password reset for {$reset_user_count} user." ); + } elseif ( $reset_user_count > 1 ) { + WP_CLI::success( "Passwords reset for {$reset_user_count} users." ); + } else { + WP_CLI::error( 'No user found to reset password.' ); + } } }