Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/04/2020 01:29:50 PM (6 years ago)
Author:
imath
Message:

BP Members: improve our password validation process

We're introducing a new function to validate the member's chosen password: bp_members_validate_user_password().

This function is primarly used to check the password is not empty, and to make sure the password confirmation matches the password. If it's the case, the function will return a WP_Error object with no error message. Otherwise this object will contain an error message.

Plugins can now use the bp_members_validate_user_password filter to add their own error messages according to a custom validation process. See the last unit tests of this commit for an example of use.

Props devnik, tharsheblows

Fixes #8066

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-settings/actions/general.php

    r12603 r12679  
    131131        /* Password Change Attempt ***************************************/
    132132
    133         if ( !empty( $_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
    134 
    135             if ( ( $_POST['pass1'] == $_POST['pass2'] ) && !strpos( " " . wp_unslash( $_POST['pass1'] ), "\\" ) ) {
    136 
     133        if ( ! empty( $_POST['pass1'] ) && ! empty( $_POST['pass2'] ) ) {
     134            $pass         = wp_unslash( $_POST['pass1'] );
     135            $pass_confirm = wp_unslash( $_POST['pass2'] );
     136            $pass_error   = bp_members_validate_user_password( $pass, $pass_confirm, $update_user );
     137
     138            if ( ! $pass_error->get_error_message() ) {
    137139                // Password change attempt is successful.
    138                 if ( ( ! empty( $_POST['pwd'] ) && $_POST['pwd'] != $_POST['pass1'] ) || is_super_admin() )  {
     140                if ( ( ! empty( $_POST['pwd'] ) && wp_unslash( $_POST['pwd'] ) !== $pass ) || is_super_admin() )  {
    139141                    $update_user->user_pass = $_POST['pass1'];
    140                     $pass_changed = true;
     142                    $pass_error             = false;
     143                    $pass_changed           = true;
    141144
    142145                // The new password is the same as the current password.
    143146                } else {
    144                     $pass_error = 'same';
     147                    $pass_error->add( 'same_user_password', __( 'The new password must be different from the current password.', 'buddypress' ) );
    145148                }
    146 
    147             // Password change attempt was unsuccessful.
    148             } else {
    149                 $pass_error = 'mismatch';
    150149            }
    151150
     
    155154
    156155        // One of the password boxes was left empty.
    157         } elseif ( ( empty( $_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) || ( !empty( $_POST['pass1'] ) && empty( $_POST['pass2'] ) ) ) {
    158             $pass_error = 'empty';
     156        } elseif ( ( empty( $_POST['pass1'] ) && ! empty( $_POST['pass2'] ) ) || ( ! empty( $_POST['pass1'] ) && empty( $_POST['pass2'] ) ) ) {
     157            $pass_error = new WP_Error( 'empty_user_password', __( 'One of the password fields was empty.', 'buddypress' ) );
    159158        }
    160159
     
    181180    // Password Error.
    182181    } else {
    183         $pass_error = 'invalid';
     182        $pass_error = new WP_Error( 'invalid_user_password', __( 'Your current password is invalid.', 'buddypress' ) );
    184183    }
    185184
     
    203202    }
    204203
    205     // Password feedback.
    206     switch ( $pass_error ) {
    207         case 'invalid' :
    208             $feedback['pass_error']    = __( 'Your current password is invalid.', 'buddypress' );
    209             break;
    210         case 'mismatch' :
    211             $feedback['pass_mismatch'] = __( 'The new password fields did not match.', 'buddypress' );
    212             break;
    213         case 'empty' :
    214             $feedback['pass_empty']    = __( 'One of the password fields was empty.', 'buddypress' );
    215             break;
    216         case 'same' :
    217             $feedback['pass_same']     = __( 'The new password must be different from the current password.', 'buddypress' );
    218             break;
    219         case false :
    220             // No change.
    221             break;
     204    if ( is_wp_error( $pass_error ) && $pass_error->get_error_message() ) {
     205        $feedback[ $pass_error->get_error_code() ] = $pass_error->get_error_message();
    222206    }
    223207
Note: See TracChangeset for help on using the changeset viewer.