2016-12-05 05:07:02 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-08-13 17:37:32 +10:00
|
|
|
class UserEmailValidator < ActiveModel::Validator
|
2017-06-10 03:46:01 +10:00
|
|
|
def validate(user)
|
2021-03-01 14:59:13 +11:00
|
|
|
return if user.valid_invitation? || user.email.blank?
|
2019-05-04 07:44:44 +10:00
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
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 05:07:02 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
def blocked_email_provider?(email, ip)
|
|
|
|
disallowed_through_email_domain_block?(email, ip) || disallowed_through_configuration?(email) || not_allowed_through_configuration?(email)
|
2017-04-05 01:04:44 +10:00
|
|
|
end
|
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
def blocked_canonical_email?(email)
|
|
|
|
CanonicalEmailBlock.block?(email)
|
2021-04-17 11:14:25 +10:00
|
|
|
end
|
2016-12-05 05:07:02 +11:00
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
def disallowed_through_email_domain_block?(email, ip)
|
|
|
|
EmailDomainBlock.block?(email, attempt_ip: ip)
|
2016-12-05 05:07:02 +11:00
|
|
|
end
|
2017-04-05 01:04:44 +10:00
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
def not_allowed_through_configuration?(email)
|
2024-08-13 17:37:32 +10:00
|
|
|
return false if Rails.configuration.x.email_domains_allowlist.blank?
|
2017-04-05 01:04:44 +10:00
|
|
|
|
2024-08-13 17:37:32 +10:00
|
|
|
domains = Rails.configuration.x.email_domains_allowlist.gsub('.', '\.')
|
2017-06-10 03:46:01 +10:00
|
|
|
regexp = Regexp.new("@(.+\\.)?(#{domains})$", true)
|
2017-04-05 01:04:44 +10:00
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
email !~ regexp
|
2017-04-05 01:04:44 +10:00
|
|
|
end
|
2021-04-17 11:14:25 +10:00
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
def disallowed_through_configuration?(email)
|
2024-08-13 17:37:32 +10:00
|
|
|
return false if Rails.configuration.x.email_domains_denylist.blank?
|
2021-04-17 11:14:25 +10:00
|
|
|
|
2024-08-13 17:37:32 +10:00
|
|
|
domains = Rails.configuration.x.email_domains_denylist.gsub('.', '\.')
|
2021-04-17 11:14:25 +10:00
|
|
|
regexp = Regexp.new("@(.+\\.)?(#{domains})", true)
|
|
|
|
|
2022-02-25 03:28:23 +11:00
|
|
|
regexp.match?(email)
|
2021-04-17 11:14:25 +10:00
|
|
|
end
|
2016-12-05 05:07:02 +11:00
|
|
|
end
|