From cdff1da901c5e649f75f9fe89e5cf17b591f049e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 9 Jun 2017 19:46:01 +0200 Subject: [PATCH] Correct validators so that existing error messages would look correct (#3668) --- app/models/account.rb | 3 ++- app/models/user.rb | 2 +- ...alidator.rb => blacklisted_email_validator.rb} | 10 +++++----- app/validators/unreserved_username_validator.rb | 15 +++++++++++++++ app/validators/unreserved_validator.rb | 15 --------------- 5 files changed, 23 insertions(+), 22 deletions(-) rename app/validators/{email_validator.rb => blacklisted_email_validator.rb} (65%) create mode 100644 app/validators/unreserved_username_validator.rb delete mode 100644 app/validators/unreserved_validator.rb diff --git a/app/models/account.rb b/app/models/account.rb index ac513713..8891adb5 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -57,7 +57,8 @@ class Account < ApplicationRecord validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: -> { !local? && username_changed? } # Local user validations - validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, unreserved: true, if: -> { local? && username_changed? } + validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, if: -> { local? && username_changed? } + validates_with UnreservedUsernameValidator, if: -> { local? && username_changed? } validates :display_name, length: { maximum: 30 }, if: -> { local? && display_name_changed? } validates :note, length: { maximum: 160 }, if: -> { local? && note_changed? } diff --git a/app/models/user.rb b/app/models/user.rb index 0fd3983b..ca11f2f5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -47,7 +47,7 @@ class User < ApplicationRecord accepts_nested_attributes_for :account validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale? - validates :email, email: true, if: :email_changed? + validates_with BlacklistedEmailValidator, if: :email_changed? scope :recent, -> { order(id: :desc) } scope :admins, -> { where(admin: true) } diff --git a/app/validators/email_validator.rb b/app/validators/blacklisted_email_validator.rb similarity index 65% rename from app/validators/email_validator.rb rename to app/validators/blacklisted_email_validator.rb index 141f209d..0ba79694 100644 --- a/app/validators/email_validator.rb +++ b/app/validators/blacklisted_email_validator.rb @@ -1,8 +1,8 @@ # 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) +class BlacklistedEmailValidator < ActiveModel::Validator + def validate(user) + user.errors.add(:email, I18n.t('users.invalid_email')) if blocked_email?(user.email) end private @@ -15,7 +15,7 @@ class EmailValidator < ActiveModel::EachValidator return false if Rails.configuration.x.email_domains_blacklist.blank? domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.') - regexp = Regexp.new("@(.+\\.)?(#{domains})", true) + regexp = Regexp.new("@(.+\\.)?(#{domains})", true) value =~ regexp end @@ -24,7 +24,7 @@ class EmailValidator < ActiveModel::EachValidator return false if Rails.configuration.x.email_domains_whitelist.blank? domains = Rails.configuration.x.email_domains_whitelist.gsub('.', '\.') - regexp = Regexp.new("@(.+\\.)?(#{domains})$", true) + regexp = Regexp.new("@(.+\\.)?(#{domains})$", true) value !~ regexp end diff --git a/app/validators/unreserved_username_validator.rb b/app/validators/unreserved_username_validator.rb new file mode 100644 index 00000000..44ea4359 --- /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 4e5b9daf..00000000 --- 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