REST API: Synchronize permission checks in ::get_items_permissions_check()
methods for post types, post statuses, and users:
* Only query post types with `'show_in_rest' => true` instead of looping over all post types and checking the `show_in_rest` property separately. * Return from the `foreach()` loop as soon as the permission check succeeded. Props pbiron, TimothyBlynJacobs, SergeyBiryukov. Fixes #49118. git-svn-id: https://develop.svn.wordpress.org/trunk@47034 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
adb9483563
commit
c27ab74ca9
@ -89,6 +89,7 @@ class WP_REST_Post_Statuses_Controller extends WP_REST_Controller {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage post statuses.' ), array( 'status' => rest_authorization_required_code() ) );
|
return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage post statuses.' ), array( 'status' => rest_authorization_required_code() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,8 +81,10 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
|||||||
*/
|
*/
|
||||||
public function get_items_permissions_check( $request ) {
|
public function get_items_permissions_check( $request ) {
|
||||||
if ( 'edit' === $request['context'] ) {
|
if ( 'edit' === $request['context'] ) {
|
||||||
foreach ( get_post_types( array(), 'object' ) as $post_type ) {
|
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
|
||||||
if ( ! empty( $post_type->show_in_rest ) && current_user_can( $post_type->cap->edit_posts ) ) {
|
|
||||||
|
foreach ( $types as $type ) {
|
||||||
|
if ( current_user_can( $type->cap->edit_posts ) ) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,15 +104,16 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
|||||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||||
*/
|
*/
|
||||||
public function get_items( $request ) {
|
public function get_items( $request ) {
|
||||||
$data = array();
|
$data = array();
|
||||||
|
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
|
||||||
|
|
||||||
foreach ( get_post_types( array(), 'object' ) as $obj ) {
|
foreach ( $types as $type ) {
|
||||||
if ( empty( $obj->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $obj->cap->edit_posts ) ) ) {
|
if ( 'edit' === $request['context'] && ! current_user_can( $type->cap->edit_posts ) ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$post_type = $this->prepare_item_for_response( $obj, $request );
|
$post_type = $this->prepare_item_for_response( $type, $request );
|
||||||
$data[ $obj->name ] = $this->prepare_response_for_collection( $post_type );
|
$data[ $type->name ] = $this->prepare_response_for_collection( $post_type );
|
||||||
}
|
}
|
||||||
|
|
||||||
return rest_ensure_response( $data );
|
return rest_ensure_response( $data );
|
||||||
|
@ -199,17 +199,16 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( 'authors' === $request['who'] ) {
|
if ( 'authors' === $request['who'] ) {
|
||||||
$can_view = false;
|
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
|
||||||
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
|
|
||||||
foreach ( $types as $type ) {
|
foreach ( $types as $type ) {
|
||||||
if ( post_type_supports( $type->name, 'author' )
|
if ( post_type_supports( $type->name, 'author' )
|
||||||
&& current_user_can( $type->cap->edit_posts ) ) {
|
&& current_user_can( $type->cap->edit_posts ) ) {
|
||||||
$can_view = true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( ! $can_view ) {
|
|
||||||
return new WP_Error( 'rest_forbidden_who', __( 'Sorry, you are not allowed to query users by this parameter.' ), array( 'status' => rest_authorization_required_code() ) );
|
return new WP_Error( 'rest_forbidden_who', __( 'Sorry, you are not allowed to query users by this parameter.' ), array( 'status' => rest_authorization_required_code() ) );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user