Make WordPress Core


Ignore:
Timestamp:
08/03/2017 09:58:50 PM (8 years ago)
Author:
flixos90
Message:

REST API: Allow site administrators to edit user roles in multisite.

While site administrators cannot generally edit users in multisite, they have always been able to change the roles of users on their site. In the REST API however, this has not been possible so far. This changeset brings parity with how it is handled in the administration panel: A REST request to edit only a user's roles succeeds correctly, while a REST request to edit any further details of a user fails.

Props jnylen0.
Fixes #40263.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r41225 r41226  
    562562        }
    563563
     564        if ( ! empty( $request['roles'] ) ) {
     565            if ( ! current_user_can( 'promote_user', $user->ID ) ) {
     566                return new WP_Error( 'rest_cannot_edit_roles', __( 'Sorry, you are not allowed to edit roles of this user.' ), array( 'status' => rest_authorization_required_code() ) );
     567            }
     568
     569            $request_params = array_keys( $request->get_params() );
     570            sort( $request_params );
     571            // If only 'id' and 'roles' are specified (we are only trying to
     572            // edit roles), then only the 'promote_user' cap is required.
     573            if ( $request_params === array( 'id', 'roles' ) ) {
     574                return true;
     575            }
     576        }
     577
    564578        if ( ! current_user_can( 'edit_user', $user->ID ) ) {
    565579            return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this user.' ), array( 'status' => rest_authorization_required_code() ) );
    566         }
    567 
    568         if ( ! empty( $request['roles'] ) && ! current_user_can( 'edit_users' ) ) {
    569             return new WP_Error( 'rest_cannot_edit_roles', __( 'Sorry, you are not allowed to edit roles of this user.' ), array( 'status' => rest_authorization_required_code() ) );
    570580        }
    571581
Note: See TracChangeset for help on using the changeset viewer.