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