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

Add --show-password flag to user reset-password #394

Merged
merged 9 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
37 changes: 37 additions & 0 deletions features/user-reset-password.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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}
"""
45 changes: 33 additions & 12 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
dsXLII marked this conversation as resolved.
Show resolved Hide resolved
* [--porcelain]
* : Output only the new password(s).
*
* ## EXAMPLES
*
* # Reset the password for two users and send them the change email.
Expand All @@ -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.' );
}
}
}

Expand Down