Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,18 @@ public function update_item( $request ) {
* @return true|WP_Error
*/
public function update_item_permissions_check( $request ) {
$retval = $this->get_items_permissions_check( $request );
$retval = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you are not allowed to perform this action.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);

// Unlike `get_items`, toggling a component is an admin-only operation.
if ( bp_current_user_can( 'manage_options' ) ) {
$retval = true;
}

/**
* Filter the components `update_item` permissions check.
Expand Down
28 changes: 28 additions & 0 deletions tests/testcases/components/test-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,34 @@ public function test_update_item_without_permission() {
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
}

/**
* @group update_item
*/
public function test_update_item_site_admin_only() {
$u = static::factory()->user->create(
array(
'role' => 'author',
)
);

$this->bp::set_current_user( $u );

// Snapshot so we can prove no component was toggled.
$before = bp_get_option( 'bp-active-components' );

$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'friends',
'action' => 'deactivate',
) );
$response = $this->server->dispatch( $request );

$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );

// The component toggle must not have executed.
$this->assertSame( $before, bp_get_option( 'bp-active-components' ) );
}

/**
* @group delete_item
*/
Expand Down
Loading