diff --git a/app/assets/stylesheets/site_settings.scss b/app/assets/stylesheets/site_settings.scss index f9a52d4d0..9cdec8029 100644 --- a/app/assets/stylesheets/site_settings.scss +++ b/app/assets/stylesheets/site_settings.scss @@ -1,4 +1,5 @@ .site-setting--value { min-height: 1em; min-width: 2em; + overflow-wrap: anywhere; } \ No newline at end of file diff --git a/app/controllers/site_settings_controller.rb b/app/controllers/site_settings_controller.rb index 7923fb86c..256b54ecc 100644 --- a/app/controllers/site_settings_controller.rb +++ b/app/controllers/site_settings_controller.rb @@ -4,6 +4,14 @@ class SiteSettingsController < ApplicationController before_action :verify_admin before_action :verify_global_admin, only: [:global] + # Checks if a given user has access to site settings on a given community + # @param [User] user user to check access for + # @param [String, nil] community_id id of the community to check access on + # @return [Boolean] + def access?(user, community_id) + community_id.present? || user.is_global_admin + end + def index # The weird argument to sort_by here sorts without throwing errors on nil values - # see https://stackoverflow.com/a/35539062/3160466. 0:1,c sorts nil last, to switch @@ -26,8 +34,41 @@ def show render json: @setting&.as_json&.merge(typed: @setting.typed) end + # Adds an audit log for a given site setting update event + # @param [User] user initiating user + # @param [SiteSetting] before current site setting + # @param [SiteSetting] after updated site setting + # @return [void] + def audit_update(user, before, after) + AuditLog.admin_audit(event_type: 'setting_update', + related: after, + user: user, + comment: "from <>\nto <>") + end + + # Deletes cache for a given site setting for a given community + # @param [SiteSetting] setting site setting to clear cache for + # @param [String, nil] community_id community id to clear cache for + # @return [Boolean] + def clear_cache(setting, community_id) + Rails.cache.delete("SiteSettings/#{community_id}/#{setting.name}", include_community: false) + end + + # Actually creates a given site setting + # @param [SiteSetting] setting site setting to create + # @param [String, nil] community_id community id to create a setting for + # @return [SiteSetting] + def do_create(setting, community_id) + SiteSetting.create(name: setting.name, + community_id: community_id, + value: '', + value_type: setting.value_type, + category: setting.category, + description: setting.description) + end + def update - if params[:community_id].blank? && !current_user.is_global_admin + unless access?(current_user, params[:community_id]) not_found return end @@ -36,20 +77,28 @@ def update matches = SiteSetting.unscoped.where(community_id: RequestContext.community_id, name: params[:name]) if matches.count.zero? global = SiteSetting.unscoped.where(community_id: nil, name: params[:name]).first - SiteSetting.create(name: global.name, community_id: RequestContext.community_id, value: '', - value_type: global.value_type, category: global.category, - description: global.description) + do_create(global, RequestContext.community_id) else matches.first end else SiteSetting.unscoped.where(community_id: nil, name: params[:name]).first end + before = @setting.attributes_print + @setting.update(setting_params) - AuditLog.admin_audit(event_type: 'setting_update', related: @setting, user: current_user, - comment: "from <>\nto <>") - Rails.cache.delete "SiteSettings/#{RequestContext.community_id}/#{@setting.name}", include_community: false + + audit_update(current_user, before, @setting) + + if @setting.global? + Community.all.each do |c| + clear_cache(@setting, c.id) + end + else + clear_cache(@setting, RequestContext.community_id) + end + render json: { status: 'OK', setting: @setting&.as_json&.merge(typed: @setting.typed) } end diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 97fa0ef75..160bbd05b 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -30,6 +30,12 @@ def self.exist?(name) SiteSetting.where(name: name).count.positive? end + # Checks whether the setting is a global site setting + # @return [Boolean] + def global? + community_id.nil? + end + def typed SettingConverter.new(value).send("as_#{value_type.downcase}") end diff --git a/app/views/site_settings/index.html.erb b/app/views/site_settings/index.html.erb index 2d5117582..9562d314c 100644 --- a/app/views/site_settings/index.html.erb +++ b/app/views/site_settings/index.html.erb @@ -17,15 +17,13 @@ <% settings.each do |setting| %> -

- <%= setting.name %> - <% if setting.community_id.nil? %> - global - <% else %> - site - <% end %> - <%= setting.value_type %> -

+ <% if setting.global? %> + global + <% else %> + site + <% end %> + <%= setting.value_type %> +

<%= setting.name %>

<%= setting.description %>