1
0
mirror of https://github.com/mastodon/mastodon synced 2024-12-12 05:38:19 +09:00
mastodon/app/validators/user_email_validator.rb

43 lines
1.3 KiB
Ruby
Raw Normal View History

2016-12-05 03:07:02 +09:00
# frozen_string_literal: true
class UserEmailValidator < ActiveModel::Validator
def validate(user)
return if user.valid_invitation? || user.email.blank?
user.errors.add(:email, :blocked) if blocked_email_provider?(user.email, user.sign_up_ip)
user.errors.add(:email, :taken) if blocked_canonical_email?(user.email)
2016-12-05 03:07:02 +09:00
end
private
def blocked_email_provider?(email, ip)
disallowed_through_email_domain_block?(email, ip) || disallowed_through_configuration?(email) || not_allowed_through_configuration?(email)
end
def blocked_canonical_email?(email)
CanonicalEmailBlock.block?(email)
end
2016-12-05 03:07:02 +09:00
def disallowed_through_email_domain_block?(email, ip)
EmailDomainBlock.block?(email, attempt_ip: ip)
2016-12-05 03:07:02 +09:00
end
def not_allowed_through_configuration?(email)
return false if Rails.configuration.x.email_domains_allowlist.blank?
domains = Rails.configuration.x.email_domains_allowlist.gsub('.', '\.')
regexp = Regexp.new("@(.+\\.)?(#{domains})$", true)
email !~ regexp
end
def disallowed_through_configuration?(email)
return false if Rails.configuration.x.email_domains_denylist.blank?
domains = Rails.configuration.x.email_domains_denylist.gsub('.', '\.')
regexp = Regexp.new("@(.+\\.)?(#{domains})", true)
regexp.match?(email)
end
2016-12-05 03:07:02 +09:00
end