Skip to content

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

DEV: Explain why a workflow actor is rejected

Régis Hanold2943afb1a5c
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index 13499682014..6bf9ee2412b 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -83,6 +83,13 @@ en:
unknown_operation: "Unknown operation: %{operation}."
actor:
blank: "The user for '%{field}' resolved to a blank value."
+ not_found: "No user found with username '%{username}'."
+ id_not_found: "No user found with ID %{id}."
+ restricted:
+ staged: "The user '%{username}' is a staged user and can't perform actions."
+ silenced: "The user '%{username}' is silenced."
+ suspended: "The user '%{username}' is suspended."
+ inactive: "The user '%{username}' hasn't activated their account."
item:
inconsistent_format_title: "Inconsistent item format"
inconsistent_format: "Every returned item must use the same format."
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/actor_policy.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/actor_policy.rb
deleted file mode 100644
index 5f085dfea04..00000000000
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/actor_policy.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseWorkflows
- class Executor
- class ActorPolicy
- def initialize(exec_ctx)
- @exec_ctx = exec_ctx
- end
-
- def ensure_allowed!(actor, field:, item_index:, source:, purpose:)
- return true if actor.is_a?(DiscourseWorkflows::AnonymousActor)
- raise Discourse::InvalidAccess if actor.blank?
- raise Discourse::InvalidAccess if actor.staged?
- raise Discourse::InvalidAccess if actor.silenced?
- raise Discourse::InvalidAccess if actor.suspended?
- raise Discourse::InvalidAccess if !actor.active?
-
- true
- end
- end
- end
-end
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 6fdc7b7d7dd..3cbd4e29caf 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,8 @@
module DiscourseWorkflows
class Executor
class NodeExecutionContext
+ include NodeErrorHandling
+
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"
@@ -307,38 +309,41 @@ module DiscourseWorkflows
username = get_node_parameter(path, item_index, default: default)
if username.blank?
- raise DiscourseWorkflows::NodeError,
- I18n.t("discourse_workflows.errors.actor.blank", field: path.to_s)
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.actor.blank", field: path.to_s),
+ item_index:,
+ )
end
- actor_from(username: username, field: path.to_s, item_index: item_index)
+ actor_from(username: username, item_index: item_index)
end
- def actor_from(username: nil, id: nil, field: nil, item_index: nil)
+ def actor_from(username: nil, id: nil, item_index: nil)
actor =
if username == DiscourseWorkflows::AnonymousActor::USERNAME
DiscourseWorkflows::AnonymousActor.new
else
- find_user(username: username.presence, id: id)
+ find_user(username: username.presence, id: id, item_index: item_index)
end
- ensure_actor_allowed!(actor, field: field, item_index: item_index)
+ ensure_actor_allowed!(actor, item_index:)
actor
end
- def find_user(username: nil, id: nil)
+ def find_user(username: nil, id: nil, item_index: nil)
if username.present? == id.present?
raise ArgumentError, "Provide exactly one of username or id"
end
- if username.present?
- user = User.find_by_username(username)
- raise DiscourseWorkflows::NodeError, "User '#{username}' not found" if user.nil?
- else
- user = User.find_by(id: id)
- raise DiscourseWorkflows::NodeError, "User with id #{id} not found" if user.nil?
- end
+ user = username.present? ? User.find_by_username(username) : User.find_by(id:)
+ return user if user
- user
+ message =
+ if username.present?
+ I18n.t("discourse_workflows.errors.actor.not_found", username:)
+ else
+ I18n.t("discourse_workflows.errors.actor.id_not_found", id:)
+ end
+ raise_node_error!(message, item_index:)
end
def http_request(method:, url:, headers: {}, body: nil, options: {}, item_index: 0)
@@ -526,6 +531,27 @@ module DiscourseWorkflows
private
+ def ensure_actor_allowed!(actor, item_index:)
+ return if actor.is_a?(DiscourseWorkflows::AnonymousActor)
+
+ reason =
+ if actor.staged?
+ :staged
+ elsif actor.silenced?
+ :silenced
+ elsif actor.suspended?
+ :suspended
+ elsif !actor.active?
+ :inactive
+ end
+ return if reason.nil?
+
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.actor.restricted.#{reason}", username: actor.username),
+ item_index:,
+ )
+ end
+
def post_destroyer(user, post)
PostDestroyer.new(
user,
@@ -560,30 +586,6 @@ module DiscourseWorkflows
with_item_index(item_index) { @resolver.resolve_hash(credential.data || {}) }
end
- def ensure_actor_allowed!(actor, field:, item_index:)
- actor_policy.ensure_allowed!(
- actor,
- field: field,
- item_index: item_index,
- source: actor_source(field, item_index),
- purpose: @node_identifier,
- )
- end
-
- def actor_policy
- @actor_policy ||= ActorPolicy.new(self)
- end
-
- def actor_source(field, item_index)
- return :direct if field.blank?
-
- raw_value = get_node_parameter(field, item_index || 0, options: { raw_expressions: true })
- return :default if raw_value.nil?
- return :expression if Schema.expression_value?(raw_value)
-
- :static_config
- end
-
def current_snapshot_node
@workflow_snapshot&.find_node(node_id)
end
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 02196ced2fe..ce224c1ac04 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
@@ -609,7 +609,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
expect { ctx.find_user(username: "nonexistent_user") }.to raise_error(
DiscourseWorkflows::NodeError,
- "User 'nonexistent_user' not found",
+ node_error_message(:not_found, scope: :actor, username: "nonexistent_user"),
)
end
@@ -618,7 +618,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
expect { ctx.find_user(id: -999) }.to raise_error(
DiscourseWorkflows::NodeError,
- "User with id -999 not found",
+ node_error_message(:id_not_found, scope: :actor, id: -999),
)
end
@@ -636,15 +636,51 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
end
end
+ describe "#actor_from" do
+ it "suffixes an unknown actor with the item index" do
+ ctx = described_class.new(input_items: [], resolver: nil)
+
+ expect { ctx.actor_from(username: "ghost", item_index: 1) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ node_error_message(:not_found, scope: :actor, item_index: 1, username: "ghost"),
+ )
+ end
+
+ it "rejects actors with restricted account states with a reason" do
+ staged_user = Fabricate(:user, staged: true, active: false)
+ silenced_user = Fabricate(:user, silenced_till: 1.year.from_now)
+ suspended_user = Fabricate(:user, suspended_till: 1.year.from_now)
+ inactive_user = Fabricate(:user, active: false)
+ ctx = described_class.new(input_items: [], resolver: nil)
+
+ aggregate_failures do
+ {
+ staged: staged_user,
+ silenced: silenced_user,
+ suspended: suspended_user,
+ inactive: inactive_user,
+ }.each do |reason, actor|
+ expect { ctx.actor_from(username: actor.username, item_index: 2) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ node_error_message(
+ "restricted.#{reason}",
+ scope: :actor,
+ item_index: 2,
+ username: actor.username,
+ ),
+ )
+ end
+ end
+ end
+ end
+
describe "#actor_from_parameter" do
fab!(:user)
- it "resolves actor fields through the central actor policy" do
+ it "resolves the actor from the parameter" do
resolver_context = { "$json" => { "actor" => user.username } }
sandbox = DiscourseWorkflows::JsSandbox.new(resolver_context)
resolver = DiscourseWorkflows::ExpressionResolver.new(resolver_context, sandbox: sandbox)
- policy = instance_spy(DiscourseWorkflows::Executor::ActorPolicy)
- allow(DiscourseWorkflows::Executor::ActorPolicy).to receive(:new).and_return(policy)
ctx =
described_class.new(
input_items: [{ "json" => { "actor" => user.username } }],
@@ -656,41 +692,11 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
)
expect(ctx.actor_from_parameter("actor_username")).to eq(user)
- expect(policy).to have_received(:ensure_allowed!).with(
- user,
- field: "actor_username",
- item_index: 0,
- source: :expression,
- purpose: "action:post",
- )
ensure
resolver&.dispose
sandbox&.dispose
end
- it "rejects actors with restricted account states" do
- staged_user = Fabricate(:user, staged: true)
- silenced_user = Fabricate(:user, silenced_till: 1.year.from_now)
- suspended_user = Fabricate(:user, suspended_till: 1.year.from_now)
- inactive_user = Fabricate(:user, active: false)
- ctx = described_class.new(input_items: [], resolver: nil)
-
- aggregate_failures do
- expect { ctx.actor_from(username: staged_user.username) }.to raise_error(
- Discourse::InvalidAccess,
- )
- expect { ctx.actor_from(username: silenced_user.username) }.to raise_error(
- Discourse::InvalidAccess,
- )
- expect { ctx.actor_from(username: suspended_user.username) }.to raise_error(
- Discourse::InvalidAccess,
- )
- expect { ctx.actor_from(username: inactive_user.username) }.to raise_error(
- Discourse::InvalidAccess,
- )
- end
- end
-
it "defaults to the system user when the actor field is not configured" do
resolver_context = { "$json" => {} }
sandbox = DiscourseWorkflows::JsSandbox.new(resolver_context)
@@ -718,7 +724,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
expect { ctx.actor_from_parameter("actor_username") }.to raise_error(
DiscourseWorkflows::NodeError,
- I18n.t("discourse_workflows.errors.actor.blank", field: "actor_username"),
+ node_error_message(:blank, scope: :actor, item_index: 0, field: "actor_username"),
)
ensure
resolver&.dispose
@@ -740,7 +746,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
expect { ctx.actor_from_parameter("actor_username") }.to raise_error(
DiscourseWorkflows::NodeError,
- I18n.t("discourse_workflows.errors.actor.blank", field: "actor_username"),
+ node_error_message(:blank, scope: :actor, item_index: 0, field: "actor_username"),
)
ensure
resolver&.dispose
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/badge_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/badge_spec.rb
index bd7b01c3444..1d3a9cbb3f7 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/badge_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/badge_spec.rb
@@ -137,7 +137,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Badge::V1 do
expect { execute_node(configuration: config, item: item) }.to raise_error(
DiscourseWorkflows::NodeError,
- "User 'nonexistent_user' not found",
+ node_error_message(:not_found, scope: :actor, username: "nonexistent_user"),
)
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/flag_user_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/flag_user_spec.rb
index dff7b206830..332850aaff6 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/flag_user_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/flag_user_spec.rb
@@ -246,7 +246,7 @@ RSpec.describe DiscourseWorkflows::Nodes::FlagUser::V1 do
it "raises when the username does not resolve" do
expect { flag("username" => "nope") }.to raise_error(
DiscourseWorkflows::NodeError,
- "User 'nope' not found",
+ node_error_message(:not_found, scope: :actor, username: "nope"),
)
end
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/group_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/group_spec.rb
index 40ee40bf988..448e0c14b4a 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/group_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/group_spec.rb
@@ -274,7 +274,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Group::V1 do
expect { execute_node(configuration: config, item: item) }.to raise_error(
DiscourseWorkflows::NodeError,
- "User 'nonexistent_user' not found",
+ node_error_message(:not_found, scope: :actor, username: "nonexistent_user"),
)
end
@@ -473,7 +473,10 @@ RSpec.describe DiscourseWorkflows::Nodes::Group::V1 do
"group_id" => group.id,
},
)
- end.to raise_error(DiscourseWorkflows::NodeError, "User 'missing_user' not found")
+ end.to raise_error(
+ DiscourseWorkflows::NodeError,
+ node_error_message(:not_found, scope: :actor, username: "missing_user"),
+ )
end
it "raises when actor_username cannot see the group" do
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 407b2d4f995..5221d1f62a9 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
@@ -196,7 +196,10 @@ RSpec.describe DiscourseWorkflows::Nodes::Post::V1 do
},
item: item,
)
- end.to raise_error(DiscourseWorkflows::NodeError, "User 'nonexistent_user' not found")
+ end.to raise_error(
+ DiscourseWorkflows::NodeError,
+ node_error_message(:not_found, scope: :actor, item_index: 0, username: "nonexistent_user"),
+ )
end
it "raises when the create author cannot see the topic" do
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb
index de2154aa437..8d939b1e3b2 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb
@@ -160,7 +160,15 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 do
},
item: item,
)
- end.to raise_error(DiscourseWorkflows::NodeError, "User 'nonexistent_user' not found")
+ end.to raise_error(
+ DiscourseWorkflows::NodeError,
+ node_error_message(
+ :not_found,
+ scope: :actor,
+ item_index: 0,
+ username: "nonexistent_user",
+ ),
+ )
end
it "raises when topic creation fails validation" do
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb
index 407d4789a3b..97efa6dfa33 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb
@@ -603,7 +603,10 @@ RSpec.describe DiscourseWorkflows::Nodes::User::V1 do
it "raises when the user does not exist" do
expect do
execute_node(configuration: { "operation" => "get", "username" => "missing_user" })
- end.to raise_error(DiscourseWorkflows::NodeError, "User 'missing_user' not found")
+ end.to raise_error(
+ DiscourseWorkflows::NodeError,
+ node_error_message(:not_found, scope: :actor, username: "missing_user"),
+ )
end
end
diff --git a/plugins/discourse-workflows/spec/support/node_execution_helpers.rb b/plugins/discourse-workflows/spec/support/node_execution_helpers.rb
index 8ad711e9d5b..954d5b1bfea 100644
--- a/plugins/discourse-workflows/spec/support/node_execution_helpers.rb
+++ b/plugins/discourse-workflows/spec/support/node_execution_helpers.rb
@@ -54,6 +54,11 @@ module NodeExecutionHelpers
result.first.first["json"]
end
+ def node_error_message(key, scope:, item_index: nil, **args)
+ message = I18n.t("discourse_workflows.errors.#{scope}.#{key}", **args)
+ item_index.nil? ? message : "#{message} [item #{item_index}]"
+ end
+
def trigger_context(parameters)
DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters.deep_stringify_keys })
end