Skip to content

Commit b746da6

Browse files
authored
Merge pull request #524 from dcavins/comp-perm-0.7b
Components: Restrict updates to site admins (0.7 branch).
2 parents 8432518 + 7a3a69b commit b746da6

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

includes/bp-components/classes/class-bp-rest-components-endpoint.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,18 @@ public function update_item( $request ) {
271271
* @return true|WP_Error
272272
*/
273273
public function update_item_permissions_check( $request ) {
274-
$retval = $this->get_items_permissions_check( $request );
274+
$retval = new WP_Error(
275+
'bp_rest_authorization_required',
276+
__( 'Sorry, you are not allowed to perform this action.', 'buddypress' ),
277+
array(
278+
'status' => rest_authorization_required_code(),
279+
)
280+
);
281+
282+
// Unlike `get_items`, toggling a component is an admin-only operation.
283+
if ( bp_current_user_can( 'manage_options' ) ) {
284+
$retval = true;
285+
}
275286

276287
/**
277288
* Filter the components `update_item` permissions check.

tests/testcases/components/test-controller.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,34 @@ public function test_update_item_without_permission() {
341341
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
342342
}
343343

344+
/**
345+
* @group update_item
346+
*/
347+
public function test_update_item_site_admin_only() {
348+
$u = static::factory()->user->create(
349+
array(
350+
'role' => 'author',
351+
)
352+
);
353+
354+
$this->bp::set_current_user( $u );
355+
356+
// Snapshot so we can prove no component was toggled.
357+
$before = bp_get_option( 'bp-active-components' );
358+
359+
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
360+
$request->set_query_params( array(
361+
'name' => 'friends',
362+
'action' => 'deactivate',
363+
) );
364+
$response = $this->server->dispatch( $request );
365+
366+
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
367+
368+
// The component toggle must not have executed.
369+
$this->assertSame( $before, bp_get_option( 'bp-active-components' ) );
370+
}
371+
344372
/**
345373
* @group delete_item
346374
*/

0 commit comments

Comments
 (0)