Skip to content

Commit ca979c4

Browse files
dsXLIIdsmith4-godaddydanielbachhuber
authored
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 <[email protected]> Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 931bf8f commit ca979c4

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5300,7 +5300,7 @@ wp user remove-role <user> [<role>]
53005300
Resets the password for one or more users.
53015301

53025302
~~~
5303-
wp user reset-password <user>... [--skip-email]
5303+
wp user reset-password <user>... [--skip-email] [--show-password] [--porcelain]
53045304
~~~
53055305

53065306
**OPTIONS**
@@ -5311,6 +5311,12 @@ wp user reset-password <user>... [--skip-email]
53115311
[--skip-email]
53125312
Don't send an email notification to the affected user(s).
53135313

5314+
[--show-password]
5315+
Show the new password(s).
5316+
5317+
[--porcelain]
5318+
Output only the new password(s).
5319+
53145320
**EXAMPLES**
53155321

53165322
# Reset the password for two users and send them the change email.
@@ -5319,6 +5325,10 @@ wp user reset-password <user>... [--skip-email]
53195325
Reset password for editor.
53205326
Success: Passwords reset for 2 users.
53215327

5328+
# Reset the password for one user, displaying only the new password, and not sending the change email.
5329+
$ wp user reset-password admin --skip-email --porcelain
5330+
yV6BP*!d70wg
5331+
53225332

53235333

53245334
### wp user session

features/user-reset-password.feature

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,40 @@ Feature: Reset passwords for one or more WordPress users.
4141
"""
4242
{ORIGINAL_PASSWORD}
4343
"""
44+
45+
@require-wp-4.3
46+
Scenario: Reset the password of a WordPress user, and show the new password
47+
Given a WP installation
48+
49+
When I run `wp user get 1 --field=user_pass`
50+
Then save STDOUT as {ORIGINAL_PASSWORD}
51+
52+
When I run `wp user reset-password 1 --skip-email --show-password`
53+
Then STDOUT should contain:
54+
"""
55+
Password:
56+
"""
57+
And an email should not be sent
58+
59+
When I run `wp user get 1 --field=user_pass`
60+
Then STDOUT should not contain:
61+
"""
62+
{ORIGINAL_PASSWORD}
63+
"""
64+
65+
@require-wp-4.3
66+
Scenario: Reset the password of a WordPress user, and show only the new password
67+
Given a WP installation
68+
69+
When I run `wp user get 1 --field=user_pass`
70+
Then save STDOUT as {ORIGINAL_PASSWORD}
71+
72+
When I run `wp user reset-password 1 --skip-email --porcelain`
73+
Then STDOUT should not be empty
74+
And an email should not be sent
75+
76+
When I run `wp user get 1 --field=user_pass`
77+
Then STDOUT should not contain:
78+
"""
79+
{ORIGINAL_PASSWORD}
80+
"""

src/User_Command.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,12 @@ public function import_csv( $args, $assoc_args ) {
10921092
* [--skip-email]
10931093
* : Don't send an email notification to the affected user(s).
10941094
*
1095+
* [--show-password]
1096+
* : Show the new password(s).
1097+
*
1098+
* [--porcelain]
1099+
* : Output only the new password(s).
1100+
*
10951101
* ## EXAMPLES
10961102
*
10971103
* # Reset the password for two users and send them the change email.
@@ -1100,35 +1106,50 @@ public function import_csv( $args, $assoc_args ) {
11001106
* Reset password for editor.
11011107
* Success: Passwords reset for 2 users.
11021108
*
1109+
* # Reset the password for one user, displaying only the new password, and not sending the change email.
1110+
* $ wp user reset-password admin --skip-email --porcelain
1111+
* yV6BP*!d70wg
1112+
*
11031113
* @subcommand reset-password
11041114
*/
11051115
public function reset_password( $args, $assoc_args ) {
1106-
$skip_email = Utils\get_flag_value( $assoc_args, 'skip-email' );
1116+
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );
1117+
$skip_email = Utils\get_flag_value( $assoc_args, 'skip-email' );
1118+
$show_new_pass = Utils\get_flag_value( $assoc_args, 'show-password' );
1119+
11071120
if ( $skip_email ) {
11081121
add_filter( 'send_password_change_email', '__return_false' );
11091122
}
11101123
$fetcher = new UserFetcher();
11111124
$users = $fetcher->get_many( $args );
11121125
foreach ( $users as $user ) {
1126+
$new_pass = wp_generate_password( 24 );
11131127
wp_update_user(
11141128
[
11151129
'ID' => $user->ID,
1116-
'user_pass' => wp_generate_password( 24 ),
1130+
'user_pass' => $new_pass,
11171131
]
11181132
);
1119-
WP_CLI::log( "Reset password for {$user->user_login}." );
1120-
}
1121-
if ( $skip_email ) {
1122-
remove_filter( 'send_password_change_email', '__return_false' );
1133+
if ( $porcelain ) {
1134+
WP_CLI::line( "$new_pass" );
1135+
} else {
1136+
WP_CLI::log( "Reset password for {$user->user_login}." );
1137+
if ( $show_new_pass ) {
1138+
WP_CLI::line( "Password: $new_pass" );
1139+
}
1140+
}
11231141
}
11241142

11251143
$reset_user_count = count( $users );
1126-
if ( 1 === $reset_user_count ) {
1127-
WP_CLI::success( "Password reset for {$reset_user_count} user." );
1128-
} elseif ( $reset_user_count > 1 ) {
1129-
WP_CLI::success( "Passwords reset for {$reset_user_count} users." );
1130-
} else {
1131-
WP_CLI::error( 'No user found to reset password.' );
1144+
1145+
if ( ! $porcelain ) {
1146+
if ( 1 === $reset_user_count ) {
1147+
WP_CLI::success( "Password reset for {$reset_user_count} user." );
1148+
} elseif ( $reset_user_count > 1 ) {
1149+
WP_CLI::success( "Passwords reset for {$reset_user_count} users." );
1150+
} else {
1151+
WP_CLI::error( 'No user found to reset password.' );
1152+
}
11321153
}
11331154
}
11341155

0 commit comments

Comments
 (0)