Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 09:20:29 +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 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 I18n from "I18n";
|
||||
|
||||
|
@ -40,25 +40,9 @@ export default SingleSelectComponent.extend(Subscription, {
|
|||
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")
|
||||
content(feature, attribute, allowGuests) {
|
||||
return this.contentList(feature, attribute, allowGuests)
|
||||
content(feature, attribute) {
|
||||
return filterValues(this.wizard, feature, attribute)
|
||||
.map((value) => {
|
||||
let allowedSubscriptionTypes = this.allowedSubscriptionTypes(
|
||||
feature,
|
||||
|
|
|
@ -10,6 +10,7 @@ import { later, scheduleOnce } from "@ember/runloop";
|
|||
import Controller from "@ember/controller";
|
||||
import copyText from "discourse/lib/copy-text";
|
||||
import I18n from "I18n";
|
||||
import { filterValues } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
|
||||
|
||||
export default Controller.extend({
|
||||
hasName: notEmpty("wizard.name"),
|
||||
|
@ -59,6 +60,19 @@ export default Controller.extend({
|
|||
}
|
||||
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) {
|
||||
if (result.backend_validation_error) {
|
||||
return result.backend_validation_error;
|
||||
|
|
|
@ -212,6 +212,24 @@ const action = {
|
|||
|
||||
const filters = {
|
||||
allow_guests: {
|
||||
field: {
|
||||
type: [
|
||||
"text",
|
||||
"textarea",
|
||||
"text_only",
|
||||
"date",
|
||||
"time",
|
||||
"date_time",
|
||||
"number",
|
||||
"checkbox",
|
||||
"url",
|
||||
"dropdown",
|
||||
"tag",
|
||||
"category",
|
||||
"group",
|
||||
"user_selector",
|
||||
],
|
||||
},
|
||||
action: {
|
||||
type: ["route_to", "send_message"],
|
||||
},
|
||||
|
@ -235,14 +253,26 @@ const wizardSchema = {
|
|||
filters,
|
||||
};
|
||||
|
||||
export function buildFieldTypes(types) {
|
||||
wizardSchema.field.types = types;
|
||||
}
|
||||
|
||||
export function buildFieldValidations(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");
|
||||
if (siteSettings.wizard_apis_enabled) {
|
||||
wizardSchema.action.types.send_to_api = {
|
||||
|
|
|
@ -14,7 +14,8 @@ const CustomWizardAdmin = EmberObject.extend({
|
|||
allowGuests(permitted) {
|
||||
return (
|
||||
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 { buildFieldTypes, buildFieldValidations } from "../lib/wizard-schema";
|
||||
import { buildFieldValidations } from "../lib/wizard-schema";
|
||||
import EmberObject, { set } from "@ember/object";
|
||||
import { A } from "@ember/array";
|
||||
import { all } from "rsvp";
|
||||
|
@ -11,7 +11,6 @@ export default DiscourseRoute.extend({
|
|||
},
|
||||
|
||||
afterModel(model) {
|
||||
buildFieldTypes(model.field_types);
|
||||
buildFieldValidations(model.realtime_validations);
|
||||
|
||||
return all([
|
||||
|
|
|
@ -161,7 +161,7 @@
|
|||
wizard=wizard
|
||||
currentField=currentField
|
||||
wizardFields=wizardFields
|
||||
fieldTypes=fieldTypes
|
||||
fieldTypes=filteredFieldTypes
|
||||
subscribed=subscribed}}
|
||||
{{/if}}
|
||||
|
||||
|
@ -179,7 +179,7 @@
|
|||
apis=apis
|
||||
removeAction="removeAction"
|
||||
wizardFields=wizardFields
|
||||
fieldTypes=fieldTypes}}
|
||||
fieldTypes=filteredFieldTypes}}
|
||||
{{/each}}
|
||||
|
||||
<div class="admin-wizard-buttons">
|
||||
|
|
|
@ -29,6 +29,11 @@ class CustomWizard::Field
|
|||
attr_accessor :index,
|
||||
:step
|
||||
|
||||
REQUIRES_USER = %w[
|
||||
composer
|
||||
upload
|
||||
]
|
||||
|
||||
def initialize(attrs)
|
||||
@raw = attrs || {}
|
||||
@id = attrs[:id]
|
||||
|
|
|
@ -30,6 +30,7 @@ class CustomWizard::TemplateValidator
|
|||
validate_subscription(field, :field)
|
||||
check_required(field, :field)
|
||||
validate_liquid_template(field, :field)
|
||||
validate_guests(field, :field)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -39,7 +40,7 @@ class CustomWizard::TemplateValidator
|
|||
validate_subscription(action, :action)
|
||||
check_required(action, :action)
|
||||
validate_liquid_template(action, :action)
|
||||
validate_action(action)
|
||||
validate_guests(action, :action)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -81,13 +82,18 @@ class CustomWizard::TemplateValidator
|
|||
end
|
||||
end
|
||||
|
||||
def validate_action(action)
|
||||
def validate_guests(object, type)
|
||||
guests_permitted = @data[:permitted] && @data[:permitted].any? do |m|
|
||||
m["output"].include?(CustomWizard::Wizard::GUEST_GROUP_ID)
|
||||
end
|
||||
return unless guests_permitted
|
||||
|
||||
if guests_permitted && CustomWizard::Action::REQUIRES_USER.include?(action[:type])
|
||||
errors.add :base, I18n.t("wizard.validation.not_permitted_for_guests", object_id: action[:id])
|
||||
if type === :action && CustomWizard::Action::REQUIRES_USER.include?(object[:type])
|
||||
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
|
||||
|
||||
|
|
|
@ -147,13 +147,17 @@ describe CustomWizard::TemplateValidator do
|
|||
).to eq(true)
|
||||
end
|
||||
|
||||
it "validates restrictions on wizards that permit guests" do
|
||||
it "validates user-only features" do
|
||||
template[:permitted] = guests_permitted['permitted']
|
||||
validator = CustomWizard::TemplateValidator.new(template)
|
||||
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")
|
||||
)
|
||||
expect(errors).to include(
|
||||
I18n.t("wizard.validation.not_permitted_for_guests", object_id: "step_2_field_7")
|
||||
)
|
||||
end
|
||||
|
||||
it "validates step attributes" do
|
||||
|
|
Laden …
In neuem Issue referenzieren