Skip to content

Commit

Permalink
Add --show-password flag to user reset-password (#394)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Daniel Bachhuber <[email protected]>
  • Loading branch information
3 people authored May 18, 2023
1 parent 931bf8f commit ca979c4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5300,7 +5300,7 @@ wp user remove-role <user> [<role>]
Resets the password for one or more users.

~~~
wp user reset-password <user>... [--skip-email]
wp user reset-password <user>... [--skip-email] [--show-password] [--porcelain]
~~~

**OPTIONS**
Expand All @@ -5311,6 +5311,12 @@ wp user reset-password <user>... [--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.
Expand All @@ -5319,6 +5325,10 @@ wp user reset-password <user>... [--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
Expand Down
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).
*
* [--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

0 comments on commit ca979c4

Please sign in to comment.