Skip to content

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

FEATURE: Add Bypass permission checks to create post (#41279)

Same33e71003181
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 3008ec3a8e9..e909fec5fc0 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -35,6 +35,7 @@ class PostCreator
# call `enqueue_jobs` after the transaction is committed.
# hidden_reason_id - Reason for hiding the post (optional)
# skip_validations - Do not validate any of the content in the post
+ # skip_staff_author_pm_membership_sync - Do not add the post author to private message allowed users when a staff guardian authorizes the post
# draft_key - the key of the draft we are creating (will be deleted on success)
# advance_draft - Destroy draft after creating post or topic
# silent - Do not update topic stats and fields like last_post_user_id
@@ -88,6 +89,10 @@ class PostCreator
@opts[:skip_validations]
end
+ def skip_staff_author_pm_membership_sync?
+ @opts[:skip_staff_author_pm_membership_sync]
+ end
+
def guardian
@guardian ||= @opts[:guardian] || Guardian.new(@user)
end
@@ -210,7 +215,7 @@ class PostCreator
create_embedded_topic
@post.link_post_uploads
delete_owned_bookmarks
- ensure_in_allowed_users if guardian.is_staff?
+ ensure_in_allowed_users if guardian.is_staff? && !skip_staff_author_pm_membership_sync?
unarchive_message if !@opts[:import_mode]
DraftSequence.next!(@user, draft_key) if !@opts[:import_mode] && @opts[:advance_draft]
@post.save_reply_relationships
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index ab61ffd9955..35ecba28423 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -418,6 +418,8 @@ en:
reply_to_post_number_description: "Reply to a specific post number in the topic"
whisper: "Create as whisper"
whisper_description: "Create the post as a whisper. The author must be allowed to create whispers."
+ bypass_permission_checks: "Bypass permission checks"
+ bypass_permission_checks_description: "The post will be created as the selected author even if they cannot see or reply to the topic — including closed topics, secure categories, and personal messages. The author will not be granted access to the topic."
author_username: "Author"
post_id: "Post ID"
editor_username: "Editor"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
index 062cca5d21b..b87e14dd200 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
@@ -3,6 +3,11 @@
module DiscourseWorkflows
class Executor
class NodeExecutionContext
+ BYPASSED_PERMISSION_CHECKS_FIELD = "discourse_workflows_bypassed_permission_checks"
+ WORKFLOW_ID_FIELD = "discourse_workflows_workflow_id"
+ WORKFLOW_VERSION_ID_FIELD = "discourse_workflows_workflow_version_id"
+ NODE_ID_FIELD = "discourse_workflows_node_id"
+
MISSING = ParameterResolver::MISSING
RUN_CODE = CodeRunner::RUN_CODE
RUN_ONCE_FOR_ALL_ITEMS = CodeRunner::RUN_ONCE_FOR_ALL_ITEMS
@@ -315,15 +320,26 @@ module DiscourseWorkflows
HttpClient.new(self, item_index).request(method:, url:, headers:, body:, options:)
end
- def create_post(user:, raw:, topic_id:, reply_to_post_number: nil, whisper: false)
+ def create_post(
+ user:,
+ raw:,
+ topic_id:,
+ reply_to_post_number: nil,
+ whisper: false,
+ bypass_permission_checks: false
+ )
topic = ::Topic.find(topic_id)
- guardian = user.guardian
- guardian.ensure_can_see!(topic)
- raise Discourse::InvalidAccess if !guardian.can_create_post?(topic)
+ bypass_permission_checks = ActiveModel::Type::Boolean.new.cast(bypass_permission_checks)
+ guardian = bypass_permission_checks ? Discourse.system_user.guardian : user.guardian
- if topic.closed? || topic.archived?
- raise DiscourseWorkflows::NodeError,
- I18n.t("discourse_workflows.errors.post.topic_closed_or_archived")
+ if !bypass_permission_checks
+ guardian.ensure_can_see!(topic)
+ raise Discourse::InvalidAccess if !guardian.can_create_post?(topic)
+
+ if topic.closed? || topic.archived?
+ raise DiscourseWorkflows::NodeError,
+ I18n.t("discourse_workflows.errors.post.topic_closed_or_archived")
+ end
end
post_args = {
@@ -345,7 +361,14 @@ module DiscourseWorkflows
post_args[:post_type] = ::Post.types[:whisper]
end
- PostCreator.new(user, post_args).create!
+ if bypass_permission_checks
+ post_args[:guardian] = guardian
+ post_args[:skip_staff_author_pm_membership_sync] = true
+ end
+
+ post = PostCreator.new(user, post_args).create!
+ record_permission_bypass!(post) if bypass_permission_checks
+ post
end
def edit_post(user:, post_id:, raw:)
@@ -449,6 +472,16 @@ module DiscourseWorkflows
private
+ def record_permission_bypass!(post)
+ post.custom_fields[BYPASSED_PERMISSION_CHECKS_FIELD] = "true"
+ post.custom_fields[WORKFLOW_ID_FIELD] = @workflow.id if @workflow&.id
+ if @workflow&.active_version_id
+ post.custom_fields[WORKFLOW_VERSION_ID_FIELD] = @workflow.active_version_id
+ end
+ post.custom_fields[NODE_ID_FIELD] = @node_id if @node_id
+ post.save_custom_fields
+ end
+
def fetch_credentials(slot, item_index)
raise ArgumentError, "credential slot is required" if slot.blank?
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
index 6faa539e0f5..0e233638097 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
@@ -117,6 +117,19 @@ module DiscourseWorkflows
},
},
},
+ bypass_permission_checks: {
+ type: :boolean,
+ required: false,
+ default: false,
+ ui: {
+ control: :boolean,
+ },
+ display_options: {
+ show: {
+ operation: ["create"],
+ },
+ },
+ },
post_id: {
type: :string,
required: true,
@@ -328,6 +341,8 @@ module DiscourseWorkflows
"reply_to_post_number" =>
exec_ctx.get_node_parameter("reply_to_post_number", item_index),
"whisper" => exec_ctx.get_node_parameter("whisper", item_index, default: false),
+ "bypass_permission_checks" =>
+ exec_ctx.get_node_parameter("bypass_permission_checks", item_index, default: false),
"post_id" => exec_ctx.get_node_parameter("post_id", item_index),
"editor_username" => exec_ctx.get_node_parameter("editor_username", item_index),
"include_raw" => exec_ctx.get_node_parameter("include_raw", item_index, default: true),
@@ -390,6 +405,7 @@ module DiscourseWorkflows
topic_id: config["topic_id"],
reply_to_post_number: config["reply_to_post_number"],
whisper: config["whisper"],
+ bypass_permission_checks: config["bypass_permission_checks"],
)
{
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
index b4a1fb0e388..e7e90d25360 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
@@ -982,6 +982,55 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
end
end
+ describe "#create_post" do
+ it "records bypass provenance custom fields on the created post" do
+ author = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
+ workflow = Fabricate(:discourse_workflows_workflow, published: true)
+ first_post = Fabricate(:post)
+
+ ctx =
+ described_class.new(input_items: [], resolver: nil, workflow: workflow, node_id: "post-1")
+
+ post =
+ ctx.create_post(
+ user: author,
+ raw: "Reply created with a permission bypass",
+ topic_id: first_post.topic_id,
+ bypass_permission_checks: true,
+ )
+
+ expect(post.reload.custom_fields).to include(
+ described_class::BYPASSED_PERMISSION_CHECKS_FIELD => "true",
+ described_class::WORKFLOW_ID_FIELD => workflow.id.to_s,
+ described_class::WORKFLOW_VERSION_ID_FIELD => workflow.active_version_id.to_s,
+ described_class::NODE_ID_FIELD => "post-1",
+ )
+ end
+
+ it "does not record provenance custom fields without a bypass" do
+ author = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
+ workflow = Fabricate(:discourse_workflows_workflow, published: true)
+ first_post = Fabricate(:post)
+
+ ctx =
+ described_class.new(input_items: [], resolver: nil, workflow: workflow, node_id: "post-1")
+
+ post =
+ ctx.create_post(
+ user: author,
+ raw: "Reply created with the author permissions",
+ topic_id: first_post.topic_id,
+ )
+
+ expect(post.reload.custom_fields.keys).not_to include(
+ described_class::BYPASSED_PERMISSION_CHECKS_FIELD,
+ described_class::WORKFLOW_ID_FIELD,
+ described_class::WORKFLOW_VERSION_ID_FIELD,
+ described_class::NODE_ID_FIELD,
+ )
+ end
+ end
+
def workflow_with_data_table_dependency(data_table)
graph =
build_workflow_graph do |g|
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
index 49c9f117203..635ecb65899 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
@@ -194,6 +194,141 @@ RSpec.describe DiscourseWorkflows::Nodes::Post::V1 do
end.to raise_error(Discourse::InvalidAccess).and not_change { hidden_topic.posts.count }
end
+ it "creates a PM reply as a TL0 user when bypassing permission checks", :aggregate_failures do
+ tl0_user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
+ private_message = Fabricate(:private_message_topic, user: user, recipient: other_user)
+ Fabricate(
+ :post,
+ topic: private_message,
+ user: user,
+ raw: "Please help with my account.",
+ post_number: 1,
+ )
+ item = { "json" => { "topic" => { "id" => private_message.id } } }
+ result = nil
+
+ expect do
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "create",
+ "topic_id" => private_message.id.to_s,
+ "raw" => "Automated reply from a TL0 user.",
+ "author_username" => tl0_user.username,
+ "bypass_permission_checks" => true,
+ },
+ item: item,
+ )
+ end.to change { private_message.posts.count }.by(1)
+
+ reply = private_message.posts.order(:id).last
+ expect(reply.user_id).to eq(tl0_user.id)
+ expect(reply.raw).to eq("Automated reply from a TL0 user.")
+ expect(private_message.topic_allowed_users.where(user_id: tl0_user.id)).to be_blank
+ expect(reply.custom_fields).to include(
+ DiscourseWorkflows::Executor::NodeExecutionContext::BYPASSED_PERMISSION_CHECKS_FIELD =>
+ "true",
+ )
+ expect(result["post"]).to include("id" => reply.id, "username" => tl0_user.username)
+ end
+
+ it "creates a secure category reply as a TL0 user when bypassing permission checks",
+ :aggregate_failures do
+ tl0_user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
+ group = Fabricate(:group)
+ private_category = Fabricate(:private_category, group: group)
+ first_post =
+ create_post(user: admin, category: private_category, raw: "Secure category topic.")
+ topic = first_post.topic
+ item = { "json" => { "post" => { "topic_id" => topic.id } } }
+ result = nil
+
+ expect do
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "create",
+ "topic_id" => topic.id.to_s,
+ "raw" => "Secure category automated reply.",
+ "author_username" => tl0_user.username,
+ "bypass_permission_checks" => true,
+ },
+ item: item,
+ )
+ end.to change { topic.posts.count }.by(1)
+
+ reply = topic.posts.order(:id).last
+ expect(reply.user_id).to eq(tl0_user.id)
+ expect(reply.raw).to eq("Secure category automated reply.")
+ expect(group.users.exists?(tl0_user.id)).to eq(false)
+ expect(result["post"]).to include("id" => reply.id, "username" => tl0_user.username)
+ end
+
+ it "creates a whisper as a TL0 user when bypassing permission checks", :aggregate_failures do
+ SiteSetting.whispers_allowed_groups = Group::AUTO_GROUPS[:staff].to_s
+ tl0_user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
+ private_message = Fabricate(:private_message_topic, user: user, recipient: other_user)
+ Fabricate(
+ :post,
+ topic: private_message,
+ user: user,
+ raw: "Please help with my account.",
+ post_number: 1,
+ )
+ item = { "json" => { "topic" => { "id" => private_message.id } } }
+ result = nil
+
+ expect do
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "create",
+ "topic_id" => private_message.id.to_s,
+ "raw" => "Staff-only automated note from a TL0 user.",
+ "author_username" => tl0_user.username,
+ "bypass_permission_checks" => true,
+ "whisper" => true,
+ },
+ item: item,
+ )
+ end.to change { private_message.posts.count }.by(1)
+
+ reply = private_message.posts.order(:id).last
+ expect(reply.user_id).to eq(tl0_user.id)
+ expect(reply.raw).to eq("Staff-only automated note from a TL0 user.")
+ expect(reply.post_type).to eq(Post.types[:whisper])
+ expect(private_message.topic_allowed_users.where(user_id: tl0_user.id)).to be_blank
+ expect(result["post"]).to include(
+ "id" => reply.id,
+ "username" => tl0_user.username,
+ "post_type" => Post.types[:whisper],
+ )
+ end
+
+ it "creates a post in a closed topic when bypassing permission checks", :aggregate_failures do
+ tl0_user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
+ first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)
+ topic = first_post.topic
+ topic.update!(closed: true)
+
+ expect do
+ execute_node(
+ configuration: {
+ "operation" => "create",
+ "topic_id" => topic.id.to_s,
+ "raw" => "System authorization can reply to closed topics.",
+ "author_username" => tl0_user.username,
+ "bypass_permission_checks" => true,
+ },
+ item: item,
+ )
+ end.to change { topic.posts.count }.by(1)
+
+ reply = topic.posts.order(:id).last
+ expect(reply.user_id).to eq(tl0_user.id)
+ expect(reply.raw).to eq("System authorization can reply to closed topics.")
+ end
+
it "raises when creating in a closed or archived topic" do
first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)
topic = first_post.topic
diff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rb
index c9425b590d2..ca63bd930f0 100644
--- a/spec/lib/post_creator_spec.rb
+++ b/spec/lib/post_creator_spec.rb
@@ -1174,6 +1174,17 @@ RSpec.describe PostCreator do
PostCreator.create!(admin2, raw: "I am also an admin, and a mod", topic_id: post.topic_id)
expect(post.topic.topic_allowed_users.where(user_id: admin2.id).count).to eq(0)
+
+ tl0_user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
+ PostCreator.create!(
+ tl0_user,
+ raw: "Automated support reply",
+ topic_id: post.topic_id,
+ guardian: Discourse.system_user.guardian,
+ skip_staff_author_pm_membership_sync: true,
+ )
+
+ expect(post.topic.topic_allowed_users.where(user_id: tl0_user.id).count).to eq(0)
end
it "does not add whisperers to allowed users of the topic" do