Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-09 20:02:54 +01:00
Guest wizards cannot use composer or upload
Dieser Commit ist enthalten in:
Ursprung
b2b93aad59
Commit
e7ee89048a
9 geänderte Dateien mit 77 neuen und 34 gelöschten Zeilen
|
@ -1,6 +1,6 @@
|
||||||
import SingleSelectComponent from "select-kit/components/single-select";
|
import SingleSelectComponent from "select-kit/components/single-select";
|
||||||
import Subscription from "../mixins/subscription";
|
import Subscription from "../mixins/subscription";
|
||||||
import wizardSchema from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
|
import { filterValues } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
|
|
||||||
|
@ -40,25 +40,9 @@ export default SingleSelectComponent.extend(Subscription, {
|
||||||
return allowedTypes;
|
return allowedTypes;
|
||||||
},
|
},
|
||||||
|
|
||||||
contentList(feature, attribute, allowGuests) {
|
|
||||||
let attributes = wizardSchema[feature][attribute];
|
|
||||||
|
|
||||||
if (allowGuests) {
|
|
||||||
const filteredFeature = wizardSchema.filters.allow_guests[feature];
|
|
||||||
if (filteredFeature) {
|
|
||||||
const filteredAttribute = filteredFeature[attribute];
|
|
||||||
if (filteredAttribute) {
|
|
||||||
attributes = attributes.filter((a) => filteredAttribute.includes(a));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return attributes;
|
|
||||||
},
|
|
||||||
|
|
||||||
@discourseComputed("feature", "attribute", "wizard.allowGuests")
|
@discourseComputed("feature", "attribute", "wizard.allowGuests")
|
||||||
content(feature, attribute, allowGuests) {
|
content(feature, attribute) {
|
||||||
return this.contentList(feature, attribute, allowGuests)
|
return filterValues(this.wizard, feature, attribute)
|
||||||
.map((value) => {
|
.map((value) => {
|
||||||
let allowedSubscriptionTypes = this.allowedSubscriptionTypes(
|
let allowedSubscriptionTypes = this.allowedSubscriptionTypes(
|
||||||
feature,
|
feature,
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { later, scheduleOnce } from "@ember/runloop";
|
||||||
import Controller from "@ember/controller";
|
import Controller from "@ember/controller";
|
||||||
import copyText from "discourse/lib/copy-text";
|
import copyText from "discourse/lib/copy-text";
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
|
import { filterValues } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
|
||||||
|
|
||||||
export default Controller.extend({
|
export default Controller.extend({
|
||||||
hasName: notEmpty("wizard.name"),
|
hasName: notEmpty("wizard.name"),
|
||||||
|
@ -59,6 +60,19 @@ export default Controller.extend({
|
||||||
}
|
}
|
||||||
return wizardFieldList(steps);
|
return wizardFieldList(steps);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@discourseComputed("fieldTypes", "wizard.allowGuests")
|
||||||
|
filteredFieldTypes(fieldTypes) {
|
||||||
|
const fieldTypeIds = fieldTypes.map((f) => f.id);
|
||||||
|
const allowedTypeIds = filterValues(
|
||||||
|
this.wizard,
|
||||||
|
"field",
|
||||||
|
"type",
|
||||||
|
fieldTypeIds
|
||||||
|
);
|
||||||
|
return fieldTypes.filter((f) => allowedTypeIds.includes(f.id));
|
||||||
|
},
|
||||||
|
|
||||||
getErrorMessage(result) {
|
getErrorMessage(result) {
|
||||||
if (result.backend_validation_error) {
|
if (result.backend_validation_error) {
|
||||||
return result.backend_validation_error;
|
return result.backend_validation_error;
|
||||||
|
|
|
@ -212,6 +212,24 @@ const action = {
|
||||||
|
|
||||||
const filters = {
|
const filters = {
|
||||||
allow_guests: {
|
allow_guests: {
|
||||||
|
field: {
|
||||||
|
type: [
|
||||||
|
"text",
|
||||||
|
"textarea",
|
||||||
|
"text_only",
|
||||||
|
"date",
|
||||||
|
"time",
|
||||||
|
"date_time",
|
||||||
|
"number",
|
||||||
|
"checkbox",
|
||||||
|
"url",
|
||||||
|
"dropdown",
|
||||||
|
"tag",
|
||||||
|
"category",
|
||||||
|
"group",
|
||||||
|
"user_selector",
|
||||||
|
],
|
||||||
|
},
|
||||||
action: {
|
action: {
|
||||||
type: ["route_to", "send_message"],
|
type: ["route_to", "send_message"],
|
||||||
},
|
},
|
||||||
|
@ -235,14 +253,26 @@ const wizardSchema = {
|
||||||
filters,
|
filters,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function buildFieldTypes(types) {
|
|
||||||
wizardSchema.field.types = types;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildFieldValidations(validations) {
|
export function buildFieldValidations(validations) {
|
||||||
wizardSchema.field.validations = validations;
|
wizardSchema.field.validations = validations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function filterValues(currentWizard, feature, attribute, values = null) {
|
||||||
|
values = values || wizardSchema[feature][attribute];
|
||||||
|
|
||||||
|
if (currentWizard.allowGuests) {
|
||||||
|
const filteredFeature = wizardSchema.filters.allow_guests[feature];
|
||||||
|
if (filteredFeature) {
|
||||||
|
const filtered = filteredFeature[attribute];
|
||||||
|
if (filtered) {
|
||||||
|
values = values.filter((v) => filtered.includes(v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
const siteSettings = getOwner(this).lookup("service:site-settings");
|
const siteSettings = getOwner(this).lookup("service:site-settings");
|
||||||
if (siteSettings.wizard_apis_enabled) {
|
if (siteSettings.wizard_apis_enabled) {
|
||||||
wizardSchema.action.types.send_to_api = {
|
wizardSchema.action.types.send_to_api = {
|
||||||
|
|
|
@ -14,7 +14,8 @@ const CustomWizardAdmin = EmberObject.extend({
|
||||||
allowGuests(permitted) {
|
allowGuests(permitted) {
|
||||||
return (
|
return (
|
||||||
permitted &&
|
permitted &&
|
||||||
permitted.filter((p) => p.output.includes(GUEST_GROUP_ID)).length
|
permitted.filter((p) => p.output && p.output.includes(GUEST_GROUP_ID))
|
||||||
|
.length
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import DiscourseRoute from "discourse/routes/discourse";
|
import DiscourseRoute from "discourse/routes/discourse";
|
||||||
import { buildFieldTypes, buildFieldValidations } from "../lib/wizard-schema";
|
import { buildFieldValidations } from "../lib/wizard-schema";
|
||||||
import EmberObject, { set } from "@ember/object";
|
import EmberObject, { set } from "@ember/object";
|
||||||
import { A } from "@ember/array";
|
import { A } from "@ember/array";
|
||||||
import { all } from "rsvp";
|
import { all } from "rsvp";
|
||||||
|
@ -11,7 +11,6 @@ export default DiscourseRoute.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
afterModel(model) {
|
afterModel(model) {
|
||||||
buildFieldTypes(model.field_types);
|
|
||||||
buildFieldValidations(model.realtime_validations);
|
buildFieldValidations(model.realtime_validations);
|
||||||
|
|
||||||
return all([
|
return all([
|
||||||
|
|
|
@ -161,7 +161,7 @@
|
||||||
wizard=wizard
|
wizard=wizard
|
||||||
currentField=currentField
|
currentField=currentField
|
||||||
wizardFields=wizardFields
|
wizardFields=wizardFields
|
||||||
fieldTypes=fieldTypes
|
fieldTypes=filteredFieldTypes
|
||||||
subscribed=subscribed}}
|
subscribed=subscribed}}
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
|
||||||
|
@ -179,7 +179,7 @@
|
||||||
apis=apis
|
apis=apis
|
||||||
removeAction="removeAction"
|
removeAction="removeAction"
|
||||||
wizardFields=wizardFields
|
wizardFields=wizardFields
|
||||||
fieldTypes=fieldTypes}}
|
fieldTypes=filteredFieldTypes}}
|
||||||
{{/each}}
|
{{/each}}
|
||||||
|
|
||||||
<div class="admin-wizard-buttons">
|
<div class="admin-wizard-buttons">
|
||||||
|
|
|
@ -29,6 +29,11 @@ class CustomWizard::Field
|
||||||
attr_accessor :index,
|
attr_accessor :index,
|
||||||
:step
|
:step
|
||||||
|
|
||||||
|
REQUIRES_USER = %w[
|
||||||
|
composer
|
||||||
|
upload
|
||||||
|
]
|
||||||
|
|
||||||
def initialize(attrs)
|
def initialize(attrs)
|
||||||
@raw = attrs || {}
|
@raw = attrs || {}
|
||||||
@id = attrs[:id]
|
@id = attrs[:id]
|
||||||
|
|
|
@ -30,6 +30,7 @@ class CustomWizard::TemplateValidator
|
||||||
validate_subscription(field, :field)
|
validate_subscription(field, :field)
|
||||||
check_required(field, :field)
|
check_required(field, :field)
|
||||||
validate_liquid_template(field, :field)
|
validate_liquid_template(field, :field)
|
||||||
|
validate_guests(field, :field)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -39,7 +40,7 @@ class CustomWizard::TemplateValidator
|
||||||
validate_subscription(action, :action)
|
validate_subscription(action, :action)
|
||||||
check_required(action, :action)
|
check_required(action, :action)
|
||||||
validate_liquid_template(action, :action)
|
validate_liquid_template(action, :action)
|
||||||
validate_action(action)
|
validate_guests(action, :action)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -81,13 +82,18 @@ class CustomWizard::TemplateValidator
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_action(action)
|
def validate_guests(object, type)
|
||||||
guests_permitted = @data[:permitted] && @data[:permitted].any? do |m|
|
guests_permitted = @data[:permitted] && @data[:permitted].any? do |m|
|
||||||
m["output"].include?(CustomWizard::Wizard::GUEST_GROUP_ID)
|
m["output"].include?(CustomWizard::Wizard::GUEST_GROUP_ID)
|
||||||
end
|
end
|
||||||
|
return unless guests_permitted
|
||||||
|
|
||||||
if guests_permitted && CustomWizard::Action::REQUIRES_USER.include?(action[:type])
|
if type === :action && CustomWizard::Action::REQUIRES_USER.include?(object[:type])
|
||||||
errors.add :base, I18n.t("wizard.validation.not_permitted_for_guests", object_id: action[:id])
|
errors.add :base, I18n.t("wizard.validation.not_permitted_for_guests", object_id: object[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
if type === :field && CustomWizard::Field::REQUIRES_USER.include?(object[:type])
|
||||||
|
errors.add :base, I18n.t("wizard.validation.not_permitted_for_guests", object_id: object[:id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -147,13 +147,17 @@ describe CustomWizard::TemplateValidator do
|
||||||
).to eq(true)
|
).to eq(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "validates restrictions on wizards that permit guests" do
|
it "validates user-only features" do
|
||||||
template[:permitted] = guests_permitted['permitted']
|
template[:permitted] = guests_permitted['permitted']
|
||||||
validator = CustomWizard::TemplateValidator.new(template)
|
validator = CustomWizard::TemplateValidator.new(template)
|
||||||
expect(validator.perform).to eq(false)
|
expect(validator.perform).to eq(false)
|
||||||
expect(validator.errors.first.type).to eq(
|
errors = validator.errors.to_a
|
||||||
|
expect(errors).to include(
|
||||||
I18n.t("wizard.validation.not_permitted_for_guests", object_id: "action_1")
|
I18n.t("wizard.validation.not_permitted_for_guests", object_id: "action_1")
|
||||||
)
|
)
|
||||||
|
expect(errors).to include(
|
||||||
|
I18n.t("wizard.validation.not_permitted_for_guests", object_id: "step_2_field_7")
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "validates step attributes" do
|
it "validates step attributes" do
|
||||||
|
|
Laden …
In neuem Issue referenzieren