From c08c34a1bfdf69b4d3375623a3bca9e7dafdf20d Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sat, 7 Nov 2026 16:13:44 +0800 Subject: [PATCH] FIX: Skip user-only actions for guests Actions that need a signed-in user are meant to be skipped for guests, but the check compared the action's id instead of its type, so it never ran. Watch tags was also missing from the list, so in a guest wizard with the wizard user option on it ran without a user and the step failed. The check now uses the action type and records the skip in the action log, and watch tags is on the list. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DSz8MWXii43sifu8gH46aE --- lib/custom_wizard/action.rb | 18 +++++++++--------- plugin.rb | 2 +- spec/components/custom_wizard/action_spec.rb | 16 ++++++++++++++++ .../custom_wizard/template_validator_spec.rb | 11 +++++++++++ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index c8a091f29..3028197e1 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -2,7 +2,7 @@ class CustomWizard::Action attr_accessor :submission, :action, :user, :guardian, :result - REQUIRES_USER = %w[update_profile open_composer watch_categories add_to_group] + REQUIRES_USER = %w[update_profile open_composer watch_categories watch_tags add_to_group] WIZARD_USER = "wizard-user" @@ -17,17 +17,17 @@ def initialize(opts) end def perform - if REQUIRES_USER.include?(action["id"]) && !@user + if REQUIRES_USER.include?(action["type"]) && !@user log_error("action requires user", "id: #{action["id"]};") - @result.success = false - return @result - end - - ActiveRecord::Base.transaction { self.send(action["type"].to_sym) } + else + ActiveRecord::Base.transaction { self.send(action["type"].to_sym) } - @result.handler&.enqueue_jobs if creates_post? && @result.success? + @result.handler&.enqueue_jobs if creates_post? && @result.success? - @submission.fields[action["id"]] = @result.output if @result.success? && @result.output.present? + if @result.success? && @result.output.present? + @submission.fields[action["id"]] = @result.output + end + end save_log diff --git a/plugin.rb b/plugin.rb index aef351273..8efeda0ed 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.18.12 +# version: 2.18.13 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index 4cc552e85..168589a9f 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -796,6 +796,22 @@ def complete_as_guest(email) expect(post.exists?).to eq(true) end end + + it "skips an action that needs a user" do + wizard = CustomWizard::Wizard.new(@template, nil, CustomWizard::Wizard.generate_guest_id) + action = + CustomWizard::Action.new( + wizard: wizard, + action: watch_tags, + submission: wizard.current_submission, + ) + + expect(action.perform.success?).to eq(false) + + log = CustomWizard::Log.list.logs.first + expect(log.message).to include("action requires user") + expect(log.username).to eq(wizard.guest_id) + end end end diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index 9e84443b5..6c652fa20 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -9,6 +9,7 @@ let(:composer_preview) { get_wizard_fixture("field/composer_preview") } let(:guests_permitted) { get_wizard_fixture("wizard/guests_permitted") } let(:upload_field) { get_wizard_fixture("field/upload") } + let(:watch_tags) { get_wizard_fixture("actions/watch_tags") } let(:validation_condition) { get_wizard_fixture("condition/validation_condition") } let(:valid_liquid_template) { <<-LIQUID.strip } @@ -105,6 +106,16 @@ def expect_validation_failure(object_id, message) ) end + it "validates user-only actions" do + template[:permitted] = guests_permitted["permitted"] + template[:actions] << watch_tags.merge(id: "watch_tags_action") + validator = CustomWizard::TemplateValidator.new(template) + expect(validator.perform).to eq(false) + expect(validator.errors.to_a).to include( + I18n.t("wizard.validation.not_permitted_for_guests", object_id: "watch_tags_action"), + ) + end + it "validates step attributes" do template[:steps][0][:condition] = user_condition["condition"] expect(CustomWizard::TemplateValidator.new(template).perform).to eq(true)