From f33b79ee6e8c2b55dbf6406980c758990bcb4428 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sat, 25 Jan 2025 04:19:19 +0300 Subject: [PATCH 1/8] added SiteSetting#global? method --- app/models/site_setting.rb | 7 +++++++ app/views/site_settings/index.html.erb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 97fa0ef75..8bf789f8b 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -30,6 +30,13 @@ 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..b69c23bde 100644 --- a/app/views/site_settings/index.html.erb +++ b/app/views/site_settings/index.html.erb @@ -19,7 +19,7 @@

<%= setting.name %> - <% if setting.community_id.nil? %> + <% if setting.global? %> global <% else %> site From 77ad07bd8ce713984255db1f6c3d668a1ed1e78b Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sat, 25 Jan 2025 04:35:03 +0300 Subject: [PATCH 2/8] ensured site setting tags don't cause wrapping --- app/views/site_settings/index.html.erb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/views/site_settings/index.html.erb b/app/views/site_settings/index.html.erb index b69c23bde..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.global? %> - global - <% else %> - site - <% end %> - <%= setting.value_type %> -

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

<%= setting.name %>

<%= setting.description %>
Date: Sun, 26 Jan 2025 20:10:46 +0300 Subject: [PATCH 3/8] abstracted site setting audit log into SiteSettingsController#audit_update method --- app/controllers/site_settings_controller.rb | 22 ++++++++++++++++++--- app/models/site_setting.rb | 1 - 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/controllers/site_settings_controller.rb b/app/controllers/site_settings_controller.rb index 7923fb86c..f43184377 100644 --- a/app/controllers/site_settings_controller.rb +++ b/app/controllers/site_settings_controller.rb @@ -26,6 +26,18 @@ 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 + def update if params[:community_id].blank? && !current_user.is_global_admin not_found @@ -45,11 +57,15 @@ def update 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) + + Rails.cache.delete("SiteSettings/#{RequestContext.community_id}/#{@setting.name}", include_community: false) + 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 8bf789f8b..160bbd05b 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -32,7 +32,6 @@ def self.exist?(name) # Checks whether the setting is a global site setting # @return [Boolean] - # def global? community_id.nil? end From 4333d0f364d60ccfc060a90efe98f93720d7de2e Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 26 Jan 2025 21:06:05 +0300 Subject: [PATCH 4/8] abstracted site setting access check into SiteSettingsController#access? method --- app/controllers/site_settings_controller.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/site_settings_controller.rb b/app/controllers/site_settings_controller.rb index f43184377..af957eae7 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] 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 @@ -39,7 +47,7 @@ def audit_update(user, before, after) end def update - if params[:community_id].blank? && !current_user.is_global_admin + unless access?(current_user, params[:community_id]) not_found return end From 463f7d10597cfeb8767da48570a9c789a44790a3 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 26 Jan 2025 21:10:39 +0300 Subject: [PATCH 5/8] abstracted site setting cache clear into SiteSettingsController#clear_cache method --- app/controllers/site_settings_controller.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/site_settings_controller.rb b/app/controllers/site_settings_controller.rb index af957eae7..945950cab 100644 --- a/app/controllers/site_settings_controller.rb +++ b/app/controllers/site_settings_controller.rb @@ -6,7 +6,7 @@ class SiteSettingsController < ApplicationController # Checks if a given user has access to site settings on a given community # @param [User] user user to check access for - # @param [String] community_id id of the community to check access on + # @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 @@ -46,6 +46,14 @@ def audit_update(user, before, after) 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 [void] + def clear_cache(setting, community_id) + Rails.cache.delete("SiteSettings/#{community_id}/#{setting.name}", include_community: false) + end + def update unless access?(current_user, params[:community_id]) not_found @@ -72,7 +80,7 @@ def update audit_update(current_user, before, @setting) - Rails.cache.delete("SiteSettings/#{RequestContext.community_id}/#{@setting.name}", include_community: false) + clear_cache(@setting, RequestContext.community_id) render json: { status: 'OK', setting: @setting&.as_json&.merge(typed: @setting.typed) } end From 4eaf7af45f4380c91f6cc0db5195ae29775d94f6 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 26 Jan 2025 21:23:04 +0300 Subject: [PATCH 6/8] abstracted site setting creation into SiteSettingsController#do_create method --- app/controllers/site_settings_controller.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/site_settings_controller.rb b/app/controllers/site_settings_controller.rb index 945950cab..19236f7a4 100644 --- a/app/controllers/site_settings_controller.rb +++ b/app/controllers/site_settings_controller.rb @@ -49,11 +49,24 @@ def audit_update(user, before, after) # 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 [void] + # @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 unless access?(current_user, params[:community_id]) not_found @@ -64,9 +77,7 @@ 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 From 5ccef2d9c48fb04980986d06b6c7547674c58334 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 26 Jan 2025 21:25:35 +0300 Subject: [PATCH 7/8] fixed updating global site settings not clearing cache for communities other than the current one --- app/controllers/site_settings_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/site_settings_controller.rb b/app/controllers/site_settings_controller.rb index 19236f7a4..256b54ecc 100644 --- a/app/controllers/site_settings_controller.rb +++ b/app/controllers/site_settings_controller.rb @@ -91,7 +91,13 @@ def update audit_update(current_user, before, @setting) - clear_cache(@setting, RequestContext.community_id) + 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 From 59feb227885e3a843d95c130233dbb931e173208 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Sun, 26 Jan 2025 22:19:21 +0300 Subject: [PATCH 8/8] fixed site setting value overflowing
content --- app/assets/stylesheets/site_settings.scss | 1 + 1 file changed, 1 insertion(+) 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