From cdff1da901c5e649f75f9fe89e5cf17b591f049e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 9 Jun 2017 19:46:01 +0200 Subject: Correct validators so that existing error messages would look correct (#3668) --- app/validators/blacklisted_email_validator.rb | 31 +++++++++++++++++++++++++ app/validators/email_validator.rb | 31 ------------------------- app/validators/unreserved_username_validator.rb | 15 ++++++++++++ app/validators/unreserved_validator.rb | 15 ------------ 4 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 app/validators/blacklisted_email_validator.rb delete mode 100644 app/validators/email_validator.rb create mode 100644 app/validators/unreserved_username_validator.rb delete mode 100644 app/validators/unreserved_validator.rb (limited to 'app/validators') diff --git a/app/validators/blacklisted_email_validator.rb b/app/validators/blacklisted_email_validator.rb new file mode 100644 index 000000000..0ba79694b --- /dev/null +++ b/app/validators/blacklisted_email_validator.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class BlacklistedEmailValidator < ActiveModel::Validator + def validate(user) + user.errors.add(:email, I18n.t('users.invalid_email')) if blocked_email?(user.email) + end + + private + + def blocked_email?(value) + on_blacklist?(value) || not_on_whitelist?(value) + end + + def on_blacklist?(value) + return false if Rails.configuration.x.email_domains_blacklist.blank? + + domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.') + regexp = Regexp.new("@(.+\\.)?(#{domains})", true) + + value =~ regexp + end + + def not_on_whitelist?(value) + return false if Rails.configuration.x.email_domains_whitelist.blank? + + domains = Rails.configuration.x.email_domains_whitelist.gsub('.', '\.') + regexp = Regexp.new("@(.+\\.)?(#{domains})$", true) + + value !~ regexp + end +end diff --git a/app/validators/email_validator.rb b/app/validators/email_validator.rb deleted file mode 100644 index 141f209d5..000000000 --- a/app/validators/email_validator.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -class EmailValidator < ActiveModel::EachValidator - def validate_each(record, attribute, value) - record.errors.add(attribute, I18n.t('users.invalid_email')) if blocked_email?(value) - end - - private - - def blocked_email?(value) - on_blacklist?(value) || not_on_whitelist?(value) - end - - def on_blacklist?(value) - return false if Rails.configuration.x.email_domains_blacklist.blank? - - domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.') - regexp = Regexp.new("@(.+\\.)?(#{domains})", true) - - value =~ regexp - end - - def not_on_whitelist?(value) - return false if Rails.configuration.x.email_domains_whitelist.blank? - - domains = Rails.configuration.x.email_domains_whitelist.gsub('.', '\.') - regexp = Regexp.new("@(.+\\.)?(#{domains})$", true) - - value !~ regexp - end -end diff --git a/app/validators/unreserved_username_validator.rb b/app/validators/unreserved_username_validator.rb new file mode 100644 index 000000000..44ea4359b --- /dev/null +++ b/app/validators/unreserved_username_validator.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class UnreservedUsernameValidator < ActiveModel::Validator + def validate(account) + return if account.username.nil? + account.errors.add(:username, I18n.t('accounts.reserved_username')) if reserved_username?(account.username) + end + + private + + def reserved_username?(value) + return false unless Setting.reserved_usernames + Setting.reserved_usernames.include?(value.downcase) + end +end diff --git a/app/validators/unreserved_validator.rb b/app/validators/unreserved_validator.rb deleted file mode 100644 index 4e5b9dafc..000000000 --- a/app/validators/unreserved_validator.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -class UnreservedValidator < ActiveModel::EachValidator - def validate_each(record, attribute, value) - return if value.nil? - record.errors.add(attribute, I18n.t('accounts.reserved_username')) if reserved_username?(value) - end - - private - - def reserved_username?(value) - return false unless Setting.reserved_usernames - Setting.reserved_usernames.include?(value.downcase) - end -end -- cgit