about summary refs log tree commit diff
path: root/app/validators
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-05-27 13:20:15 +0200
committerThibaut Girka <thib@sitedethib.com>2018-05-27 13:20:15 +0200
commit659b8a12ece7110608ee84748e57647c08d1dbda (patch)
tree1d8350cdad20ad1eb8c67a4d17a18ff7183f22e5 /app/validators
parentc2e528916cae422baee3d72a3b4409a23c476b3f (diff)
parent63c7b9157274f57c496399a1a5c728b32415034c (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts:
	config/locales/ca.yml
	config/locales/nl.yml
	config/locales/oc.yml
	config/locales/pt-BR.yml

Resolved conflicts by removing upstream-specific changes
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/email_mx_validator.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/app/validators/email_mx_validator.rb b/app/validators/email_mx_validator.rb
new file mode 100644
index 000000000..d4c7cc252
--- /dev/null
+++ b/app/validators/email_mx_validator.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+require 'resolv'
+
+class EmailMxValidator < ActiveModel::Validator
+  def validate(user)
+    return if Rails.env.test?
+    user.errors.add(:email, I18n.t('users.invalid_email')) if invalid_mx?(user.email)
+  end
+
+  private
+
+  def invalid_mx?(value)
+    _, domain = value.split('@', 2)
+
+    return true if domain.nil?
+
+    records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }
+    records.empty? || on_blacklist?(records)
+  end
+
+  def on_blacklist?(values)
+    EmailDomainBlock.where(domain: values).any?
+  end
+end