FIX: Category topics should not be deletable via review queue

This commit is contained in:
Robin Ward 2019-05-30 16:42:59 -04:00
parent 0f3c3bc309
commit 2e0a40007b
5 changed files with 47 additions and 3 deletions

View File

@ -550,9 +550,9 @@ class Admin::UsersController < Admin::AdminController
if post = Post.where(id: params[:post_id]).first
case params[:post_action]
when 'delete'
PostDestroyer.new(current_user, post).destroy
PostDestroyer.new(current_user, post).destroy if guardian.can_delete_post_or_topic?(post)
when "delete_replies"
PostDestroyer.delete_with_replies(current_user, post)
PostDestroyer.delete_with_replies(current_user, post) if guardian.can_delete_post_or_topic?(post)
when 'edit'
revisor = PostRevisor.new(post)

View File

@ -66,7 +66,7 @@ class ReviewableFlaggedPost < Reviewable
build_action(actions, :ignore, icon: 'external-link-alt')
if guardian.is_staff?
if guardian.can_delete_post_or_topic?(post)
delete = actions.add_bundle("#{id}-delete", icon: "far-trash-alt", label: "reviewables.actions.delete.title")
build_action(actions, :delete_and_ignore, icon: 'external-link-alt', bundle: delete)
if post.reply_count > 0

View File

@ -164,6 +164,10 @@ module PostGuardian
false
end
def can_delete_post_or_topic?(post)
post.is_first_post? ? post.topic && can_delete_topic?(post.topic) : can_delete_post?(post)
end
# Deleting Methods
def can_delete_post?(post)
return false if !can_see_post?(post)

View File

@ -44,6 +44,16 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
expect(actions.has?(:disagree_and_restore)).to eq(false)
end
it "doesn't include deletes for category topics" do
c = Fabricate(:category)
flag = PostActionCreator.spam(user, c.topic.posts.first).reviewable
actions = flag.actions_for(guardian)
expect(actions.has?(:delete_and_ignore)).to eq(false)
expect(actions.has?(:delete_and_ignore_replies)).to eq(false)
expect(actions.has?(:delete_and_agree)).to eq(false)
expect(actions.has?(:delete_and_replies)).to eq(false)
end
it "returns `agree_and_restore` if the post is user deleted" do
post.update(user_deleted: true)
expect(reviewable.actions_for(guardian).has?(:agree_and_restore)).to eq(true)

View File

@ -185,6 +185,36 @@ RSpec.describe Admin::UsersController do
expect(response.status).to eq(200)
end
it "won't delete a category topic" do
c = Fabricate(:category)
cat_post = c.topic.posts.first
put(
"/admin/users/#{user.id}/suspend.json",
params: suspend_params.merge(
post_action: 'delete',
post_id: cat_post.id
)
)
cat_post.reload
expect(cat_post.deleted_at).to be_blank
expect(response.status).to eq(200)
end
it "won't delete a category topic by replies" do
c = Fabricate(:category)
cat_post = c.topic.posts.first
put(
"/admin/users/#{user.id}/suspend.json",
params: suspend_params.merge(
post_action: 'delete_replies',
post_id: cat_post.id
)
)
cat_post.reload
expect(cat_post.deleted_at).to be_blank
expect(response.status).to eq(200)
end
it "can delete an associated post and its replies" do
reply = PostCreator.create(
Fabricate(:user),