Finish email allow/deny list naming migration (#30530)
This commit is contained in:
parent
f6d090fdf5
commit
02df1b4e4a
11 changed files with 43 additions and 44 deletions
42
app/validators/user_email_validator.rb
Normal file
42
app/validators/user_email_validator.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue