0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 01:10:28 +01:00

simplified logic

Dieser Commit ist enthalten in:
Faizaan Gagan 2022-01-31 12:50:20 +05:30
Ursprung 5d882d69a2
Commit f5a35baa1b
4 geänderte Dateien mit 40 neuen und 121 gelöschten Zeilen

Datei anzeigen

@ -1,59 +1,17 @@
# frozen_string_literal: true # frozen_string_literal: true
module CustomWizardGuardian module CustomWizardGuardian
def can_see_topic?(topic, hide_deleted = true)
wizard_user_can_create_topic_on_category?(topic) || super
end
def can_edit_topic?(topic) def can_edit_topic?(topic)
wizard_user_can_create_topic_on_category?(topic) || super wizard_can_edit_topic?(topic) || super
end end
def can_create_post?(parent) def wizard_can_edit_topic?(topic)
result = parent.present? ? wizard_user_can_create_topic_on_category?(parent) : false created_by_wizard = !!topic.wizard_submission_id
result || super (
end is_my_own?(topic) &&
created_by_wizard &&
private can_see_topic?(topic) &&
can_create_post_on_topic?(topic)
def wizard_user_can_create_topic_on_category?(topic) )
wizard = creating_wizard(topic)
(wizard.present? && wizard.permitted? && wizard_can_create_topic_on_category?(wizard, topic))
end
def creating_wizard(topic)
wizard_id = topic.wizard_id.presence
wizard = CustomWizard::Builder.new(wizard_id, @user).build if wizard_id
wizard.presence
end
def wizard_can_create_topic_on_category?(wizard, topic)
return false unless topic.category.present?
wizard_actions = wizard.actions
return false if wizard_actions.empty?
create_topic_actions = wizard_actions.select do |action|
action['type'] === 'create_topic'
end
submission_data = begin
submissions = CustomWizard::Submission.list(wizard)
submissions.find { |sub| sub.id == topic.wizard_submission_id }&.fields_and_meta
end
categories = wizard_actions.map do |action|
category = CustomWizard::Mapper.new(
inputs: action['category'],
data: submission_data,
user: @user
).perform
category
end
categories.flatten!
return true if categories.include?(topic.category.id)
false
end end
end end

Datei anzeigen

@ -517,7 +517,6 @@ class CustomWizard::Action
skip_validations: true, skip_validations: true,
topic_opts: { topic_opts: {
custom_fields: { custom_fields: {
wizard_id: @wizard.id,
wizard_submission_id: @wizard.current_submission.id wizard_submission_id: @wizard.current_submission.id
} }
} }

Datei anzeigen

@ -126,10 +126,6 @@ after_initialize do
Liquid::Template.register_filter(::CustomWizard::LiquidFilter::FirstNonEmpty) Liquid::Template.register_filter(::CustomWizard::LiquidFilter::FirstNonEmpty)
add_to_class(:topic, :wizard_id) do
custom_fields['wizard_id']
end
add_to_class(:topic, :wizard_submission_id) do add_to_class(:topic, :wizard_submission_id) do
custom_fields['wizard_submission_id'] custom_fields['wizard_submission_id']
end end

Datei anzeigen

@ -1,4 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../plugin_helper' require_relative '../plugin_helper'
describe ::Guardian do describe ::Guardian do
@ -14,82 +15,47 @@ describe ::Guardian do
) )
} }
def create_topic_by_wizard(wizard)
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
topic
end
before do before do
CustomWizard::Template.save(wizard_template, skip_jobs: true) CustomWizard::Template.save(wizard_template, skip_jobs: true)
@template = CustomWizard::Template.find('super_mega_fun_wizard') @template = CustomWizard::Template.find('super_mega_fun_wizard')
end end
context "the user has access to creating wizard" do context "topic created by user using wizard" do
it "allows editing the topic first post" do it "allows editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build wizard = CustomWizard::Builder.new(@template[:id], user).build
topic = create_topic_by_wizard(wizard)
wizard.create_updater( expect(user.guardian.wizard_can_edit_topic?(topic)).to be_truthy
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_truthy
end end
end end
context "the user doesn't have access to creating wizard" do context "topic created by user without wizard" do
it "restricts editing the topic first post" do it "restricts editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build topic_params = {
CustomWizard::Wizard.any_instance.stubs(:permitted?).returns(false)
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title", title: "Topic Title",
category_id: category.id raw: "Topic body",
).first skip_validations: true
}
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey post = PostCreator.new(user, topic_params).create
end expect(user.guardian.wizard_can_edit_topic?(post.topic)).to be_falsey
end
context "the wizard can't create a topic in the category" do
it "restricts editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
new_category = Fabricate(:category)
topic.category_id = new_category.id
topic.save
topic.reload
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey
end end
end end
end end