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