Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 2751512

Browse files
mikejolleyAljullu
authored andcommitted
Refactor permission checks for authors (#1006)
1 parent 7b82ebd commit 2751512

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/RestApi/Controllers/Products.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,19 @@ public function get_item_permissions_check( $request ) {
105105
}
106106

107107
/**
108-
* Change read permissions to allow author access to this API.
108+
* Change REST API permissions so that authors have access to this API.
109109
*
110-
* @param array $permissions Array of access permissions.
110+
* This code only runs for methods of this class. @see Products::get_items below.
111+
*
112+
* @param bool $permission Does the current user have access to the API.
113+
* @return bool
111114
*/
112-
public function change_permissions( $permissions ) {
113-
$permissions['read'] = 'edit_posts';
114-
return $permissions;
115+
public function force_edit_posts_permission( $permission ) {
116+
// If user has access already, we can bypass additonal checks.
117+
if ( $permission ) {
118+
return $permission;
119+
}
120+
return current_user_can( 'edit_posts' );
115121
}
116122

117123
/**
@@ -121,9 +127,9 @@ public function change_permissions( $permissions ) {
121127
* @return WP_Error|WP_REST_Response
122128
*/
123129
public function get_items( $request ) {
124-
add_filter( 'woocommerce_rest_check_permissions', array( $this, 'change_permissions' ) );
130+
add_filter( 'woocommerce_rest_check_permissions', array( $this, 'force_edit_posts_permission' ) );
125131
$response = parent::get_items( $request );
126-
remove_filter( 'woocommerce_rest_check_permissions', array( $this, 'change_permissions' ) );
132+
remove_filter( 'woocommerce_rest_check_permissions', array( $this, 'force_edit_posts_permission' ) );
127133

128134
return $response;
129135
}

0 commit comments

Comments
 (0)