Make WordPress Core

Ticket #27317: 27317.4.diff

File 27317.4.diff, 4.5 KB (added by danielbachhuber, 11 years ago)

Add illegal_user_logins check to more places, and add tests

  • src/wp-includes/user.php

     
    15421542        if ( !$update && username_exists( $user_login ) )
    15431543                return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) );
    15441544
     1545        if ( in_array( $user_login, apply_filters( 'illegal_user_logins', array() ) ) ) {
     1546                return new WP_Error( 'illegal-user-login', __( "Sorry, that username is not allowed." ) );
     1547        }
     1548
    15451549        if ( empty($user_nicename) )
    15461550                $user_nicename = sanitize_title( $user_login );
    15471551
     
    19821986                $sanitized_user_login = '';
    19831987        } elseif ( username_exists( $sanitized_user_login ) ) {
    19841988                $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
     1989        } elseif ( in_array( $sanitized_user_login, apply_filters( 'illegal_user_logins', array() ) ) ) {
     1990                $errors->add( 'illegal_user_login', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
    19851991        }
    19861992
    19871993        // Check the e-mail address
  • src/wp-includes/ms-functions.php

     
    489489        if ( in_array( $user_name, $illegal_names ) == true )
    490490                $errors->add('user_name',  __( 'That username is not allowed.' ) );
    491491
     492        if ( in_array( $user_name, apply_filters( 'illegal_user_logins', array() ) ) ) {
     493                $errors->add('user_name',  __( 'That username is not allowed.' ) );
     494        }
     495
    492496        if ( is_email_address_unsafe( $user_email ) )
    493497                $errors->add('user_email',  __('You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.'));
    494498
  • src/wp-admin/includes/user.php

     
    176176                $user_id = wp_update_user( $user );
    177177        } else {
    178178                $user_id = wp_insert_user( $user );
    179                 wp_new_user_notification( $user_id, isset( $_POST['send_password'] ) ? wp_unslash( $pass1 ) : '' );
     179                if ( ! is_wp_error( $user_id ) ) {
     180                        wp_new_user_notification( $user_id, isset( $_POST['send_password'] ) ? wp_unslash( $pass1 ) : '' );
     181                }
    180182        }
    181183        return $user_id;
    182184}
  • tests/phpunit/tests/user.php

     
    627627                // If this test fails, it will error out for calling the to_array() method on a non-object.
    628628                $this->assertInstanceOf( 'WP_Error', wp_update_user( array( 'ID' => $user_id ) ) );
    629629        }
     630
     631        /**
     632         * @ticket 27317
     633         */
     634        function test_illegal_user_logins_single() {
     635
     636                $user_data = array(
     637                        'user_login'       => 'testuser',
     638                        'user_email'       => 'testuser@example.com',
     639                        'user_pass'        => wp_generate_password(),
     640                        );
     641
     642                add_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     643
     644                $response = wp_insert_user( $user_data );
     645                $this->assertInstanceOf( 'WP_Error', $response );
     646                $this->assertEquals( 'illegal-user-login', $response->get_error_code() );
     647
     648                remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     649
     650                $user_id = wp_insert_user( $user_data );
     651                $user = get_user_by( 'id', $user_id );
     652                $this->assertInstanceOf( 'WP_User', $user );
     653        }
     654
     655        /**
     656         * @ticket 27317
     657         */
     658        function test_illegal_user_logins_multisite() {
     659
     660                if ( ! is_multisite() ) {
     661                        return;
     662                }
     663
     664                $user_data = array(
     665                        'user_login'       => 'testuser',
     666                        'user_email'       => 'testuser@example.com',
     667                        );
     668
     669                add_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     670
     671                $response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
     672                $this->assertInstanceOf( 'WP_Error', $response['errors'] );
     673                $this->assertEquals( 'user_name', $response['errors']->get_error_code() );
     674
     675                remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     676
     677                $response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
     678                $this->assertInstanceOf( 'WP_Error', $response['errors'] );
     679                $this->assertEquals( 0, count( $response['errors']->get_error_codes() ) );
     680        }
     681
     682        function _illegal_user_logins() {
     683                return array( 'testuser' );     
     684        }
    630685}