A platform for community discussion. Free, open, simple.
FIX: Authorize topic recovery against the post it actually recovers (#42892)
Régis Hanol9672c7534b08
diff --git a/app/models/topic.rb b/app/models/topic.rbindex 54ed5496142..b3c3a40f5bd 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -145,6 +145,10 @@ class Topic < ActiveRecord::Base@featured_users ||= TopicFeaturedUsers.new(self)end+ def first_post_with_deleted+ posts.with_deleted.find_by(post_number: 1)+ end+def trash!(trashed_by = nil)trigger_event = falsediff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rbindex 330f6692098..68a19003fa0 100644--- a/lib/guardian/topic_guardian.rb+++ b/lib/guardian/topic_guardian.rb@@ -151,7 +151,10 @@ module TopicGuardianif is_category_group_moderator?(topic.category) || can_delete_all_posts_and_topics?topic.deleted_at?else- can_recover_post?(topic.ordered_posts.first)+ original_post = topic.first_post_with_deleted+ return false if original_post&.trashed? && !can_see_deleted_post?(original_post)++ can_recover_post?(original_post)endenddiff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rbindex efd3e4bd061..58d0aa51953 100644--- a/lib/post_destroyer.rb+++ b/lib/post_destroyer.rb@@ -121,7 +121,9 @@ class PostDestroyeruser_recovered if user_recovery@topic.update_column(:user_id, Discourse::SYSTEM_USER_ID) if [email protected]_id- @topic.recover!(@user) if (staff_recovery || user_recovery) && @post.is_first_post?+ if (staff_recovery || user_recovery) && @post.is_first_post? && @post.deleted_at.nil?+ @topic.recover!(@user)+ end@topic.update_statistics!Topic.publish_stats_to_clients!(@topic.id, :recovered)diff --git a/spec/lib/guardian/topic_guardian_spec.rb b/spec/lib/guardian/topic_guardian_spec.rbindex fb7762de1b9..d476851c272 100644--- a/spec/lib/guardian/topic_guardian_spec.rb+++ b/spec/lib/guardian/topic_guardian_spec.rb@@ -150,6 +150,55 @@ RSpec.describe TopicGuardian doSiteSetting.delete_all_posts_and_topics_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]expect(Guardian.new.can_recover_topic?(Topic.with_deleted.last)).to eq(false)end++ it "returns false when the user only owns a surviving reply and staff trashed the first post" do+ trashed_topic = Fabricate(:topic, category: category, user: user)+ Fabricate(+ :post,+ topic: trashed_topic,+ user: user,+ post_number: 1,+ deleted_at: 1.day.ago,+ deleted_by: admin,+ )+ Fabricate(:post, topic: trashed_topic, user: user, post_number: 2, user_deleted: true)+ trashed_topic.trash!(admin)++ expect(Guardian.new(user).can_recover_topic?(trashed_topic)).to eq(false)+ end++ it "returns true for the author of a topic they deleted themselves" do+ own_topic = Fabricate(:topic, category: category, user: user)+ Fabricate(:post, topic: own_topic, user: user, post_number: 1, user_deleted: true)++ expect(Guardian.new(user).can_recover_topic?(own_topic)).to eq(true)+ end++ it "returns false for tl4 when someone else trashed the first post of a live topic" do+ live_topic = Fabricate(:topic, category: category)+ Fabricate(+ :post,+ topic: live_topic,+ post_number: 1,+ deleted_at: 1.day.ago,+ deleted_by: moderator,+ )++ expect(Guardian.new(tl4_user).can_recover_topic?(live_topic)).to eq(false)+ end++ it "returns true for tl4 when they trashed the first post of a live topic themselves" do+ live_topic = Fabricate(:topic, category: category)+ Fabricate(+ :post,+ topic: live_topic,+ post_number: 1,+ deleted_at: 1.day.ago,+ deleted_by: tl4_user,+ )++ expect(Guardian.new(tl4_user).can_recover_topic?(live_topic)).to eq(true)+ endenddescribe "#can_edit_topic?" dodiff --git a/spec/lib/post_destroyer_spec.rb b/spec/lib/post_destroyer_spec.rbindex f8606100929..f3c0d818078 100644--- a/spec/lib/post_destroyer_spec.rb+++ b/spec/lib/post_destroyer_spec.rb@@ -1633,4 +1633,13 @@ RSpec.describe PostDestroyer doexpect(post.reload.deleted_at).to be_presentexpect(Topic.with_deleted.find(post.topic_id).deleted_at).to be_presentend++ it "leaves the topic deleted when its author recovers a first post staff trashed" do+ PostDestroyer.new(moderator, post).destroy++ PostDestroyer.new(post.user, post.reload).recover++ expect(post.reload.deleted_at).to be_present+ expect(Topic.with_deleted.find(post.topic_id).deleted_at).to be_present+ endenddiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 6c30d35e027..da0639e9d09 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -1992,6 +1992,16 @@ RSpec.describe TopicsController doput "/t/#{topic.id}/recover.json"expect(response).to be_forbiddenend++ it "raises an exception when only the user's own deleted reply survives" do+ Fabricate(:post, topic: topic, user: user, post_number: 2, user_deleted: true)+ sign_in(user)++ put "/t/#{topic.id}/recover.json"++ expect(response).to be_forbidden+ expect(topic.reload).to be_trashed+ endendcontext "with permission" do