In 4.7 the capabilities checked in the REST API when updating a user are wrong.
In multisite, only network administrators can edit other users than themselves. Regular site administrators can only change roles of other users.
In the REST API, network administrators can do these things, however site administrators cannot change user roles.
→ That has not been discussed yet.
Current code
if ( ! current_user_can( 'edit_user', $user->ID ) ) {
return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this user.' ), array( 'status' => rest_authorization_required_code() ) );
}
if ( ! empty( $request['roles'] ) && ! current_user_can( 'edit_users' ) ) {
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() ) );
}
return true;
Possible fix
if ( ! empty( $request['roles'] ) ) {
if ( ! current_user_can( 'promote_user', $user->ID ) ) {
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() ) );
}
$request_params = $request->get_params();
if ( count( $request_params ) === 2 ) {
return true;
}
}
if ( ! current_user_can( 'edit_user', $user->ID ) ) {
return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this user.' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
1.b. Implement missing functionality for wp/v2/users
Current state:
You cannot view or edit users from another site.
You cannot add existing users to a site.
You cannot remove users from a site.
You cannot delete users.
The latter two features were discussed and disabled in Ticket #38962 prior to 4.7.
Challenge: Users are global objects in a Multisite.
Possible solution: Introduce a global parameter.
Read access to users
GET wp/v2/users will list users from the current site.
GET wp/v2/users?global=true will list all users.
Read access to a user
GET wp/v2/users/<id> displays a user from the current site.
GET wp/v2/users/<id>?global=true displays any user.
Edit access to a user
POST/PUT/PATCH wp/v2/users/<id> allows editing a user of the current site.
POST/PUT/PATCH wp/v2/users/<id>?global=true allows editing a user from any site.
Creating and adding a user
POST wp/v2/users creates a new user and adds it to the current site.
POST wp/v2/users?email=<existing-email-address> adds an existing user to the current site.
→ This is not very clear and we might need to find a better solution.
Related problem: Site administrators can create users, but not edit them. ¯\_(ツ)_/¯
Deleting and removing a user
DELETE wp/v2/users/<id> removes a user from the current site.
DELETE wp/v2/users/<id>?global=true deletes a user completely.
2. Introducing a wp/v2/sites endpoint
Implement a set of functions for a real sites API
Figure out how to support queries by certain site data