FIX: prevent users from moving whispers to new topic

This commit is contained in:
Régis Hanol 2018-01-22 17:23:19 +01:00
parent dde0fcc658
commit 5c1eaeca9e
4 changed files with 32 additions and 13 deletions

View File

@ -772,9 +772,12 @@ export default Ember.Controller.extend(BufferedContent, {
return selectedPostsCount > 0 && (selectedAllPosts || selectedPosts.every(p => p.can_delete));
},
@computed('canMergeTopic', 'selectedAllPosts')
canSplitTopic(canMergeTopic, selectedAllPosts) {
return canMergeTopic && !selectedAllPosts;
@computed('canMergeTopic', 'selectedAllPosts', 'selectedPosts', 'selectedPosts.[]')
canSplitTopic(canMergeTopic, selectedAllPosts, selectedPosts) {
return canMergeTopic &&
!selectedAllPosts &&
selectedPosts.length > 0 &&
selectedPosts.sort((a, b) => a.post_number - b.post_number)[0].post_type === 1;
},
@computed('model.details.can_move_posts', 'selectedPostsCount')

View File

@ -393,10 +393,8 @@ class ApplicationController < ActionController::Base
end
def post_ids_including_replies
post_ids = params[:post_ids].map { |p| p.to_i }
if params[:reply_post_ids]
post_ids |= PostReply.where(post_id: params[:reply_post_ids].map { |p| p.to_i }).pluck(:reply_id)
end
post_ids = params[:post_ids].map(&:to_i)
post_ids |= PostReply.where(post_id: params[:reply_post_ids]).pluck(:reply_id) if params[:reply_post_ids]
post_ids
end

View File

@ -521,13 +521,18 @@ class TopicsController < ApplicationController
end
def move_posts
params.require(:post_ids)
params.require(:topic_id)
post_ids = params.require(:post_ids)
topic_id = params.require(:topic_id)
params.permit(:category_id)
topic = Topic.with_deleted.find_by(id: params[:topic_id])
topic = Topic.with_deleted.find_by(id: topic_id)
guardian.ensure_can_move_posts!(topic)
# when creating a new topic, ensure the 1st post is a regular post
if params[:title].present? && Post.where(topic: topic, id: post_ids).order(:post_number).pluck(:post_type).first != Post.types[:regular]
return render_json_error("When moving posts to a new topic, the first post must be a regular post.")
end
dest_topic = move_posts_to_destination(topic)
render_topic_changes(dest_topic)
rescue ActiveRecord::RecordInvalid => ex

View File

@ -71,10 +71,10 @@ describe TopicsController do
describe 'moving to a new topic' do
let(:user) { log_in(:moderator) }
let(:p1) { Fabricate(:post, user: user) }
let(:p1) { Fabricate(:post, user: user, post_number: 1) }
let(:topic) { p1.topic }
it "raises an error without postIds" do
it "raises an error without post_ids" do
expect do
post :move_posts, params: {
topic_id: topic.id, title: 'blah'
@ -92,6 +92,19 @@ describe TopicsController do
expect(response).to be_forbidden
end
it "raises an error when the OP is not a regular post" do
p2 = Fabricate(:post, topic: topic, post_number: 2, post_type: Post.types[:whisper])
p3 = Fabricate(:post, topic: topic, post_number: 3)
post :move_posts, params: {
topic_id: topic.id, title: 'blah', post_ids: [p2.id, p3.id]
}, format: :json
result = ::JSON.parse(response.body)
expect(result['errors']).to_not be_empty
end
context 'success' do
let(:user) { log_in(:admin) }
let(:p2) { Fabricate(:post, user: user, topic: topic) }
@ -142,7 +155,7 @@ describe TopicsController do
end
context 'failure' do
let(:p2) { Fabricate(:post, user: user) }
let(:p2) { Fabricate(:post, topic: topic, user: user) }
before do
Topic.any_instance.expects(:move_posts).with(user, [p2.id], title: 'blah').returns(nil)