闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
850 B

  1. # frozen_string_literal: true
  2. class EmailValidator < ActiveModel::EachValidator
  3. def validate_each(record, attribute, value)
  4. record.errors.add(attribute, I18n.t('users.invalid_email')) if blocked_email?(value)
  5. end
  6. private
  7. def blocked_email?(value)
  8. on_blacklist?(value) || not_on_whitelist?(value)
  9. end
  10. def on_blacklist?(value)
  11. return false if Rails.configuration.x.email_domains_blacklist.blank?
  12. domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.')
  13. regexp = Regexp.new("@(.+\\.)?(#{domains})", true)
  14. value =~ regexp
  15. end
  16. def not_on_whitelist?(value)
  17. return false if Rails.configuration.x.email_domains_whitelist.blank?
  18. domains = Rails.configuration.x.email_domains_whitelist.gsub('.', '\.')
  19. regexp = Regexp.new("@(.+\\.)?(#{domains})$", true)
  20. value !~ regexp
  21. end
  22. end