mirror of
https://github.com/discourse/discourse.git
synced 2025-05-29 00:14:17 +08:00
Can add shadowed_by_global
to a site setting to override
If a site setting has `shadowed_by_global: true` then, if a `GlobalSetting` exists with the same name then that value will be used instead. Additionally, the setting will be hidden. This is useful in a multisite environment for private settings such as API keys. You want to set it globally, yet not display the key to customers.
This commit is contained in:
@ -46,6 +46,10 @@ module SiteSettingExtension
|
||||
@choices ||= {}
|
||||
end
|
||||
|
||||
def shadowed_settings
|
||||
@shadowed_settings ||= []
|
||||
end
|
||||
|
||||
def hidden_settings
|
||||
@hidden_settings ||= []
|
||||
end
|
||||
@ -84,6 +88,15 @@ module SiteSettingExtension
|
||||
hidden_settings << name
|
||||
end
|
||||
|
||||
# You can "shadow" a site setting with a GlobalSetting. If the GlobalSetting
|
||||
# exists it will be used instead of the setting and the setting will be hidden.
|
||||
# Useful for things like API keys on multisite.
|
||||
if opts[:shadowed_by_global] && GlobalSetting.respond_to?(name)
|
||||
hidden_settings << name
|
||||
shadowed_settings << name
|
||||
current_value = GlobalSetting.send(name)
|
||||
end
|
||||
|
||||
if opts[:refresh]
|
||||
refresh_settings << name
|
||||
end
|
||||
@ -97,7 +110,7 @@ module SiteSettingExtension
|
||||
end
|
||||
|
||||
current[name] = current_value
|
||||
setup_methods(name, current_value)
|
||||
setup_methods(name)
|
||||
end
|
||||
end
|
||||
|
||||
@ -133,7 +146,7 @@ module SiteSettingExtension
|
||||
# Retrieve all settings
|
||||
def all_settings(include_hidden=false)
|
||||
@defaults
|
||||
.reject{|s, _| hidden_settings.include?(s) || include_hidden}
|
||||
.reject{|s, _| hidden_settings.include?(s) && !include_hidden}
|
||||
.map do |s, v|
|
||||
value = send(s)
|
||||
type = types[get_data_type(s, value)]
|
||||
@ -177,9 +190,11 @@ module SiteSettingExtension
|
||||
|
||||
if deletions.length > 0 || changes.length > 0
|
||||
changes.each do |name, val|
|
||||
next if shadowed_settings.include?(name)
|
||||
current[name] = val
|
||||
end
|
||||
deletions.each do |name,val|
|
||||
next if shadowed_settings.include?(name)
|
||||
current[name] = defaults[name]
|
||||
end
|
||||
end
|
||||
@ -364,7 +379,7 @@ module SiteSettingExtension
|
||||
end
|
||||
|
||||
|
||||
def setup_methods(name, current_value)
|
||||
def setup_methods(name)
|
||||
clean_name = name.to_s.sub("?", "")
|
||||
|
||||
eval "define_singleton_method :#{clean_name} do
|
||||
|
Reference in New Issue
Block a user