DEV: Add auto map from TL -> group site settings in DeprecatedSettings (#24959)

When setting an old TL based site setting in the console e.g.:

SiteSetting.min_trust_level_to_allow_ignore = TrustLevel[3]

We will silently convert this to the corresponding Group::AUTO_GROUP. And vice-versa, when we read the value on the old setting, we will automatically get the lowest trust level corresponding to the lowest auto group for the new setting in the database.
This commit is contained in:
Martin Brennan
2023-12-26 16:39:18 +10:00
committed by GitHub
parent 12131c8e21
commit 89705be722
7 changed files with 377 additions and 49 deletions

View File

@ -35,9 +35,80 @@ module SiteSettings::DeprecatedSettings
["min_trust_level_to_allow_ignore", "ignore_allowed_groups", false, "3.3"],
]
OVERRIDE_TL_GROUP_SETTINGS = %w[
anonymous_posting_min_trust_level
shared_drafts_min_trust_level
min_trust_level_for_here_mention
approve_unless_trust_level
approve_new_topics_unless_trust_level
email_in_min_trust
min_trust_to_edit_wiki_post
allow_uploaded_avatars
min_trust_to_create_topic
min_trust_to_edit_post
min_trust_to_flag_posts
tl4_delete_posts_and_topics
min_trust_level_to_allow_user_card_background
min_trust_level_to_allow_invite
min_trust_level_to_allow_ignore
]
def group_to_tl(old_setting, new_setting)
tl_and_staff = is_tl_and_staff_setting?(old_setting)
valid_auto_groups =
self.public_send("#{new_setting}_map") &
# We only want auto groups, no actual groups because they cannot be
# mapped to TLs.
Group.auto_groups_between(tl_and_staff ? :admins : :trust_level_0, :trust_level_4)
# We don't want to return nil because this could lead to permission holes;
# so we return the max available permission in this case.
return tl_and_staff ? "admin" : TrustLevel[4] if valid_auto_groups.empty?
if tl_and_staff
valid_auto_groups_excluding_staff_and_admins =
valid_auto_groups - [Group::AUTO_GROUPS[:staff], Group::AUTO_GROUPS[:admins]]
if valid_auto_groups_excluding_staff_and_admins.any?
return valid_auto_groups_excluding_staff_and_admins.min - Group::AUTO_GROUPS[:trust_level_0]
end
if valid_auto_groups.include?(Group::AUTO_GROUPS[:staff])
"staff"
elsif valid_auto_groups.include?(Group::AUTO_GROUPS[:admins])
"admin"
end
else
valid_auto_groups.min - Group::AUTO_GROUPS[:trust_level_0]
end
end
def tl_to_group(old_setting, val)
tl_and_staff = is_tl_and_staff_setting?(old_setting)
if val == "admin"
Group::AUTO_GROUPS[:admins]
elsif val == "staff"
Group::AUTO_GROUPS[:staff]
else
if tl_and_staff
"#{Group::AUTO_GROUPS[:admins]}|#{Group::AUTO_GROUPS[:staff]}|#{val.to_i + Group::AUTO_GROUPS[:trust_level_0]}"
else
"#{val.to_i + Group::AUTO_GROUPS[:trust_level_0]}"
end
end
end
def is_tl_and_staff_setting?(old_setting)
SiteSetting.type_supervisor.get_type(old_setting.to_sym) == :enum &&
SiteSetting.type_supervisor.get_enum_class(old_setting.to_sym).name ==
TrustLevelAndStaffSetting.name
end
def setup_deprecated_methods
SETTINGS.each do |old_setting, new_setting, override, version|
unless override
if !override
SiteSetting.singleton_class.public_send(
:alias_method,
:"_#{old_setting}",
@ -45,6 +116,12 @@ module SiteSettings::DeprecatedSettings
)
end
if OVERRIDE_TL_GROUP_SETTINGS.include?(old_setting)
define_singleton_method "_group_to_tl_#{old_setting}" do |warn: true|
group_to_tl(old_setting, new_setting)
end
end
define_singleton_method old_setting do |warn: true|
if warn
Discourse.deprecate(
@ -53,10 +130,14 @@ module SiteSettings::DeprecatedSettings
)
end
self.public_send(override ? new_setting : "_#{old_setting}")
if OVERRIDE_TL_GROUP_SETTINGS.include?(old_setting)
self.public_send("_group_to_tl_#{old_setting}")
else
self.public_send(override ? new_setting : "_#{old_setting}")
end
end
unless override
if !override
SiteSetting.singleton_class.public_send(
:alias_method,
:"_#{old_setting}?",
@ -75,7 +156,7 @@ module SiteSettings::DeprecatedSettings
self.public_send("#{override ? new_setting : "_" + old_setting}?")
end
unless override
if !override
SiteSetting.singleton_class.public_send(
:alias_method,
:"_#{old_setting}=",
@ -91,7 +172,14 @@ module SiteSettings::DeprecatedSettings
)
end
self.public_send("#{override ? new_setting : "_" + old_setting}=", val)
if OVERRIDE_TL_GROUP_SETTINGS.include?(old_setting)
# We want to set both the new group setting here to the equivalent of the
# TL, as well as setting the TL value in the DB so they remain in sync.
self.public_send("_#{old_setting}=", val)
self.public_send("#{new_setting}=", tl_to_group(old_setting, val).to_s)
else
self.public_send("#{override ? new_setting : "_" + old_setting}=", val)
end
end
end
end