FIX: Can't recover a post when its user has been deleted.

https://meta.discourse.org/t/moving-posts-to-new-topic/58436
This commit is contained in:
Guo Xiang Tan 2017-03-06 13:17:57 +08:00
parent b2cfad5f47
commit a28704bcee
3 changed files with 56 additions and 6 deletions

View File

@ -149,7 +149,11 @@ module PostGuardian
# Recovery Method
def can_recover_post?(post)
is_staff? || (is_my_own?(post) && post.user_deleted && !post.deleted_at)
if is_staff?
post.deleted_at && post.user
else
is_my_own?(post) && post.user_deleted && !post.deleted_at
end
end
def can_delete_post_action?(post_action)

View File

@ -52,7 +52,7 @@ module TopicGuardian
# Recovery Method
def can_recover_topic?(topic)
is_staff?
topic && topic.deleted_at && topic.user && is_staff?
end
def can_delete_topic?(topic)

View File

@ -880,8 +880,30 @@ describe Guardian do
expect(Guardian.new(user).can_recover_topic?(topic)).to be_falsey
end
it "returns true for a moderator" do
expect(Guardian.new(moderator).can_recover_topic?(topic)).to be_truthy
context 'as a moderator' do
before do
topic.save!
post.save!
end
describe 'when post has been deleted' do
it "should return the right value" do
expect(Guardian.new(moderator).can_recover_topic?(topic)).to be_falsey
PostDestroyer.new(moderator, topic.first_post).destroy
expect(Guardian.new(moderator).can_recover_topic?(topic.reload)).to be_truthy
end
end
describe "when post's user has been deleted" do
it 'should return the right value' do
PostDestroyer.new(moderator, topic.first_post).destroy
topic.first_post.user.destroy!
expect(Guardian.new(moderator).can_recover_topic?(topic.reload)).to be_falsey
end
end
end
end
@ -899,8 +921,32 @@ describe Guardian do
expect(Guardian.new(user).can_recover_post?(post)).to be_falsey
end
it "returns true for a moderator" do
expect(Guardian.new(moderator).can_recover_post?(post)).to be_truthy
context 'as a moderator' do
let(:other_post) { Fabricate(:post, topic: topic, user: topic.user) }
before do
topic.save!
post.save!
end
describe 'when post has been deleted' do
it "should return the right value" do
expect(Guardian.new(moderator).can_recover_post?(post)).to be_falsey
PostDestroyer.new(moderator, post).destroy
expect(Guardian.new(moderator).can_recover_post?(post.reload)).to be_truthy
end
describe "when post's user has been deleted" do
it 'should return the right value' do
PostDestroyer.new(moderator, post).destroy
post.user.destroy!
expect(Guardian.new(moderator).can_recover_post?(post.reload)).to be_falsey
end
end
end
end
end