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

Restrict guest support to standard and business subscriptions

- Support mapped value subscription restrictions
- Restrict permitted guest value to standard and business
Dieser Commit ist enthalten in:
Angus McLeod 2023-02-08 13:32:24 +01:00
Ursprung 0cb76659e9
Commit edc94b6ea7
5 geänderte Dateien mit 62 neuen und 18 gelöschten Zeilen

Datei anzeigen

@ -15,6 +15,7 @@ import {
import Component from "@ember/component"; import Component from "@ember/component";
import { bind, later } from "@ember/runloop"; import { bind, later } from "@ember/runloop";
import I18n from "I18n"; import I18n from "I18n";
import Subscription from "../mixins/subscription";
const customFieldActionMap = { const customFieldActionMap = {
topic: ["create_topic", "send_message"], topic: ["create_topic", "send_message"],
@ -26,7 +27,7 @@ const customFieldActionMap = {
const values = ["present", "true", "false"]; const values = ["present", "true", "false"];
export default Component.extend({ export default Component.extend(Subscription, {
classNameBindings: [":mapper-selector", "activeType"], classNameBindings: [":mapper-selector", "activeType"],
showText: computed("activeType", function () { showText: computed("activeType", function () {
@ -129,24 +130,27 @@ export default Component.extend({
return this.connector === "is"; return this.connector === "is";
}), }),
@discourseComputed("site.groups", "guestGroup") @discourseComputed("site.groups", "guestGroup", "subscriptionType")
groups(groups, guestGroup) { groups(groups, guestGroup, subscriptionType) {
let result = groups; let result = groups;
if (!guestGroup) { if (!guestGroup) {
return result; return result;
} }
let guestIndex; if (["standard", "business"].includes(subscriptionType)) {
result.forEach((r, index) => { let guestIndex;
if (r.id === 0) { result.forEach((r, index) => {
r.name = I18n.t("admin.wizard.selector.label.users"); if (r.id === 0) {
guestIndex = index; r.name = I18n.t("admin.wizard.selector.label.users");
} guestIndex = index;
}); }
result.splice(guestIndex, 0, { });
id: -1, result.splice(guestIndex, 0, {
name: I18n.t("admin.wizard.selector.label.guests"), id: -1,
}); name: I18n.t("admin.wizard.selector.label.guests"),
});
}
return result; return result;
}, },
categories: alias("site.categories"), categories: alias("site.categories"),

Datei anzeigen

@ -53,7 +53,7 @@ en:
after_signup_after_time: "You can't use 'after time' and 'after signup' on the same wizard." after_signup_after_time: "You can't use 'after time' and 'after signup' on the same wizard."
after_time: "After time setting is invalid." after_time: "After time setting is invalid."
liquid_syntax_error: "Liquid syntax error in %{attribute}: %{message}" liquid_syntax_error: "Liquid syntax error in %{attribute}: %{message}"
subscription: "%{type} %{property} is subscription only" subscription: "%{type} %{property} usage is not supported on your subscription"
not_permitted_for_guests: "%{object_id} is not permitted when guests can access the wizard" not_permitted_for_guests: "%{object_id} is not permitted when guests can access the wizard"
site_settings: site_settings:

Datei anzeigen

@ -284,4 +284,8 @@ class CustomWizard::Mapper
user.avatar_template_url.gsub("{size}", parts.last) user.avatar_template_url.gsub("{size}", parts.last)
end end
end end
def self.mapped_value?(value)
value.is_a?(Array) && value.all? { |v| v.is_a?(Hash) && v.key?("type") }
end
end end

Datei anzeigen

@ -17,7 +17,7 @@ class CustomWizard::Subscription
none: [], none: [],
standard: ['*'], standard: ['*'],
business: ['*'], business: ['*'],
community: ['*'] community: ['*', "!#{CustomWizard::Wizard::GUEST_GROUP_ID}"]
}, },
restart_on_revisit: { restart_on_revisit: {
none: [], none: [],
@ -114,8 +114,15 @@ class CustomWizard::Subscription
## Subscription type does not support the attribute. ## Subscription type does not support the attribute.
return false if values.blank? return false if values.blank?
## Value is an exception for the subscription type
if (exceptions = get_exceptions(values)).any?
value = mapped_output(value) if CustomWizard::Mapper.mapped_value?(value)
value = [*value].map(&:to_s)
return false if (exceptions & value).length > 0
end
## Subscription type supports all values of the attribute. ## Subscription type supports all values of the attribute.
return true if values.first === "*" return true if values.include?("*")
## Subscription type supports some values of the attributes. ## Subscription type supports some values of the attributes.
values.include?(value) values.include?(value)
@ -192,4 +199,21 @@ class CustomWizard::Subscription
def self.includes?(feature, attribute, value) def self.includes?(feature, attribute, value)
new.includes?(feature, attribute, value) new.includes?(feature, attribute, value)
end end
protected
def get_exceptions(values)
values.reduce([]) do |result, value|
result << value.split("!").last if value.start_with?("!")
result
end
end
def mapped_output(value)
value.reduce([]) do |result, v|
## We can only validate mapped assignment values at the moment
result << v["output"] if v.is_a?(Hash) && v["type"] === "assignment"
result
end.flatten
end
end end

Datei anzeigen

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
describe CustomWizard::Subscription do describe CustomWizard::Subscription do
let(:guests_permitted) { get_wizard_fixture("wizard/guests_permitted") }
def undefine_client_classes def undefine_client_classes
Object.send(:remove_const, :SubscriptionClient) if Object.constants.include?(:SubscriptionClient) Object.send(:remove_const, :SubscriptionClient) if Object.constants.include?(:SubscriptionClient)
Object.send(:remove_const, :SubscriptionClientSubscription) if Object.constants.include?(:SubscriptionClientSubscription) Object.send(:remove_const, :SubscriptionClientSubscription) if Object.constants.include?(:SubscriptionClientSubscription)
@ -40,7 +42,7 @@ describe CustomWizard::Subscription do
expect(described_class.includes?(:wizard, :after_signup, true)).to eq(true) expect(described_class.includes?(:wizard, :after_signup, true)).to eq(true)
end end
it "ubscriber features are not included" do it "subscriber features are not included" do
expect(described_class.includes?(:wizard, :permitted, {})).to eq(false) expect(described_class.includes?(:wizard, :permitted, {})).to eq(false)
end end
end end
@ -69,6 +71,16 @@ describe CustomWizard::Subscription do
end end
end end
context "with a subscription" do
it "handles mapped values" do
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::STANDARD_PRODUCT_ID)
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(true)
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::COMMUNITY_PRODUCT_ID)
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(false)
end
end
context "with standard subscription" do context "with standard subscription" do
before do before do
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::STANDARD_PRODUCT_ID) SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::STANDARD_PRODUCT_ID)