Merge pull request #3683 from riking/site-setting-validate

FEATURE: Arbitrary validations for site settings
This commit is contained in:
Régis Hanol 2015-08-21 11:37:44 +02:00
commit 4fd878d078
3 changed files with 30 additions and 4 deletions

View file

@ -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."

View file

@ -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!

View file

@ -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