Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,6 @@ public function create_item( $request ) {
}
return $error;
}
}

if ( is_multisite() ) {
$user_id = wpmu_create_user( $user->user_login, $user->user_pass, $user->user_email );

if ( ! $user_id ) {
Expand All @@ -637,21 +634,35 @@ public function create_item( $request ) {

$user->ID = $user_id;
$user_id = wp_update_user( wp_slash( (array) $user ) );
} else {
$user_id = wp_insert_user( wp_slash( (array) $user ) );
}

if ( is_wp_error( $user_id ) ) {
return $user_id;
if ( is_wp_error( $user_id ) ) {
if ( in_array( $user_id->get_error_code(), array( 'existing_user_login' ) ) ) {
return new WP_Error(
'rest_existing_user_login',
__( 'Sorry, that username already exists!' ),
array( 'status' => 409 )
);
}

if ( in_array( $user_id->get_error_code(), array( 'existing_user_email' ) ) ) {
return new WP_Error(
'rest_user_existing_user_email',
__( 'Sorry, that email address is already used!",' ),
array( 'status' => 409 )
);
}

return $user_id;
}

if ( is_multisite() ) {
$result = add_user_to_blog( get_site()->id, $user_id, '' );
if ( is_wp_error( $result ) ) {
return $result;
}
} else {
$user_id = wp_insert_user( wp_slash( (array) $user ) );

if ( is_wp_error( $user_id ) ) {
return $user_id;
}
}

$user = get_user_by( 'id', $user_id );
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/rest-api/rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,48 @@ public function test_create_user_invalid_role() {
$this->assertErrorResponse( 'rest_user_invalid_role', $response, 400 );
}

/**
* @ticket 41672
*/
public function test_create_user_with_existing_username_or_email() {
$this->allow_user_to_manage_multisite();
wp_set_current_user( self::$user );

// Create User
$params = array(
'username' => 'testjsonuser',
'password' => 'testjsonpassword',
'email' => 'testjson@example.com',
);

$request = new WP_REST_Request( 'POST', '/wp/v2/users' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );
$response = rest_get_server()->dispatch( $request );
$this->check_add_edit_user_response( $response );

// Make request again, expecting existing_user_login response
$params = array(
'username' => 'testjsonuser',
'password' => 'testjsonpassword',
'email' => 'testjson1@example.com',
);

$request->set_body( wp_json_encode( $params ) );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_existing_user_login', $response, 409 );

// Make request again, expecting existing_user_email response
$params = array(
'username' => 'testjsonuser1',
'password' => 'testjsonpassword',
'email' => 'testjson@example.com',
);
$request->set_body( wp_json_encode( $params ) );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_user_existing_user_email', $response, 409 );
}

public function test_update_item() {
$user_id = self::factory()->user->create(
array(
Expand Down
Loading