Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 64 additions & 52 deletions src/wp-includes/rest-api/endpoints/class-wp-rest-sites-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,12 @@ public function register_routes() {
* @return WP_Error|bool True if the request has read access, error object otherwise.
*/
public function get_items_permissions_check( $request ) {

if ( 0 === get_current_user_id() ) {
return false;
}

if ( ! is_multisite() ) {
return new WP_Error( 'rest_multisite_not_installed', __( 'Multisite is not installed' ), array( 'status' => 400 ) );
$multisite_support = $this->check_multisite_support();
if ( is_wp_error( $multisite_support ) ) {
return $multisite_support;
}

if ( current_user_can( 'manage_sites' ) ) {
if ( $this->check_edit_permission() ) {
return true;
}

Expand All @@ -136,7 +132,7 @@ public function get_items_permissions_check( $request ) {
return true;
}

return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit sites.' ), array( 'status' => rest_authorization_required_code() ) );
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view sites.' ), array( 'status' => rest_authorization_required_code() ) );
}

/**
Expand All @@ -155,10 +151,10 @@ protected function is_own_user_filter( $request ) {
}

if ( 'me' === $user ) {
return true;
return is_user_logged_in();
}

return get_current_user_id() === (int) $user;
return is_user_logged_in() && get_current_user_id() === (int) $user;
}

/**
Expand Down Expand Up @@ -395,20 +391,23 @@ public function get_user_site_ids( $user_id ) {
* @return WP_Error|bool True if the request has read access for the item, error object otherwise.
*/
public function get_item_permissions_check( $request ) {
$multisite_support = $this->check_multisite_support();
if ( is_wp_error( $multisite_support ) ) {
return $multisite_support;
}

$site = $this->get_site( $request['id'] );
if ( is_wp_error( $site ) ) {
return $site;
}

if ( 0 === get_current_user_id() ) {
return false;
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';

if ( ! is_multisite() ) {
return new WP_Error( 'rest_multisite_not_installed', __( 'Multisite is not installed' ), array( 'status' => 400 ) );
if ( in_array( $context, array( 'view', 'embed' ), true ) && is_user_member_of_blog( get_current_user_id(), (int) $site->blog_id ) ) {
return true;
}

if ( ! empty( $request['context'] ) && 'edit' === $request['context'] && ! current_user_can( 'manage_sites' ) ) {
if ( ! $this->check_edit_permission() ) {
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view sites.' ), array( 'status' => rest_authorization_required_code() ) );
}

Expand Down Expand Up @@ -444,15 +443,16 @@ public function get_item( $request ) {
* @return WP_Error|bool True if the request has access to create items, error object otherwise.
*/
public function create_item_permissions_check( $request ) {
if ( 0 === get_current_user_id() ) {
return false;
$multisite_support = $this->check_multisite_support();
if ( is_wp_error( $multisite_support ) ) {
return $multisite_support;
}

if ( ! is_multisite() ) {
return new WP_Error( 'rest_multisite_not_installed', __( 'Multisite is not installed' ), array( 'status' => 400 ) );
if ( ! current_user_can( 'create_sites' ) ) {
return new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to create sites.' ), array( 'status' => rest_authorization_required_code() ) );
}

return current_user_can( 'create_sites' );
return true;
}

/**
Expand Down Expand Up @@ -542,8 +542,7 @@ public function create_item( $request ) {
return $fields_update;
}

$context = current_user_can( 'manage_sites' ) ? 'edit' : 'view';

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$request->set_param( 'context', $context );

$response = $this->prepare_item_for_response( $site, $request );
Expand All @@ -564,20 +563,17 @@ public function create_item( $request ) {
* @return WP_Error|bool True if the request has access to update the item, error object otherwise.
*/
public function update_item_permissions_check( $request ) {
$multisite_support = $this->check_multisite_support();
if ( is_wp_error( $multisite_support ) ) {
return $multisite_support;
}

$site = $this->get_site( $request['id'] );
if ( is_wp_error( $site ) ) {
return $site;
}

if ( 0 === get_current_user_id() ) {
return false;
}

if ( ! is_multisite() ) {
return new WP_Error( 'rest_multisite_not_installed', __( 'Multisite is not installed' ), array( 'status' => 400 ) );
}

if ( ! $this->check_edit_permission( $site ) ) {
if ( ! $this->check_edit_permission() ) {
return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this site.' ), array( 'status' => rest_authorization_required_code() ) );
}

Expand Down Expand Up @@ -645,7 +641,8 @@ public function update_item( $request ) {
return $fields_update;
}

$request->set_param( 'context', 'edit' );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$request->set_param( 'context', $context );

$response = $this->prepare_item_for_response( $site, $request );

Expand All @@ -661,20 +658,17 @@ public function update_item( $request ) {
* @return WP_Error|bool True if the request has access to delete the item, error object otherwise.
*/
public function delete_item_permissions_check( $request ) {
$multisite_support = $this->check_multisite_support();
if ( is_wp_error( $multisite_support ) ) {
return $multisite_support;
}

$site = $this->get_site( $request['id'] );
if ( is_wp_error( $site ) ) {
return $site;
}

if ( 0 === (int) get_current_user_id() ) {
return false;
}

if ( ! is_multisite() ) {
return new WP_Error( 'rest_multisite_not_installed', __( 'Multisite is not installed' ), array( 'status' => 400 ) );
}

if ( ! current_user_can( 'delete_site', $request['id'] ) ) {
if ( ! $this->check_delete_permission( $site ) ) {
return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete this site.' ), array( 'status' => rest_authorization_required_code() ) );
}

Expand Down Expand Up @@ -730,7 +724,7 @@ public function delete_item( $request ) {
}
}

update_blog_status( $blog_id, 'deleted', 1 );
update_blog_status( $blog_id, 'deleted', '1' );

/** This action is documented in wp-includes/ms-site.php */
do_action_deprecated( 'deleted_blog', array( $blog_id, false ), '5.1.0' );
Expand Down Expand Up @@ -1546,22 +1540,40 @@ public function get_collection_params() {
}

/**
* Checks if a site can be edited or deleted.
* Checks if the current user can edit sites.
*
* @since 7.2.0
*
* @return bool Whether the current user can edit sites.
*/
protected function check_edit_permission() {
return current_user_can( 'manage_sites' );
}

/**
* Checks if a site can be deleted.
*
* @since 7.2.0
*
* @param object $site Site object.
* @return bool Whether the site can be edited or deleted.
* @return bool Whether the site can be deleted.
*/
protected function check_edit_permission( $site ) {
if ( 0 === (int) get_current_user_id() ) {
return false;
}
protected function check_delete_permission( $site ) {
return current_user_can( 'delete_sites' ) && current_user_can( 'delete_site', $site->blog_id );
}

/**
* Checks that multisite is enabled, as this controller requires it.
*
* @since 7.2.0
*
* @return true|WP_Error True if multisite is enabled, WP_Error otherwise.
*/
protected function check_multisite_support() {
if ( ! is_multisite() ) {
return false;
return new WP_Error( 'rest_multisite_not_installed', __( 'Multisite is not installed' ), array( 'status' => 400 ) );
}

return current_user_can( 'manage_sites' );
return true;
}
}
Loading