Skip to content

Commit 51efe1c

Browse files
committed
add check-password subcommand to validate user credentials
1 parent 035b74e commit 51efe1c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

features/user.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ Feature: Manage WordPress users
6464
When I run `wp user delete {USER_ID} --yes`
6565
Then STDOUT should not be empty
6666

67+
When I run `wp user create testuser3 testuser3@example.com --user_pass=testuser3pass`
68+
Then STDOUT should not contain:
69+
"""
70+
Password:
71+
"""
72+
73+
# Check with valid password.
74+
When I run `wp user check-password testuser3 testuser3pass`
75+
Then the return code should be 0
76+
77+
# Check with invalid password.
78+
When I try `wp user check-password testuser3 invalidpass`
79+
Then the return code should be 1
80+
81+
When I try `wp user check-password invaliduser randomstring`
82+
Then STDERR should contain:
83+
"""
84+
Invalid user ID, email or login: 'invaliduser'
85+
"""
86+
And the return code should be 1
87+
6788
Scenario: Reassigning user posts
6889
Given a WP multisite install
6990

src/User_Command.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,4 +1197,41 @@ private function update_msuser_status( $user_ids, $pref, $value ) {
11971197
Utils\report_batch_operation_results( 'user', $verb, count( $user_ids ), $successes, $errors );
11981198
}
11991199

1200+
/**
1201+
* Checks if given user credentials are valid or not.
1202+
*
1203+
* ## OPTIONS
1204+
*
1205+
* <user>
1206+
* : The user login or user email of the user to check credentials.
1207+
*
1208+
* <user_pass>
1209+
* : A string that contains the plain text password for the user.
1210+
*
1211+
* ## EXAMPLES
1212+
*
1213+
* # Check whether given credentials are valid; exit status 0 if valid, otherwise 1
1214+
* $ wp user check-password admin adminpass
1215+
* $ echo $?
1216+
* 1
1217+
*
1218+
* # Bash script for checking whether given credentials are valid or not
1219+
* if ! $(wp user check-password admin adminpass); then
1220+
* notify-send "Invalid Credentials";
1221+
* fi
1222+
*
1223+
* @subcommand check-password
1224+
*/
1225+
public function check_password( $args ) {
1226+
1227+
$user = $this->fetcher->get_check( $args[0] );
1228+
$user_pass = $args[1];
1229+
1230+
if ( wp_check_password( $user_pass, $user->data->user_pass, $user->ID ) ) {
1231+
WP_CLI::halt( 0 );
1232+
} else {
1233+
WP_CLI::halt( 1 );
1234+
}
1235+
}
1236+
12001237
}

0 commit comments

Comments
 (0)