Skip to content

A platform for community discussion. Free, open, simple.

DEV: restrict everyone on whisper groups via a new disallowed_groups attr (#37515)

Renato Atiliod5519068970d
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 401b66d4982..b26acfbd97f 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -16,6 +16,9 @@
# The key should be as the same as possible value of default_locale.
# mandatory_values - A list of mandatory values that must be included in the setting, these cannot be changed or removed
# in the UI.
+# disallowed_groups - A list of group IDs (pipe-separated) that should be hidden from the group selector.
+# These groups cannot be selected and will be stripped if set via API.
+# Only applies to group_list type settings.
# requires_confirmation - A string that indicates if the setting requires confirmation before it can be changed.
# Only valid value here is "simple" which will display a confirmation dialog when the setting
# is changed.
@@ -449,6 +452,7 @@ basic:
default: ""
allow_any: false
refresh: true
+ disallowed_groups: "0"
area: "group_permissions"
hidden_post_visible_groups:
type: group_list
diff --git a/frontend/discourse/admin/components/site-settings/group-list.gjs b/frontend/discourse/admin/components/site-settings/group-list.gjs
index a10192021f5..b9127111a3a 100644
--- a/frontend/discourse/admin/components/site-settings/group-list.gjs
+++ b/frontend/discourse/admin/components/site-settings/group-list.gjs
@@ -10,11 +10,14 @@ export default class GroupList extends Component {
nameProperty = "name";
valueProperty = "id";
- @computed("site.groups")
+ @computed("site.groups", "setting.disallowed_groups")
get groupChoices() {
- return (this.site.groups || []).map((g) => {
- return { name: g.name, id: g.id.toString() };
- });
+ const disallowed = (this.setting?.disallowed_groups || "")
+ .split("|")
+ .filter(Boolean);
+ return (this.site.groups || [])
+ .filter((g) => !disallowed.includes(g.id.toString()))
+ .map((g) => ({ name: g.name, id: g.id.toString() }));
}
@computed("value")
diff --git a/frontend/discourse/tests/integration/components/group-list-setting-test.gjs b/frontend/discourse/tests/integration/components/group-list-setting-test.gjs
index 465fc8b0f46..912eef74b71 100644
--- a/frontend/discourse/tests/integration/components/group-list-setting-test.gjs
+++ b/frontend/discourse/tests/integration/components/group-list-setting-test.gjs
@@ -102,4 +102,58 @@ module("Integration | Component | group-list site-setting", function (hooks) {
assert.dom(".selected-content button").hasClass("disabled");
});
+
+ test("disallowed groups", async function (assert) {
+ this.site.groups = [
+ {
+ id: 0,
+ name: "everyone",
+ },
+ {
+ id: 1,
+ name: "Donuts",
+ },
+ {
+ id: 2,
+ name: "Cheese cake",
+ },
+ ];
+
+ this.set(
+ "setting",
+ SiteSetting.create({
+ category: "foo",
+ default: "",
+ description: "Choose groups",
+ placeholder: null,
+ preview: null,
+ secret: false,
+ setting: "foo_bar",
+ type: "group_list",
+ disallowed_groups: "0",
+ value: "",
+ })
+ );
+
+ await render(
+ <template><SiteSettingComponent @setting={{this.setting}} /></template>
+ );
+
+ const subject = selectKit(".list-setting");
+
+ await subject.expand();
+
+ assert.false(
+ subject.rowByValue("0").exists(),
+ "disallowed group is not in the list"
+ );
+ assert.true(
+ subject.rowByValue("1").exists(),
+ "allowed group is in the list"
+ );
+ assert.true(
+ subject.rowByValue("2").exists(),
+ "allowed group is in the list"
+ );
+ });
});
diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb
index f7dd93b2ec1..41632988f50 100644
--- a/lib/site_setting_extension.rb
+++ b/lib/site_setting_extension.rb
@@ -138,6 +138,10 @@ module SiteSettingExtension
@mandatory_values ||= {}
end
+ def disallowed_groups
+ @disallowed_groups ||= {}
+ end
+
def shadowed_settings
@shadowed_settings ||= Set.new
end
@@ -466,6 +470,7 @@ module SiteSettingExtension
secret: secret_settings.include?(s),
placeholder: placeholder(s),
mandatory_values: mandatory_values[s],
+ disallowed_groups: disallowed_groups[s],
requires_confirmation: requires_confirmation_settings[s],
upcoming_change: only_upcoming_changes ? upcoming_change_metadata[s] : nil,
themeable: themeable[s],
@@ -697,6 +702,11 @@ module SiteSettingExtension
(mandatory_values[name.to_sym].split("|") | sanitized_val.to_s.split("|")).join("|")
end
+ if disallowed_groups[name.to_sym]
+ disallowed = disallowed_groups[name.to_sym].split("|")
+ sanitized_val = sanitized_val.to_s.split("|").reject { |v| disallowed.include?(v) }.join("|")
+ end
+
provider.save(name, sanitized_val, type)
current[name] = type_supervisor.to_rb_value(name, sanitized_val)
modified[name] = current[name]
@@ -1110,6 +1120,7 @@ module SiteSettingExtension
defaults.load_setting(name, default, opts.delete(:locale_default))
mandatory_values[name] = opts[:mandatory_values] if opts[:mandatory_values]
+ disallowed_groups[name] = opts[:disallowed_groups] if opts[:disallowed_groups]
requires_confirmation_settings[name] = (
if SiteSettings::TypeSupervisor::REQUIRES_CONFIRMATION_TYPES.values.include?(
diff --git a/lib/site_settings/type_supervisor.rb b/lib/site_settings/type_supervisor.rb
index 7820b8ff517..36e623408b7 100644
--- a/lib/site_settings/type_supervisor.rb
+++ b/lib/site_settings/type_supervisor.rb
@@ -26,6 +26,7 @@ class SiteSettings::TypeSupervisor
depends_behavior
authorized_extensions
max_file_size_kb
+ disallowed_groups
].freeze
VALIDATOR_OPTS = %i[min max regex hidden regex_error json_schema schema].freeze
diff --git a/spec/lib/site_setting_extension_spec.rb b/spec/lib/site_setting_extension_spec.rb
index bbe496bab33..8f130e7247c 100644
--- a/spec/lib/site_setting_extension_spec.rb
+++ b/spec/lib/site_setting_extension_spec.rb
@@ -1300,6 +1300,24 @@ RSpec.describe SiteSettingExtension do
end
end
+ describe "disallowed_groups for group list settings" do
+ it "strips disallowed groups when setting a value" do
+ SiteSetting.whispers_allowed_groups = "0|1|2"
+ expect(SiteSetting.whispers_allowed_groups).to eq("1|2")
+
+ SiteSetting.whispers_allowed_groups = "0"
+ expect(SiteSetting.whispers_allowed_groups).to eq("")
+
+ SiteSetting.whispers_allowed_groups = "1|0|2|0"
+ expect(SiteSetting.whispers_allowed_groups).to eq("1|2")
+ end
+
+ it "is included in all_settings output" do
+ setting = SiteSetting.all_settings.find { |s| s[:setting] == :whispers_allowed_groups }
+ expect(setting[:disallowed_groups]).to eq("0")
+ end
+ end
+
describe "requires_confirmation settings" do
it "returns 'simple' for settings that require confirmation with 'simple' type" do
expect(