-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Restrict note editing capabilities to their authors or comment moderators #11191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -438,7 +438,7 @@ public function get_item_permissions_check( $request ) { | |||||
| } | ||||||
|
|
||||||
| // Re-map edit context capabilities when requesting `note` type. | ||||||
| $edit_cap = 'note' === $comment->comment_type ? array( 'edit_comment', $comment->comment_ID ) : array( 'moderate_comments' ); | ||||||
| $edit_cap = 'note' === $comment->comment_type ? array( 'edit_post', $comment->comment_post_ID ) : array( 'moderate_comments' ); | ||||||
| if ( ! empty( $request['context'] ) && 'edit' === $request['context'] && ! current_user_can( ...$edit_cap ) ) { | ||||||
| return new WP_Error( | ||||||
| 'rest_forbidden_context', | ||||||
|
|
@@ -1920,6 +1920,17 @@ protected function check_read_permission( $comment, $request ) { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| * Notes can be read by any user who can edit the parent post. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor point: I don't think “parent” is right to mention here, since it could be confused with
Suggested change
|
||||||
| * This is separate from the edit_comment capability, which controls | ||||||
| * whether a user can modify or delete the note. | ||||||
| * | ||||||
| * @since 7.0.0 | ||||||
| */ | ||||||
| if ( 'note' === $comment->comment_type && ! empty( $comment->comment_post_ID ) ) { | ||||||
| return current_user_can( 'edit_post', $comment->comment_post_ID ); | ||||||
| } | ||||||
|
|
||||||
| if ( 0 === get_current_user_id() ) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -4134,6 +4134,158 @@ public function test_get_note_with_children_link() { | |||||||
| $this->assertStringContainsString( 'type=note', $children[0]['href'] ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Tests that a contributor cannot update another user's note via the REST API. | ||||||||
| * | ||||||||
| * @ticket 64779 | ||||||||
| */ | ||||||||
| public function test_contributor_cannot_update_others_note() { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| $post_id = self::factory()->post->create( | ||||||||
| array( | ||||||||
| 'post_author' => self::$contributor_id, | ||||||||
| 'post_status' => 'draft', | ||||||||
| ) | ||||||||
| ); | ||||||||
| $note_id = self::factory()->comment->create( | ||||||||
| array( | ||||||||
| 'comment_post_ID' => $post_id, | ||||||||
| 'comment_type' => 'note', | ||||||||
| 'user_id' => self::$admin_id, | ||||||||
| 'comment_content' => 'Admin note', | ||||||||
| ) | ||||||||
| ); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| wp_set_current_user( self::$contributor_id ); | ||||||||
|
|
||||||||
| $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); | ||||||||
| $request->set_param( 'content', 'Modified by contributor' ); | ||||||||
| $response = rest_get_server()->dispatch( $request ); | ||||||||
|
|
||||||||
| $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Tests that a contributor cannot delete another user's note via the REST API. | ||||||||
| * | ||||||||
| * @ticket 64779 | ||||||||
| */ | ||||||||
| public function test_contributor_cannot_delete_others_note() { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| $post_id = self::factory()->post->create( | ||||||||
| array( | ||||||||
| 'post_author' => self::$contributor_id, | ||||||||
| 'post_status' => 'draft', | ||||||||
| ) | ||||||||
| ); | ||||||||
| $note_id = self::factory()->comment->create( | ||||||||
| array( | ||||||||
| 'comment_post_ID' => $post_id, | ||||||||
| 'comment_type' => 'note', | ||||||||
| 'user_id' => self::$admin_id, | ||||||||
| 'comment_content' => 'Admin note', | ||||||||
| ) | ||||||||
| ); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| wp_set_current_user( self::$contributor_id ); | ||||||||
|
|
||||||||
| $request = new WP_REST_Request( 'DELETE', '/wp/v2/comments/' . $note_id ); | ||||||||
| $request->set_param( 'force', true ); | ||||||||
| $response = rest_get_server()->dispatch( $request ); | ||||||||
|
|
||||||||
| $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Tests that a note author can update their own note via the REST API. | ||||||||
| * | ||||||||
| * @ticket 64779 | ||||||||
| */ | ||||||||
| public function test_note_author_can_update_own_note() { | ||||||||
| $post_id = self::factory()->post->create( | ||||||||
| array( | ||||||||
| 'post_author' => self::$contributor_id, | ||||||||
| 'post_status' => 'draft', | ||||||||
| ) | ||||||||
| ); | ||||||||
| $note_id = self::factory()->comment->create( | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| array( | ||||||||
| 'comment_post_ID' => $post_id, | ||||||||
| 'comment_type' => 'note', | ||||||||
| 'user_id' => self::$contributor_id, | ||||||||
| 'comment_content' => 'Original content', | ||||||||
| ) | ||||||||
| ); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| wp_set_current_user( self::$contributor_id ); | ||||||||
|
|
||||||||
| $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| $request->set_param( 'content', 'Updated content' ); | ||||||||
| $response = rest_get_server()->dispatch( $request ); | ||||||||
|
|
||||||||
| $this->assertSame( 200, $response->get_status() ); | ||||||||
| $this->assertSame( 'Updated content', get_comment( $note_id )->comment_content ); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Tests that an editor can update another user's note via the REST API. | ||||||||
| * | ||||||||
| * @ticket 64779 | ||||||||
| */ | ||||||||
| public function test_editor_can_update_others_note() { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| $post_id = self::factory()->post->create( | ||||||||
| array( | ||||||||
| 'post_author' => self::$contributor_id, | ||||||||
| 'post_status' => 'draft', | ||||||||
| ) | ||||||||
| ); | ||||||||
| $note_id = self::factory()->comment->create( | ||||||||
| array( | ||||||||
| 'comment_post_ID' => $post_id, | ||||||||
| 'comment_type' => 'note', | ||||||||
| 'user_id' => self::$contributor_id, | ||||||||
| 'comment_content' => 'Contributor note', | ||||||||
| ) | ||||||||
| ); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just helps with static analysis, since
Suggested change
|
||||||||
|
|
||||||||
| wp_set_current_user( self::$editor_id ); | ||||||||
|
|
||||||||
| $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); | ||||||||
| $request->set_param( 'content', 'Edited by editor' ); | ||||||||
| $response = rest_get_server()->dispatch( $request ); | ||||||||
|
|
||||||||
| $this->assertSame( 200, $response->get_status() ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Tests that a contributor can still read notes on their own post. | ||||||||
| * | ||||||||
| * @ticket 64779 | ||||||||
| */ | ||||||||
| public function test_contributor_can_read_others_notes_on_own_post() { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| $post_id = self::factory()->post->create( | ||||||||
| array( | ||||||||
| 'post_author' => self::$contributor_id, | ||||||||
| 'post_status' => 'draft', | ||||||||
| ) | ||||||||
| ); | ||||||||
| $note_id = self::factory()->comment->create( | ||||||||
| array( | ||||||||
| 'comment_post_ID' => $post_id, | ||||||||
| 'comment_type' => 'note', | ||||||||
| 'user_id' => self::$admin_id, | ||||||||
| 'comment_content' => 'Admin feedback', | ||||||||
| ) | ||||||||
| ); | ||||||||
|
|
||||||||
| wp_set_current_user( self::$contributor_id ); | ||||||||
|
|
||||||||
| $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $note_id ); | ||||||||
| $request->set_param( 'context', 'edit' ); | ||||||||
| $response = rest_get_server()->dispatch( $request ); | ||||||||
|
|
||||||||
| $this->assertSame( 200, $response->get_status() ); | ||||||||
| $this->assertSame( $note_id, $response->get_data()['id'] ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Test retrieving comments by type as authenticated user. | ||||||||
| * | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this isn't a dockblock, a
@sincetag I don't think is warranted. I don't see other examples of this in the function.