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