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