mirror of
https://github.com/discourse/discourse.git
synced 2024-12-17 10:03:19 +08:00
4aea12fdcb
This commit adds ability to fetch a subset of site settings from the `/admin/site_settings` endpoint so that it can be used in all places where the client app needs access to a subset of the site settings. Additionally, this commit also introduces a new service class called `UpdateSiteSetting` that encapsulates all the logic that surrounds updating a site setting so that it can be used to update site setting(s) anywhere in the backend. This service comes in handy with, for example, the controller for the flags admin config area which may need to update some site settings related to flags. Internal topic: t/130713.
66 lines
1.6 KiB
Ruby
66 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UpdateSiteSetting
|
|
include Service::Base
|
|
|
|
policy :current_user_is_admin
|
|
|
|
contract
|
|
|
|
step :convert_name_to_sym
|
|
|
|
policy :setting_is_visible
|
|
policy :setting_is_configurable
|
|
|
|
step :cleanup_value
|
|
step :save
|
|
|
|
class Contract
|
|
attribute :setting_name
|
|
attribute :new_value
|
|
attribute :allow_changing_hidden, :boolean, default: false
|
|
|
|
validates :setting_name, presence: true
|
|
end
|
|
|
|
private
|
|
|
|
def convert_name_to_sym(setting_name:)
|
|
context.setting_name = setting_name.to_sym
|
|
end
|
|
|
|
def current_user_is_admin(guardian:)
|
|
guardian.is_admin?
|
|
end
|
|
|
|
def setting_is_visible(setting_name:)
|
|
context.allow_changing_hidden || !SiteSetting.hidden_settings.include?(setting_name)
|
|
end
|
|
|
|
def setting_is_configurable(setting_name:)
|
|
return true if !SiteSetting.plugins[setting_name]
|
|
|
|
Discourse.plugins_by_name[SiteSetting.plugins[setting_name]].configurable?
|
|
end
|
|
|
|
def cleanup_value(setting_name:, new_value:)
|
|
new_value = new_value.strip if new_value.is_a?(String)
|
|
|
|
case SiteSetting.type_supervisor.get_type(setting_name)
|
|
when :integer
|
|
new_value = new_value.tr("^-0-9", "").to_i if new_value.is_a?(String)
|
|
when :file_size_restriction
|
|
new_value = new_value.tr("^0-9", "").to_i if new_value.is_a?(String)
|
|
when :uploaded_image_list
|
|
new_value = new_value.blank? ? "" : Upload.get_from_urls(new_value.split("|")).to_a
|
|
when :upload
|
|
new_value = Upload.get_from_url(new_value) || ""
|
|
end
|
|
context.new_value = new_value
|
|
end
|
|
|
|
def save(setting_name:, new_value:, guardian:)
|
|
SiteSetting.set_and_log(setting_name, new_value, guardian.user)
|
|
end
|
|
end
|