diff --git a/lib/custom_wizard/guardian.rb b/lib/custom_wizard/guardian.rb new file mode 100644 index 00000000..c453f509 --- /dev/null +++ b/lib/custom_wizard/guardian.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true +class CustomWizard::Guardian + def initialize(user) + @user = user + end + + def can_edit_topic?(topic) + creating_wizard = topic.wizard_created.presence + return false unless creating_wizard + + wizard_builder = CustomWizard::Builder.new(creating_wizard, @user) + wizard = wizard_builder.build + wizard_actions = wizard.actions + return false if wizard_actions.empty? + + create_topic_action = wizard_actions.find do |action| + action['type'] === 'create_topic' + end + + return wizard_can_create_topic_on_category?(action, topic) + end + + private + + def wizard_can_create_topic_on_category?(action, topic) + return false unless topic.category.present? + + category = CustomWizard::Mapper.new( + inputs: action['category'], + data: + ) + end +end diff --git a/plugin.rb b/plugin.rb index 7e8c50e9..d412c76b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -86,6 +86,7 @@ after_initialize do ../lib/custom_wizard/submission.rb ../lib/custom_wizard/template.rb ../lib/custom_wizard/wizard.rb + ../lib/custom_wizard/guardian.rb ../lib/custom_wizard/api/api.rb ../lib/custom_wizard/api/authorization.rb ../lib/custom_wizard/api/endpoint.rb diff --git a/spec/components/custom_wizard/guardian_spec.rb b/spec/components/custom_wizard/guardian_spec.rb new file mode 100644 index 00000000..bb7578d2 --- /dev/null +++ b/spec/components/custom_wizard/guardian_spec.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +require_relative '../../plugin_helper' + +describe CustomWizard::Guardian do + +end