Make WordPress Core


Ignore:
Timestamp:
09/30/2025 03:49:18 PM (11 months ago)
Author:
johnbillion
Message:

REST API: Increase the specificity of capability checks for collections when the edit context is in use.

The edit access in now taken into account for each individual post, term, or user in the response.

Props andraganescu, desrosj, ehti, hurayraiit, iandunn, joehoyle, johnbillion, jorbin, mnelson4, noisysocks, peterwilsoncc, rmccue, timothyblynjacobs, vortfu, whyisjake, zieladam.

File:
1 edited

Legend:

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

    r59970 r60814  
    221221                        return new WP_Error(
    222222                                'rest_forbidden_context',
    223                                 __( 'Sorry, you are not allowed to list users.' ),
     223                                __( 'Sorry, you are not allowed to edit users.' ),
    224224                                array( 'status' => rest_authorization_required_code() )
    225225                        );
     
    380380
    381381                        foreach ( $query->get_results() as $user ) {
     382                                if ( 'edit' === $request['context'] && ! current_user_can( 'edit_user', $user->ID ) ) {
     383                                        continue;
     384                                }
     385
    382386                                $data    = $this->prepare_item_for_response( $user, $request );
    383387                                $users[] = $this->prepare_response_for_collection( $data );
     
    480484                }
    481485
    482                 if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
    483                         return new WP_Error(
    484                                 'rest_user_cannot_view',
    485                                 __( 'Sorry, you are not allowed to list users.' ),
     486                if ( 'edit' === $request['context'] && ! current_user_can( 'edit_user', $user->ID ) ) {
     487                        return new WP_Error(
     488                                'rest_forbidden_context',
     489                                __( 'Sorry, you are not allowed to edit this user.' ),
    486490                                array( 'status' => rest_authorization_required_code() )
    487491                        );
    488                 } elseif ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) {
     492                }
     493
     494                if ( ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) && ! count_user_posts( $user->ID, $types ) ) {
    489495                        return new WP_Error(
    490496                                'rest_user_cannot_view',
     
    10871093                }
    10881094
    1089                 if ( in_array( 'roles', $fields, true ) ) {
     1095                if ( in_array( 'roles', $fields, true ) && ( current_user_can( 'list_users' ) || current_user_can( 'edit_user', $user->ID ) ) ) {
    10901096                        // Defensively call array_values() to ensure an array is returned.
    10911097                        $data['roles'] = array_values( $user->roles );
Note: See TracChangeset for help on using the changeset viewer.