Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
FIX: Can't recover a post when its user has been deleted.
  • Loading branch information
tgxworld committed Mar 6, 2017
1 parent b2cfad5 commit a28704b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/guardian/post_guardian.rb
Expand Up @@ -149,7 +149,11 @@ def can_delete_post?(post)

# 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)
Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/topic_guardian.rb
Expand Up @@ -52,7 +52,7 @@ def can_edit_topic?(topic)

# Recovery Method
def can_recover_topic?(topic)
is_staff?
topic && topic.deleted_at && topic.user && is_staff?
end

def can_delete_topic?(topic)
Expand Down
54 changes: 50 additions & 4 deletions spec/components/guardian_spec.rb
Expand Up @@ -880,8 +880,30 @@
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

Expand All @@ -899,8 +921,32 @@
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
Expand Down

0 comments on commit a28704b

Please sign in to comment.