Make WordPress Core

Ticket #27317: 27317.6.patch

File 27317.6.patch, 4.5 KB (added by chriscct7, 9 years ago)
  • src/wp-admin/includes/user.php

     
    142142        if ( !$update && username_exists( $user->user_login ) )
    143143                $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
    144144
     145        $usernames = array();
     146        /**
     147         * Restrict allowed usernames
     148         *
     149         * @since 4.4.0
     150         *
     151         * @param array $usernames Array of blacklisted usernames
     152         */
     153        $usernames = apply_filters( 'illegal_user_logins', $usernames );
     154        if ( in_array( $user->user_login, $usernames ) ) {
     155                $errors->add( 'illegal_user_login', __( "Sorry, that username is not allowed." ) );
     156        }
     157
    145158        /* checking email address */
    146159        if ( empty( $user->user_email ) ) {
    147160                $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) );
  • src/wp-includes/ms-functions.php

     
    430430        if ( in_array( $user_name, $illegal_names ) )
    431431                $errors->add('user_name',  __( 'That username is not allowed.' ) );
    432432
     433        /** This filter is documented in wp-admin/includes/user.php */
     434        if ( in_array( $user_name, apply_filters( 'illegal_user_logins', array() ) ) ) {
     435                $errors->add('user_name',  __( 'That username is not allowed.' ) );
     436        }
     437
    433438        if ( is_email_address_unsafe( $user_email ) )
    434439                $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.'));
    435440
  • src/wp-includes/user-functions.php

     
    13151315                return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) );
    13161316        }
    13171317
     1318        /** This filter is documented in wp-admin/includes/user.php */
     1319        if ( in_array( $user_login, apply_filters( 'illegal_user_logins', array() ) ) ) {
     1320                $return new WP_Error( 'illegal_user_login', __( "Sorry, that username is not allowed." ) );
     1321        }       
     1322
    13181323        /*
    13191324         * If a nicename is provided, remove unsafe user characters before using it.
    13201325         * Otherwise build a nicename from the user_login.
  • tests/phpunit/tests/user.php

     
    596596                        if ( ! defined( 'WP_IMPORTING' ) ) {
    597597                                $this->assertWPError( $return );
    598598                        }
     599        }
     600
     601        /**
     602         * @ticket 27317
     603         */
     604        function test_illegal_user_logins_single() {
     605
     606                $user_data = array(
     607                        'user_login'       => 'testuser',
     608                        'user_email'       => 'testuser@example.com',
     609                        'user_pass'        => wp_generate_password(),
     610                        );
     611
     612                add_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     613
     614                $response = wp_insert_user( $user_data );
     615                $this->assertInstanceOf( 'WP_Error', $response );
     616                $this->assertEquals( 'illegal_user_login', $response->get_error_code() );
     617
     618                remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     619
     620                $user_id = wp_insert_user( $user_data );
     621                $user = get_user_by( 'id', $user_id );
     622                $this->assertInstanceOf( 'WP_User', $user );
     623        }
     624
     625        /**
     626         * @ticket 27317
     627         */
     628        function test_illegal_user_logins_multisite() {
     629
     630                if ( ! is_multisite() ) {
     631                        return;
    599632                }
     633
     634                $user_data = array(
     635                        'user_login'       => 'testuser',
     636                        'user_email'       => 'testuser@example.com',
     637                        );
     638
     639                add_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     640
     641                $response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
     642                $this->assertInstanceOf( 'WP_Error', $response['errors'] );
     643                $this->assertEquals( 'user_name', $response['errors']->get_error_code() );
     644
     645                remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     646
     647                $response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
     648                $this->assertInstanceOf( 'WP_Error', $response['errors'] );
     649                $this->assertEquals( 0, count( $response['errors']->get_error_codes() ) );
    600650        }
    601651
     652        function _illegal_user_logins() {
     653                return array( 'testuser' );     
     654        }
     655
    602656        /**
    603657         * @ticket 24618
    604658         */