0
0
Fork 0

Finish email allow/deny list naming migration (#30530)

This commit is contained in:
Matt Jankowski 2024-08-13 03:37:32 -04:00 committed by GitHub
parent f6d090fdf5
commit 02df1b4e4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 43 additions and 44 deletions

View file

@ -0,0 +1,42 @@
# 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)
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
def disallowed_through_email_domain_block?(email, ip)
EmailDomainBlock.block?(email, attempt_ip: ip)
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
end