Skip to content

Commit a1f89f4

Browse files
committed
Use 'invalid_username' error code when tripping 'illegal_user_logins'.
This gives us better compatibility with existing errors thrown by `sanitize_user()`, especially in Multisite, where user_login has more restrictions on allowed characters. Props markjaquith. Fixes #27317. git-svn-id: https://develop.svn.wordpress.org/trunk@35772 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6825c72 commit a1f89f4

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/wp-admin/includes/user.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function edit_user( $user_id = 0 ) {
146146
$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
147147

148148
if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ) ) ) {
149-
$errors->add( 'illegal_user_login', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
149+
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
150150
}
151151

152152
/* checking email address */

src/wp-includes/user.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ function wp_insert_user( $userdata ) {
13311331
$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
13321332

13331333
if ( in_array( strtolower( $user_login ), array_map( 'strtolower', $illegal_logins ) ) ) {
1334-
return new WP_Error( 'illegal_user_login', __( 'Sorry, that username is not allowed.' ) );
1334+
return new WP_Error( 'invalid_username', __( 'Sorry, that username is not allowed.' ) );
13351335
}
13361336

13371337
/*
@@ -2124,6 +2124,13 @@ function register_new_user( $user_login, $user_email ) {
21242124
$sanitized_user_login = '';
21252125
} elseif ( username_exists( $sanitized_user_login ) ) {
21262126
$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
2127+
2128+
} else {
2129+
/** This filter is documented in wp-includes/user.php */
2130+
$illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) );
2131+
if ( in_array( strtolower( $sanitized_user_login ), $illegal_user_logins ) ) {
2132+
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
2133+
}
21272134
}
21282135

21292136
// Check the email address

tests/phpunit/tests/user.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ function test_illegal_user_logins_single( $user_login ) {
622622

623623
$response = wp_insert_user( $user_data );
624624
$this->assertInstanceOf( 'WP_Error', $response );
625-
$this->assertEquals( 'illegal_user_login', $response->get_error_code() );
625+
$this->assertEquals( 'invalid_username', $response->get_error_code() );
626626

627627
remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
628628

@@ -631,6 +631,26 @@ function test_illegal_user_logins_single( $user_login ) {
631631
$this->assertInstanceOf( 'WP_User', $user );
632632
}
633633

634+
/**
635+
* @ticket 27317
636+
* @dataProvider _illegal_user_logins_data
637+
*/
638+
function test_illegal_user_logins_single_wp_create_user( $user_login ) {
639+
$user_email = 'testuser-' . $user_login . '@example.com';
640+
641+
add_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
642+
643+
$response = register_new_user( $user_login, $user_email );
644+
$this->assertInstanceOf( 'WP_Error', $response );
645+
$this->assertEquals( 'invalid_username', $response->get_error_code() );
646+
647+
remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
648+
649+
$response = register_new_user( $user_login, $user_email );
650+
$user = get_user_by( 'id', $response );
651+
$this->assertInstanceOf( 'WP_User', $user );
652+
}
653+
634654
/**
635655
* @ticket 27317
636656
*/
@@ -658,10 +678,15 @@ function test_illegal_user_logins_multisite() {
658678
}
659679

660680
function _illegal_user_logins_data() {
661-
return array(
662-
array( 'testuser' ),
663-
array( 'TestUser' ),
681+
$data = array(
682+
array( 'testuser' )
664683
);
684+
685+
// Multisite doesn't allow mixed case logins ever
686+
if ( ! is_multisite() ) {
687+
$data[] = array( 'TestUser' );
688+
}
689+
return $data;
665690
}
666691

667692
function _illegal_user_logins() {

0 commit comments

Comments
 (0)