Skip to content

A platform for community discussion. Free, open, simple.

FIX: Restrict Post Voting comment edits when silenced, or topic closed/archived (#45039)

Isaac Janzen5fa2ad99946c
diff --git a/plugins/discourse-post-voting/lib/post_voting/guardian_extension.rb b/plugins/discourse-post-voting/lib/post_voting/guardian_extension.rb
index 4dc82a8e7fe..cbc60bd19aa 100644
--- a/plugins/discourse-post-voting/lib/post_voting/guardian_extension.rb
+++ b/plugins/discourse-post-voting/lib/post_voting/guardian_extension.rb
@@ -21,14 +21,15 @@ module PostVoting
def can_edit_comment?(comment)
return false if !user
- return true if comment.user_id == user.id
- return true if is_admin?
- return true if is_moderator?
- false
+ return true if is_admin? || is_moderator?
+ return false if comment.user_id != user.id
+ can_create_post_on_topic?(comment.post.topic)
end
def can_delete_comment?(comment)
- can_edit_comment?(comment)
+ return false if !user
+ return true if is_admin? || is_moderator?
+ comment.user_id == user.id
end
def can_flag_post_voting_comments?
diff --git a/plugins/discourse-post-voting/spec/requests/post_voting/comments_controller_spec.rb b/plugins/discourse-post-voting/spec/requests/post_voting/comments_controller_spec.rb
index 4aa221f036e..4607ec8ab12 100644
--- a/plugins/discourse-post-voting/spec/requests/post_voting/comments_controller_spec.rb
+++ b/plugins/discourse-post-voting/spec/requests/post_voting/comments_controller_spec.rb
@@ -270,6 +270,62 @@ RSpec.describe PostVoting::CommentsController do
expect(body["cooked"]).to eq("<p>this is some new raw</p>")
end
+ it "returns 403 when the author is silenced" do
+ UserSilencer.silence(comment.user, Discourse.system_user)
+ sign_in(comment.user)
+
+ put "/post_voting/comments.json",
+ params: {
+ comment_id: comment.id,
+ raw: "this is some new raw",
+ }
+
+ expect(response.status).to eq(403)
+ expect(comment.reload.raw).not_to eq("this is some new raw")
+ end
+
+ it "returns 403 when the author edits a comment on a closed topic" do
+ topic.update!(closed: true)
+ sign_in(comment.user)
+
+ put "/post_voting/comments.json",
+ params: {
+ comment_id: comment.id,
+ raw: "this is some new raw",
+ }
+
+ expect(response.status).to eq(403)
+ expect(comment.reload.raw).not_to eq("this is some new raw")
+ end
+
+ it "returns 403 when the author edits a comment on an archived topic" do
+ topic.update!(archived: true)
+ sign_in(comment.user)
+
+ put "/post_voting/comments.json",
+ params: {
+ comment_id: comment.id,
+ raw: "this is some new raw",
+ }
+
+ expect(response.status).to eq(403)
+ expect(comment.reload.raw).not_to eq("this is some new raw")
+ end
+
+ it "still allows staff to edit a comment on a closed topic" do
+ topic.update!(closed: true)
+ sign_in(admin)
+
+ put "/post_voting/comments.json",
+ params: {
+ comment_id: comment.id,
+ raw: "this is some new raw",
+ }
+
+ expect(response.status).to eq(200)
+ expect(comment.reload.raw).to eq("this is some new raw")
+ end
+
it "updates the user's own comment and publishes the change" do
sign_in(comment.user)