Skip to content

Commit

Permalink
Merge pull request #506 from buddypress/feature/bp-view-feature
Browse files Browse the repository at this point in the history
Review the `bp_view` capability support in endpoints
  • Loading branch information
renatonascalves authored May 24, 2024
2 parents c50601d + 06ea906 commit 1c2baf3
Show file tree
Hide file tree
Showing 28 changed files with 691 additions and 236 deletions.
15 changes: 13 additions & 2 deletions includes/bp-activity/classes/class-bp-rest-activity-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ public function get_items( $request ) {
* @return true|WP_Error
*/
public function 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(),
)
);

if ( bp_current_user_can( 'bp_view', array( 'bp_component' => 'activity' ) ) ) {
$retval = true;
}

/**
* Filter the activity `get_items` permissions check.
Expand All @@ -269,7 +280,7 @@ public function get_items_permissions_check( $request ) {
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request Full data about the request.
*/
return apply_filters( 'bp_rest_activity_get_items_permissions_check', true, $request );
return apply_filters( 'bp_rest_activity_get_items_permissions_check', $retval, $request );
}

/**
Expand Down Expand Up @@ -332,7 +343,7 @@ public function get_item_permissions_check( $request ) {
)
);

if ( $this->can_see( $request ) ) {
if ( bp_current_user_can( 'bp_view', array( 'bp_component' => 'activity' ) ) && $this->can_see( $request ) ) {
$retval = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* @since 6.0.0
*/
class BP_REST_Attachments_Blog_Avatar_Endpoint extends WP_REST_Controller {

use BP_REST_Attachments;

/**
Expand Down Expand Up @@ -172,26 +171,24 @@ public function get_item_permissions_check( $request ) {
)
);

$this->blog = $this->blogs_endpoint->get_blog_object( $request->get_param( 'id' ) );

if ( ! is_object( $this->blog ) ) {
$retval = new WP_Error(
'bp_rest_blog_invalid_id',
__( 'Invalid group ID.', 'buddypress' ),
array(
'status' => 404,
)
);
} elseif ( buddypress()->avatar->show_avatars ) {
$retval = true;
} else {
$retval = new WP_Error(
'bp_rest_attachments_blog_avatar_disabled',
__( 'Sorry, blog avatar is disabled.', 'buddypress' ),
array(
'status' => 500,
)
);
if ( bp_current_user_can( 'bp_view', array( 'bp_component' => 'blogs' ) ) ) {
$this->blog = $this->blogs_endpoint->get_blog_object( $request->get_param( 'id' ) );

if ( ! is_object( $this->blog ) ) {
$retval = new WP_Error(
'bp_rest_blog_invalid_id',
__( 'Invalid group ID.', 'buddypress' ),
array( 'status' => 404 )
);
} elseif ( buddypress()->avatar->show_avatars ) {
$retval = true;
} else {
$retval = new WP_Error(
'bp_rest_attachments_blog_avatar_disabled',
__( 'Sorry, blog avatar is disabled.', 'buddypress' ),
array( 'status' => 500 )
);
}
}

/**
Expand Down
26 changes: 24 additions & 2 deletions includes/bp-blogs/classes/class-bp-rest-blogs-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ public function get_items( $request ) {
* @return true|WP_Error
*/
public function 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(),
)
);

if ( bp_current_user_can( 'bp_view', array( 'bp_component' => 'blogs' ) ) ) {
$retval = true;
}

/**
* Filter the blogs `get_items` permissions check.
Expand All @@ -174,7 +185,7 @@ public function get_items_permissions_check( $request ) {
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_blogs_get_items_permissions_check', true, $request );
return apply_filters( 'bp_rest_blogs_get_items_permissions_check', $retval, $request );
}

/**
Expand Down Expand Up @@ -229,6 +240,17 @@ public function get_item( $request ) {
* @return true|WP_Error
*/
public function get_item_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(),
)
);

if ( bp_current_user_can( 'bp_view', array( 'bp_component' => 'blogs' ) ) ) {
$retval = true;
}

/**
* Filter the blog `get_item` permissions check.
Expand All @@ -238,7 +260,7 @@ public function get_item_permissions_check( $request ) {
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_blogs_get_item_permissions_check', true, $request );
return apply_filters( 'bp_rest_blogs_get_item_permissions_check', $retval, $request );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* @since 0.1.0
*/
class BP_REST_Attachments_Group_Avatar_Endpoint extends WP_REST_Controller {

use BP_REST_Attachments;

/**
Expand Down Expand Up @@ -178,13 +177,12 @@ public function get_item_permissions_check( $request ) {
);

if ( bp_current_user_can( 'bp_view', array( 'bp_component' => 'groups' ) ) ) {
$retval = new WP_Error(
$retval = new WP_Error(
'bp_rest_group_invalid_id',
__( 'Invalid group ID.', 'buddypress' ),
array(
'status' => 404,
)
array( 'status' => 404 )
);

$this->group = $this->groups_endpoint->get_group_object( $request );

if ( false !== $this->group ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* @since 6.0.0
*/
class BP_REST_Attachments_Group_Cover_Endpoint extends WP_REST_Controller {

use BP_REST_Attachments;

/**
Expand Down Expand Up @@ -167,9 +166,7 @@ public function get_item_permissions_check( $request ) {
$retval = new WP_Error(
'bp_rest_group_invalid_id',
__( 'Invalid group ID.', 'buddypress' ),
array(
'status' => 404,
)
array( 'status' => 404 )
);

$this->group = $this->groups_endpoint->get_group_object( $request );
Expand Down
Loading

0 comments on commit 1c2baf3

Please sign in to comment.