Skip to content

A platform for community discussion. Free, open, simple.

DEV: Move permission checks into PostMover (#43444)

Alan Guo Xiang Tan2539cce079be
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index c54530dc262..112b6edb5c0 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -987,10 +987,7 @@ class TopicsController < ApplicationController
topic = Topic.find_by(id: topic_id)
guardian.ensure_can_move_posts!(topic)
- destination_topic = Topic.find_by(id: destination_topic_id)
- guardian.ensure_can_create_post_on_topic!(destination_topic)
-
- args = {}
+ args = { guardian: guardian }
args[:destination_topic_id] = destination_topic_id.to_i
args[:chronological_order] = params[:chronological_order] == "true"
args[:freeze_original] = params[:freeze_original] == "true"
@@ -1005,6 +1002,8 @@ class TopicsController < ApplicationController
hijack(info: "merging topic #{topic_id.inspect} into #{destination_topic_id.inspect}") do
destination_topic = topic.move_posts(acting_user, topic.posts.pluck(:id), args)
render_topic_changes(destination_topic)
+ rescue Discourse::InvalidAccess => ex
+ rescue_with_handler(ex)
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => ex
render_json_error(ex)
end
@@ -1040,14 +1039,12 @@ class TopicsController < ApplicationController
end
end
- if params[:destination_topic_id].present?
- destination_topic = Topic.find_by(id: params[:destination_topic_id])
- guardian.ensure_can_create_post_on_topic!(destination_topic)
- end
-
+ request_guardian = guardian
hijack do
- destination_topic = move_posts_to_destination(topic)
+ destination_topic = move_posts_to_destination(topic, guardian: request_guardian)
render_topic_changes(destination_topic)
+ rescue Discourse::InvalidAccess => ex
+ rescue_with_handler(ex)
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => ex
render_json_error(ex)
end
@@ -1620,8 +1617,8 @@ class TopicsController < ApplicationController
end
end
- def move_posts_to_destination(topic)
- args = {}
+ def move_posts_to_destination(topic, guardian:)
+ args = { guardian: guardian }
args[:title] = params[:title] if params[:title].present?
args[:destination_topic_id] = params[:destination_topic_id].to_i if params[
:destination_topic_id
diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb
index e985d4b5f23..2e86aa9214e 100644
--- a/app/models/post_mover.rb
+++ b/app/models/post_mover.rb
@@ -9,10 +9,18 @@ class PostMover
# options:
# freeze_original: :boolean - if true, the original topic will be frozen but not deleted and posts will be "copied" to topic
- def initialize(original_topic, user, post_ids, move_to_pm: false, options: {})
+ def initialize(
+ original_topic,
+ user,
+ post_ids,
+ guardian: user.guardian,
+ move_to_pm: false,
+ options: {}
+ )
@original_topic = original_topic
@original_topic_title = original_topic.title
@user = user
+ @guardian = guardian
@post_ids = post_ids
# For now we store a copy of post_ids. If `freeze_original` is present, we will have new post_ids.
# When we create the new posts, we will pluck out post_ids out of this and replace with updated ids.
@@ -22,11 +30,14 @@ class PostMover
end
def to_topic(id, participants: nil, chronological_order: false)
+ @guardian.ensure_can_move_posts!(original_topic)
+ topic = Topic.find_by_id(id)
+ @guardian.ensure_can_create_post_on_topic!(topic)
+
@move_type = PostMover.move_types[:existing_topic]
@creating_new_topic = false
@chronological_order = chronological_order
- topic = Topic.find_by_id(id)
if topic.archetype != @original_topic.archetype &&
[@original_topic.archetype, topic.archetype].include?(Archetype.private_message)
raise Discourse::InvalidParameters
@@ -39,6 +50,9 @@ class PostMover
end
def to_new_topic(title, category_id = nil, tag_ids: nil, tags: nil)
+ @guardian.ensure_can_move_posts!(original_topic)
+ @guardian.ensure_can_create_topic_on_category!(category_id) if category_id.present?
+
@move_type = PostMover.move_types[:new_topic]
@creating_new_topic = true
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 92f3ebf42d4..7ef0ab9bb85 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -1409,6 +1409,7 @@ class Topic < ActiveRecord::Base
self,
moved_by,
post_ids,
+ guardian: opts.fetch(:guardian) { moved_by.guardian },
move_to_pm: opts[:archetype].present? && opts[:archetype] == "private_message",
options: {
freeze_original: opts[:freeze_original],
diff --git a/plugins/discourse-ai/lib/agents/tools/move_posts.rb b/plugins/discourse-ai/lib/agents/tools/move_posts.rb
index 57eb6d17677..110269d21b4 100644
--- a/plugins/discourse-ai/lib/agents/tools/move_posts.rb
+++ b/plugins/discourse-ai/lib/agents/tools/move_posts.rb
@@ -89,7 +89,8 @@ module DiscourseAi
opts[:category_id] = parameters[:category_id] if parameters[:category_id].present?
end
- destination_topic = topic.move_posts(acting_user, post_ids, opts)
+ destination_topic =
+ topic.move_posts(acting_user, post_ids, opts.merge(guardian: guardian))
if destination_topic.present?
{
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/move_posts_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/move_posts_spec.rb
index 24fcd151a74..4076ceaa635 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/move_posts_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/move_posts_spec.rb
@@ -21,6 +21,29 @@ RSpec.describe DiscourseAi::Agents::Tools::MovePosts do
let(:context) { DiscourseAi::Agents::BotContext.new }
+ describe "#invoke" do
+ fab!(:user, :trust_level_4)
+ let(:context) { DiscourseAi::Agents::BotContext.new(user: user) }
+
+ it "leaves posts unchanged when the user can only reply in the destination category" do
+ category = Fabricate(:category)
+ category.set_permissions(everyone: :reply)
+ category.save!
+
+ expect do
+ expect do
+ tool(
+ topic_id: topic.id,
+ post_ids: [post2.id],
+ new_title: "A separate discussion",
+ category_id: category.id,
+ reason: "Organizing the discussion",
+ ).invoke
+ end.to raise_error(Discourse::InvalidAccess)
+ end.not_to change { [Topic.count, Post.count, post2.reload.topic_id] }
+ end
+ end
+
it "moves posts to an existing topic" do
result =
tool(
diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb
index e0e09f4fefe..039ac3c711b 100644
--- a/spec/models/post_mover_spec.rb
+++ b/spec/models/post_mover_spec.rb
@@ -4,6 +4,85 @@ RSpec.describe PostMover do
fab!(:admin)
fab!(:evil_trout) { Fabricate(:evil_trout, refresh_auto_groups: true) }
+ describe "#to_topic" do
+ fab!(:topic)
+ fab!(:first_post) { Fabricate(:post, topic: topic) }
+ fab!(:reply) { Fabricate(:post, topic: topic) }
+ fab!(:destination_topic, :topic)
+ fab!(:user)
+
+ it "rejects the move when the user cannot move posts from the source topic" do
+ mover = described_class.new(topic, user, [reply.id])
+
+ expect do
+ expect { mover.to_topic(destination_topic.id) }.to raise_error(Discourse::InvalidAccess)
+ end.not_to change { [Post.count, reply.reload.topic_id, topic.reload.closed] }
+ end
+
+ it "rejects the move when the destination category is read-only for the user" do
+ mover_user = Fabricate(:trust_level_4)
+ category = Fabricate(:category)
+ category.set_permissions(everyone: :readonly)
+ category.save!
+ destination_topic.update!(category: category)
+ mover = described_class.new(topic, mover_user, [reply.id])
+
+ expect do
+ expect { mover.to_topic(destination_topic.id) }.to raise_error(Discourse::InvalidAccess)
+ end.not_to change { [Post.count, reply.reload.topic_id, topic.reload.closed] }
+ end
+
+ it "rejects the move when no destination is given" do
+ mover = described_class.new(topic, admin, [reply.id])
+
+ expect { mover.to_topic(nil) }.to raise_error(Discourse::InvalidAccess)
+ expect(reply.reload.topic_id).to eq(topic.id)
+ end
+ end
+
+ describe "#to_new_topic" do
+ fab!(:topic)
+ fab!(:first_post) { Fabricate(:post, topic: topic) }
+ fab!(:reply) { Fabricate(:post, topic: topic) }
+
+ it "does not create a topic when the user cannot move posts from the source topic" do
+ mover = described_class.new(topic, Fabricate(:user), [reply.id])
+
+ expect do
+ expect { mover.to_new_topic("A separate discussion") }.to raise_error(
+ Discourse::InvalidAccess,
+ )
+ end.not_to change { [Topic.count, Post.count, reply.reload.topic_id] }
+ end
+
+ it "does not create a topic when the user can only reply in the destination category" do
+ user = Fabricate(:trust_level_4)
+ category = Fabricate(:category)
+ category.set_permissions(everyone: :reply)
+ category.save!
+ mover = described_class.new(topic, user, [reply.id])
+
+ expect do
+ expect { mover.to_new_topic("A separate discussion", category.id) }.to raise_error(
+ Discourse::InvalidAccess,
+ )
+ end.not_to change { [Topic.count, Post.count, reply.reload.topic_id] }
+ end
+
+ it "allows an admin to split posts on behalf of a user with reply-only access" do
+ user = Fabricate(:trust_level_4)
+ category = Fabricate(:category)
+ category.set_permissions(everyone: :reply)
+ category.save!
+ mover = described_class.new(topic, user, [reply.id], guardian: admin.guardian)
+
+ destination = mover.to_new_topic("A separate discussion", category.id)
+
+ expect(destination.category_id).to eq(category.id)
+ expect(reply.reload.topic_id).to eq(destination.id)
+ end
+ end
+
describe "#move_types" do
context "when verifying enum sequence" do
it "'new_topic' should be at 1st position" do
@@ -2995,7 +3074,7 @@ RSpec.describe PostMover do
fab!(:topic_1, :topic)
fab!(:topic_2, :topic)
fab!(:post_1) { Fabricate(:post, topic: topic_1) }
- fab!(:user)
+ fab!(:user, :trust_level_4)
before { SiteSetting.delete_merged_stub_topics_after_days = 0 }
@@ -3023,7 +3102,7 @@ RSpec.describe PostMover do
end
it "allows specific user to merge topics" do
- special_user = Fabricate(:user)
+ special_user = Fabricate(:trust_level_4)
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(:is_allowed_to_delete_after_merge, &modifier_block)
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 875fa8935cd..17cdc1da106 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -16,6 +16,38 @@ RSpec.describe Topic do
it_behaves_like "it has custom fields"
+ describe "#move_posts" do
+ fab!(:topic)
+ fab!(:first_post) { Fabricate(:post, topic: topic) }
+ fab!(:reply) { Fabricate(:post, topic: topic) }
+ fab!(:destination_topic, :topic)
+
+ it "rejects the move when only the credited user has permission" do
+ expect do
+ expect do
+ topic.move_posts(
+ admin,
+ [reply.id],
+ destination_topic_id: destination_topic.id,
+ guardian: user.guardian,
+ )
+ end.to raise_error(Discourse::InvalidAccess)
+ end.not_to change { [Post.count, reply.reload.topic_id, topic.reload.closed] }
+ end
+
+ it "credits the specified user for a move authorized by an admin" do
+ topic.move_posts(
+ user,
+ [reply.id],
+ destination_topic_id: destination_topic.id,
+ guardian: admin.guardian,
+ )
+
+ expect(reply.reload.topic_id).to eq(destination_topic.id)
+ expect(topic.posts.find_by(action_code: "split_topic").user_id).to eq(user.id)
+ end
+ end
+
describe "Validations" do
let(:topic) { Fabricate.build(:topic) }
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 69ad3eb20b2..63eb93ebf5e 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -342,6 +342,34 @@ RSpec.describe TopicsController do
expect(response).to be_forbidden
end
+ it "does not allow posts to be moved to a private category when a destination topic is supplied" do
+ post "/t/#{topic.id}/move-posts.json",
+ params: {
+ title: "Logan is a good movie",
+ post_ids: [p2.id],
+ category_id: staff_category.id,
+ destination_topic_id: dest_topic.id,
+ }
+
+ expect(response).to be_forbidden
+ expect(response.parsed_body["errors"]).to be_present
+ expect(p2.reload.topic_id).to eq(topic.id)
+ end
+
+ it "does not allow posts to be moved to a private category when an archetype is supplied" do
+ post "/t/#{topic.id}/move-posts.json",
+ params: {
+ title: "Logan is a good movie",
+ post_ids: [p2.id],
+ category_id: staff_category.id,
+ archetype: Archetype.default,
+ }
+
+ expect(response).to be_forbidden
+ expect(response.parsed_body["errors"]).to be_present
+ expect(p2.reload.topic_id).to eq(topic.id)
+ end
+
it "ignores unrelated post IDs without exposing their reviewables" do
restricted_category = Fabricate(:private_category, group: Fabricate(:group))
restricted_topic = Fabricate(:topic, category: restricted_category)
@@ -614,9 +642,8 @@ RSpec.describe TopicsController do
}
expect(response.status).to eq(200)
- result = response.parsed_body
- expect(result["success"]).to eq(true)
- expect(result["url"]).to be_present
+ expect(response.parsed_body).to eq("success" => true, "url" => dest_topic.relative_url)
+ expect(p2.reload.topic_id).to eq(dest_topic.id)
end
it "does not allow posts to be moved to a private category" do
@@ -629,6 +656,9 @@ RSpec.describe TopicsController do
}
expect(response).to be_forbidden
+ expect(response.parsed_body["errors"]).to be_present
+ expect(p2.reload.topic_id).to eq(topic.id)
+ expect(topic.reload.deleted_at).to be_nil
end
it "does not allow posts outside of the category to be moved" do
@@ -934,9 +964,8 @@ RSpec.describe TopicsController do
post "/t/#{topic.id}/merge-topic.json", params: { destination_topic_id: dest_topic.id }
expect(response.status).to eq(200)
- result = response.parsed_body
- expect(result["success"]).to eq(true)
- expect(result["url"]).to be_present
+ expect(response.parsed_body).to eq("success" => true, "url" => dest_topic.relative_url)
+ expect(p2.reload.topic_id).to eq(dest_topic.id)
end
it "does not allow posts to be moved to a private category" do
@@ -945,6 +974,9 @@ RSpec.describe TopicsController do
post "/t/#{topic.id}/merge-topic.json", params: { destination_topic_id: dest_topic.id }
expect(response).to be_forbidden
+ expect(response.parsed_body["errors"]).to be_present
+ expect(p2.reload.topic_id).to eq(topic.id)
+ expect(topic.reload.deleted_at).to be_nil
end
it "does not allow posts outside of the category to be moved" do