From 2363897a258fdc886b3c55384d125696b6cd57e9 Mon Sep 17 00:00:00 2001 From: Kane York Date: Thu, 20 Aug 2015 18:27:19 -0700 Subject: [PATCH] FEATURE: Arbitrary validations for site settings --- config/locales/server.en.yml | 11 +++++++---- lib/site_setting_extension.rb | 6 ++++++ lib/site_setting_validations.rb | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 lib/site_setting_validations.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 429fe03dfee..dba03554a76 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -90,6 +90,11 @@ en: other: ! '%{count} errors prohibited this %{model} from being saved' embed: load_from_remote: "There was an error loading that post." + site_settings: + min_username_length_exists: "You cannot set the minimum username length above the shortest username." + min_username_length_range: "You cannot set the minimum above the maximum." + max_username_length_exists: "You cannot set the maximum username length below the longest username." + max_username_length_range: "You cannot set the maximum above the minimum." activemodel: errors: @@ -892,14 +897,12 @@ en: invite_expiry_days: "How long user invitation keys are valid, in days" invite_passthrough_hours: "How long a user can use a previously redeemed invitation key to log in, in hours" - # TODO: perhaps we need a way of protecting these settings for hosted solution, global settings ... - invite_only: "Public registration is disabled, all new users must be explicitly invited by other members or staff." login_required: "Require authentication to read content on this site, disallow anonymous access." - min_username_length: "Minimum username length in characters. WARNING: ANY EXISTING USERS WITH NAMES SHORTER THAN THIS WILL BE UNABLE TO ACCESS THE SITE." - max_username_length: "Maximum username length in characters. WARNING: ANY EXISTING USERS WITH NAMES LONGER THAN THIS WILL BE UNABLE TO ACCESS THE SITE." + min_username_length: "Minimum username length in characters." + max_username_length: "Maximum username length in characters." reserved_usernames: "Usernames for which signup is not allowed." diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 228a7e6f395..f4c7c1df20a 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -1,7 +1,9 @@ require_dependency 'enum' require_dependency 'site_settings/db_provider' +require 'site_setting_validations' module SiteSettingExtension + include SiteSettingValidations # For plugins, so they can tell if a feature is supported def supported_types @@ -303,6 +305,10 @@ module SiteSettingExtension end end + if self.respond_to? "validate_#{name}" + send("validate_#{name}", val) + end + provider.save(name, val, type) current[name] = convert(val, type) clear_cache! diff --git a/lib/site_setting_validations.rb b/lib/site_setting_validations.rb new file mode 100644 index 00000000000..6ed44df8037 --- /dev/null +++ b/lib/site_setting_validations.rb @@ -0,0 +1,17 @@ + +module SiteSettingValidations + + def validate_error(key) + raise Discourse::InvalidParameters.new(I18n.t("errors.site_settings.#{key}")) + end + + def validate_min_username_length(new_val) + validate_error :min_username_length_range if new_val > SiteSetting.max_username_length + validate_error :min_username_length_exists if User.where('length(username) < ?', new_val).exists? + end + + def validate_max_username_length(new_val) + validate_error :min_username_length_range if new_val < SiteSetting.min_username_length + validate_error :max_username_length_exists if User.where('length(username) > ?', new_val).exists? + end +end