Skip to content

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

FIX: Store the site contact group by id (#42771)

Régis Hanol6c7ef4dfec92
diff --git a/app/models/group.rb b/app/models/group.rb
index ec0919f99c7..4154bf19767 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -776,6 +776,14 @@ class Group < ActiveRecord::Base
relation
end
+ def self.find_by_id_or_name(value)
+ value = value.to_s.strip
+ return if value.blank?
+ return find_by(id: value) if value.match?(/\A\d+\z/)
+
+ find_by("lower(name) = ?", value.downcase)
+ end
+
def self.lookup_group(name)
if id = AUTO_GROUPS[name]
Group.find_by(id: id)
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 8c80b7f8f5d..2b862cc35aa 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2180,7 +2180,7 @@ en:
share_links: "Determine which items appear on the share dialog, and in what order."
allow_username_in_share_links: "Allow usernames to be included in share links. This is useful to reward badges based on unique visitors."
site_contact_username: "A valid staff username to send all automated messages from. If left blank the default System account will be used."
- site_contact_group_name: "A valid name of a group that gets invited to all automatically sent personal messages."
+ site_contact_group_name: "The group that gets invited to all automatically sent personal messages."
send_welcome_message: "Send all new users a welcome message with a quick start guide."
send_tl1_welcome_message: "Send new trust level 1 users a welcome message."
send_tl2_promotion_message: "Send new trust level 2 users a message about promotion."
@@ -3077,7 +3077,7 @@ en:
invalid_email: "Invalid email address."
invalid_username: "There's no user with that username."
valid_username: "There's a user with that username."
- invalid_group: "There's no group with that name."
+ invalid_group: "There's no such group."
invalid_svg: "The SVG image is invalid. It must contain an SVG element and no scripts or event handlers."
invalid_upload: "The upload is invalid or does not exist."
invalid_topic: "There's no topic with that ID."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 043df167ab2..32f5af68192 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -88,7 +88,7 @@
# are allowed based on authorized_extensions setting.
# - max_file_size_kb: maximum file size in KB. If not specified,
# uses max_image_size_kb or max_attachment_size_kb.
-# type: group - A single group ID.
+# type: group - A single group ID. A name is converted to its ID on write.
# type: group_list - A list of group IDs.
# type: tag_list - A list of tag names.
# type: emoji_list - A list of emoji names (e.g. joy, cold_hands)
diff --git a/db/post_migrate/20260820092502_convert_site_contact_group_name_to_id.rb b/db/post_migrate/20260820092502_convert_site_contact_group_name_to_id.rb
new file mode 100644
index 00000000000..bd423bb17e2
--- /dev/null
+++ b/db/post_migrate/20260820092502_convert_site_contact_group_name_to_id.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class ConvertSiteContactGroupNameToId < ActiveRecord::Migration[8.0]
+ def up
+ execute <<~SQL
+ UPDATE site_settings
+ SET value = groups.id::text, updated_at = NOW()
+ FROM groups
+ WHERE site_settings.name = 'site_contact_group_name'
+ AND lower(site_settings.value) = lower(groups.name)
+ AND site_settings.value NOT IN (SELECT id::text FROM groups)
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index 9676afd53d0..d49e6c5d827 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -24360,6 +24360,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20260821164114'),
('20260820171539'),
('20260820143851'),
+('20260820092502'),
('20260819131113'),
('20260818143417'),
('20260818081537'),
diff --git a/frontend/discourse/admin/components/admin-config-area-cards/about/contact-information.gjs b/frontend/discourse/admin/components/admin-config-area-cards/about/contact-information.gjs
index 6ae752e58b7..50ee17309fe 100644
--- a/frontend/discourse/admin/components/admin-config-area-cards/about/contact-information.gjs
+++ b/frontend/discourse/admin/components/admin-config-area-cards/about/contact-information.gjs
@@ -1,5 +1,5 @@
import Component from "@glimmer/component";
-import { cached, tracked } from "@glimmer/tracking";
+import { cached } from "@glimmer/tracking";
import { hash } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
@@ -14,11 +14,6 @@ export default class AdminConfigAreasAboutContactInformation extends Component {
@service site;
@service toasts;
- @tracked
- contactGroupId = this.site.groups.find(
- (group) => group.name === this.data.contactGroupName
- )?.id;
-
@cached
get data() {
return {
@@ -28,7 +23,10 @@ export default class AdminConfigAreasAboutContactInformation extends Component {
),
contactEmail: this.args.contactInformation.contactEmail.value,
contactURL: this.args.contactInformation.contactURL.value,
- contactGroupName: this.args.contactInformation.contactGroupName.value,
+ contactGroupName:
+ this.site.groupsById[
+ this.args.contactInformation.contactGroupName.value
+ ]?.id ?? null,
contactUsername:
this.args.contactInformation.contactUsername.value || null,
};
@@ -41,8 +39,7 @@ export default class AdminConfigAreasAboutContactInformation extends Component {
@action
setContactGroup(groupIds, { set }) {
- this.contactGroupId = groupIds[0];
- set("contactGroupName", this.site.groupsById[groupIds[0]]?.name);
+ set("contactGroupName", groupIds[0] ?? null);
}
@action
@@ -184,7 +181,7 @@ export default class AdminConfigAreasAboutContactInformation extends Component {
<field.Control>
<GroupChooser
@content={{this.site.groups}}
- @value={{this.contactGroupId}}
+ @value={{field.value}}
@options={{hash maximum=1}}
@onChange={{field.set}}
/>
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 26b07c8ac7d..62c0e6c8f42 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -989,6 +989,11 @@ module Discourse
user ||= system_user || User.admins.real.order(:id).first
end
+ # A global override bypasses the write-time name-to-id conversion.
+ def self.site_contact_group
+ Group.find_by_id_or_name(SiteSetting.site_contact_group_name)
+ end
+
SYSTEM_USER_ID = -1
def self.system_user
diff --git a/lib/site_settings/type_supervisor.rb b/lib/site_settings/type_supervisor.rb
index 20c15d6c202..2211265858b 100644
--- a/lib/site_settings/type_supervisor.rb
+++ b/lib/site_settings/type_supervisor.rb
@@ -358,6 +358,8 @@ class SiteSettings::TypeSupervisor
val = val.is_a?(String) ? val : val.map(&:id).join("|")
elsif type == self.class.types[:upload] && val.present?
val = val.is_a?(Integer) ? val : val.id
+ elsif type == self.class.types[:group] && val.present?
+ val = (Group.find_by_id_or_name(val)&.id || val).to_s
elsif type == self.class.types[:objects] && val.present?
begin
objects = val.is_a?(String) ? JSON.parse(val) : val
diff --git a/lib/system_message.rb b/lib/system_message.rb
index 823f4f261a6..dd0557ba9c5 100644
--- a/lib/system_message.rb
+++ b/lib/system_message.rb
@@ -43,14 +43,7 @@ class SystemMessage
user = Discourse.system_user
else
user = Discourse.site_contact_user
- target_group_names =
- (
- if Group.exists?(name: SiteSetting.site_contact_group_name)
- SiteSetting.site_contact_group_name
- else
- nil
- end
- )
+ target_group_names = Discourse.site_contact_group&.name
end
post_creator_args = [
diff --git a/lib/validators/group_setting_validator.rb b/lib/validators/group_setting_validator.rb
index c17032448c6..f027813a8c5 100644
--- a/lib/validators/group_setting_validator.rb
+++ b/lib/validators/group_setting_validator.rb
@@ -7,9 +7,9 @@ class GroupSettingValidator
def valid_value?(val)
return true if val.blank?
+ return false if !val.to_s.match?(/\A\d+\z/)
- group_id = Integer(val, exception: false)
- (group_id.present? && Group.exists?(id: group_id)) || Group.exists?(name: val)
+ Group.exists?(id: val)
end
def error_message
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
index 67f5a564a28..ffd935df4b3 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
@@ -864,7 +864,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
recipient = Fabricate(:user)
group = Fabricate(:group)
group.add(Fabricate(:user))
- SiteSetting.site_contact_group_name = "#{group.name}"
+ SiteSetting.site_contact_group_name = group.id.to_s
SystemMessage.new(recipient).create("tl2_promotion_message", {})
diff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rb
index e6736321d65..202f9ec00aa 100644
--- a/spec/lib/discourse_spec.rb
+++ b/spec/lib/discourse_spec.rb
@@ -282,6 +282,36 @@ RSpec.describe Discourse do
end
end
+ describe "#site_contact_group" do
+ fab!(:group) { Fabricate(:group, name: "support") }
+
+ it "returns nothing when the setting is blank" do
+ SiteSetting.site_contact_group_name = ""
+ expect(Discourse.site_contact_group).to eq(nil)
+ end
+
+ it "resolves the stored group id" do
+ SiteSetting.site_contact_group_name = group.id.to_s
+ expect(Discourse.site_contact_group).to eq(group)
+ end
+
+ it "returns nothing when the group no longer exists" do
+ SiteSetting.site_contact_group_name = group.id.to_s
+ group.destroy!
+ expect(Discourse.site_contact_group).to eq(nil)
+ end
+
+ it "resolves a group name regardless of case" do
+ SiteSetting.stubs(:site_contact_group_name).returns("SUPPORT")
+ expect(Discourse.site_contact_group).to eq(group)
+ end
+
+ it "does not read a group id out of a value that merely starts with a digit" do
+ SiteSetting.stubs(:site_contact_group_name).returns("0support")
+ expect(Discourse.site_contact_group).to eq(nil)
+ end
+ end
+
describe "#system_user" do
it "returns the system user" do
expect(Discourse.system_user.id).to eq(-1)
diff --git a/spec/lib/site_settings/type_supervisor_spec.rb b/spec/lib/site_settings/type_supervisor_spec.rb
index f9728497c6c..36b3f293fd1 100644
--- a/spec/lib/site_settings/type_supervisor_spec.rb
+++ b/spec/lib/site_settings/type_supervisor_spec.rb
@@ -188,6 +188,7 @@ RSpec.describe SiteSettings::TypeSupervisor do
settings.setting(:type_mock_validate_method, "no_value")
settings.setting(:type_custom, "custom", type: "list")
settings.setting(:type_upload, "", type: "upload")
+ settings.setting(:type_group, "", type: "group")
settings.setting(
:type_json_schema,
"[{\"name\":\"Brett\"}]",
@@ -214,6 +215,34 @@ RSpec.describe SiteSettings::TypeSupervisor do
let(:true_val) { "t" }
let(:false_val) { "f" }
+ it "converts a group name to its id" do
+ group = Fabricate(:group, name: "Support")
+
+ expect(settings.type_supervisor.to_db_value(:type_group, "Support")).to eq [
+ group.id.to_s,
+ SiteSetting.types[:group],
+ ]
+ expect(settings.type_supervisor.to_db_value(:type_group, "support")).to eq [
+ group.id.to_s,
+ SiteSetting.types[:group],
+ ]
+ end
+
+ it "stores an integer group id as a string" do
+ group = Fabricate(:group)
+
+ expect(settings.type_supervisor.to_db_value(:type_group, group.id)).to eq [
+ group.id.to_s,
+ SiteSetting.types[:group],
+ ]
+ end
+
+ it "rejects a name that matches no group" do
+ expect { settings.type_supervisor.to_db_value(:type_group, "nope") }.to raise_error(
+ Discourse::InvalidParameters,
+ )
+ end
+
it "gives a second chance to guess even told :null type" do
expect(settings.type_supervisor.to_db_value(:type_null, 1)).to eq [
1,
diff --git a/spec/lib/system_message_spec.rb b/spec/lib/system_message_spec.rb
index 575fcc5f011..53fedd0f181 100644
--- a/spec/lib/system_message_spec.rb
+++ b/spec/lib/system_message_spec.rb
@@ -62,16 +62,12 @@ RSpec.describe SystemMessage do
expect(post.raw).to eq("override body")
end
- it "should allow site_contact_group_name" do
+ it "invites the site contact group" do
group = Fabricate(:group)
- SiteSetting.site_contact_group_name = group.name
+ SiteSetting.site_contact_group_name = group.id.to_s
post = SystemMessage.create(user, :welcome_invite)
expect(post.topic.allowed_groups).to contain_exactly(group)
-
- group.update!(name: "anewname")
- post = SystemMessage.create(user, :welcome_invite)
- expect(post.topic.allowed_groups).to eq([])
end
it "sends event with post object" do
diff --git a/spec/lib/validators/at_least_one_group_validator_spec.rb b/spec/lib/validators/at_least_one_group_validator_spec.rb
index 00cf3c0c81d..24685b98095 100644
--- a/spec/lib/validators/at_least_one_group_validator_spec.rb
+++ b/spec/lib/validators/at_least_one_group_validator_spec.rb
@@ -31,7 +31,7 @@ RSpec.describe AtLeastOneGroupValidator do
context "when one of the groups doesn't exist" do
before { validator.valid_value?("10|1337") }
- it { expect(validator.error_message).to eq("There's no group with that name.") }
+ it { expect(validator.error_message).to eq("There's no such group.") }
end
end
end
diff --git a/spec/lib/validators/group_setting_validator_spec.rb b/spec/lib/validators/group_setting_validator_spec.rb
index 97d7d0b790d..e74ecf32ab3 100644
--- a/spec/lib/validators/group_setting_validator_spec.rb
+++ b/spec/lib/validators/group_setting_validator_spec.rb
@@ -15,16 +15,16 @@ RSpec.describe GroupSettingValidator do
expect(validator.valid_value?(group.id.to_s)).to eq(true)
end
- it "returns true if value matches an existing group name" do
- expect(validator.valid_value?(group.name)).to eq(true)
+ it "returns false if value matches a group name rather than an id" do
+ expect(validator.valid_value?(group.name)).to eq(false)
end
it "returns false if value does not match a group id" do
- expect(validator.valid_value?("-9999")).to eq(false)
+ expect(validator.valid_value?("999999999")).to eq(false)
end
- it "returns false for a non-numeric value that does not match a group name" do
- expect(validator.valid_value?("notagroup")).to eq(false)
+ it "does not read a group id out of a value that merely starts with a digit" do
+ expect(validator.valid_value?("0#{group.name}")).to eq(false)
end
end
end
diff --git a/spec/migrations/convert_site_contact_group_name_to_id_spec.rb b/spec/migrations/convert_site_contact_group_name_to_id_spec.rb
new file mode 100644
index 00000000000..687fd77f820
--- /dev/null
+++ b/spec/migrations/convert_site_contact_group_name_to_id_spec.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+require Rails.root.join("db/post_migrate/20260820092502_convert_site_contact_group_name_to_id.rb")
+
+RSpec.describe ConvertSiteContactGroupNameToId do
+ subject(:migrate) { described_class.new.up }
+
+ before do
+ @verbose = ActiveRecord::Migration.verbose
+ ActiveRecord::Migration.verbose = false
+ end
+
+ after { ActiveRecord::Migration.verbose = @verbose }
+
+ def store(value)
+ SiteSetting.create!(
+ name: "site_contact_group_name",
+ value:,
+ data_type: SiteSetting.types[:group],
+ )
+ end
+
+ def stored
+ SiteSetting.find_by(name: "site_contact_group_name").value
+ end
+
+ it "converts a group name to its id regardless of case" do
+ group = Fabricate(:group, name: "support")
+ store("SUPPORT")
+
+ migrate
+
+ expect(stored).to eq(group.id.to_s)
+ end
+
+ it "leaves an id alone even when a group is named after it" do
+ group = Fabricate(:group, id: 12_345)
+ Fabricate(:group, name: group.id.to_s)
+ store(group.id.to_s)
+
+ migrate
+
+ expect(stored).to eq(group.id.to_s)
+ end
+end
diff --git a/spec/models/reviewable_queued_post_spec.rb b/spec/models/reviewable_queued_post_spec.rb
index fed80821669..0b6dfcbe557 100644
--- a/spec/models/reviewable_queued_post_spec.rb
+++ b/spec/models/reviewable_queued_post_spec.rb
@@ -149,7 +149,7 @@ RSpec.describe ReviewableQueuedPost, type: :model do
fab!(:contact_user, :user)
before do
- SiteSetting.site_contact_group_name = contact_group.name
+ SiteSetting.site_contact_group_name = contact_group.id.to_s
SiteSetting.site_contact_username = contact_user.username
end
diff --git a/spec/system/admin_about_config_area_spec.rb b/spec/system/admin_about_config_area_spec.rb
index 7b441c9b59b..106a15bfde9 100644
--- a/spec/system/admin_about_config_area_spec.rb
+++ b/spec/system/admin_about_config_area_spec.rb
@@ -25,7 +25,7 @@ describe "Admin About Config Area Page" do
SiteSetting.contact_email = "[email protected]"
SiteSetting.contact_url = "https://hello.com"
SiteSetting.site_contact_username = admin.username
- SiteSetting.site_contact_group_name = admin.groups.first.name
+ SiteSetting.site_contact_group_name = admin.groups.first.id.to_s
SiteSetting.company_name = "kitty company inc."
SiteSetting.company_url = "https://kitty.company"
@@ -219,7 +219,7 @@ describe "Admin About Config Area Page" do
expect(SiteSetting.contact_email).to eq("[email protected]")
expect(SiteSetting.contact_url).to eq("https://website.owner.com/blah")
expect(SiteSetting.site_contact_username).to eq(admin.username)
- expect(SiteSetting.site_contact_group_name).to eq(group.name)
+ expect(SiteSetting.site_contact_group_name).to eq(group.id.to_s)
end
end
diff --git a/spec/system/reviewables_spec.rb b/spec/system/reviewables_spec.rb
index 0cdc6309ee8..2f024da13cf 100644
--- a/spec/system/reviewables_spec.rb
+++ b/spec/system/reviewables_spec.rb
@@ -203,7 +203,7 @@ describe "Reviewables" do
fab!(:contact_user, :user)
before do
- SiteSetting.site_contact_group_name = contact_group.name
+ SiteSetting.site_contact_group_name = contact_group.id.to_s
SiteSetting.site_contact_username = contact_user.username
end