Skip to content

Commit e5c87e7

Browse files
committed
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. Merges [60814] into the 6.8 branch. Props andraganescu, desrosj, ehti, hurayraiit, iandunn, joehoyle, johnbillion, jorbin, mnelson4, noisysocks, peterwilsoncc, rmccue, timothyblynjacobs, vortfu, whyisjake, zieladam. git-svn-id: https://develop.svn.wordpress.org/branches/6.8@60817 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9565bce commit e5c87e7

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,13 @@ static function ( $format ) {
463463
}
464464

465465
foreach ( $query_result as $post ) {
466-
if ( ! $this->check_read_permission( $post ) ) {
466+
if ( 'edit' === $request['context'] ) {
467+
$permission = $this->check_update_permission( $post );
468+
} else {
469+
$permission = $this->check_read_permission( $post );
470+
}
471+
472+
if ( ! $permission ) {
467473
continue;
468474
}
469475

src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ public function get_items( $request ) {
365365
if ( ! $is_head_request ) {
366366
$response = array();
367367
foreach ( $query_result as $term ) {
368+
if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', $term->term_id ) ) {
369+
continue;
370+
}
371+
368372
$data = $this->prepare_item_for_response( $term, $request );
369373
$response[] = $this->prepare_response_for_collection( $data );
370374
}

src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function get_items_permissions_check( $request ) {
220220
if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
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
);
226226
}
@@ -379,6 +379,10 @@ static function ( $column ) use ( $search_columns_mapping ) {
379379
$users = array();
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 );
384388
}
@@ -479,13 +483,15 @@ public function get_item_permissions_check( $request ) {
479483
return true;
480484
}
481485

482-
if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
486+
if ( 'edit' === $request['context'] && ! current_user_can( 'edit_user', $user->ID ) ) {
483487
return new WP_Error(
484-
'rest_user_cannot_view',
485-
__( 'Sorry, you are not allowed to list users.' ),
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',
491497
__( 'Sorry, you are not allowed to list users.' ),
@@ -1086,7 +1092,7 @@ public function prepare_item_for_response( $item, $request ) {
10861092
$data['slug'] = $user->user_nicename;
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 );
10921098
}

tests/phpunit/tests/rest-api/rest-users-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ public function test_get_item_published_author_wrong_context() {
13111311
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $author_id ) );
13121312
$request->set_param( 'context', 'edit' );
13131313
$response = rest_get_server()->dispatch( $request );
1314-
$this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 );
1314+
$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
13151315
}
13161316

13171317
/**

0 commit comments

Comments
 (0)