about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorEugen <eugen@zeonfederated.com>2017-04-05 03:04:58 +0200
committerGitHub <noreply@github.com>2017-04-05 03:04:58 +0200
commit117b22e905832132367b20e2ef79e98cf9f03f17 (patch)
treebff17e40071d818d2fab57c5df7945ccdadf05cf /app/lib
parentd3dab68978103e304d38589d154b34ef890e0715 (diff)
parente9a6da6bc739f4f68447f56b93810762da388ce8 (diff)
Merge pull request #852 from peterkeen/email-whitelist-817
[#817] Add email whitelist
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/email_validator.rb17
1 files changed, 15 insertions, 2 deletions
diff --git a/app/lib/email_validator.rb b/app/lib/email_validator.rb
index 856b8b1f7..06e9375f6 100644
--- a/app/lib/email_validator.rb
+++ b/app/lib/email_validator.rb
@@ -2,17 +2,30 @@
 
 class EmailValidator < ActiveModel::EachValidator
   def validate_each(record, attribute, value)
-    return if Rails.configuration.x.email_domains_blacklist.empty?
-
     record.errors.add(attribute, I18n.t('users.invalid_email')) if blocked_email?(value)
   end
 
   private
 
   def blocked_email?(value)
+    on_blacklist?(value) || not_on_whitelist?(value)
+  end
+
+  def on_blacklist?(value)
+    return false if Rails.configuration.x.email_domains_blacklist.blank?
+
     domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.')
     regexp  = Regexp.new("@(.+\\.)?(#{domains})", true)
 
     value =~ regexp
   end
+
+  def not_on_whitelist?(value)
+    return false if Rails.configuration.x.email_domains_whitelist.blank?
+
+    domains = Rails.configuration.x.email_domains_whitelist.gsub('.', '\.')
+    regexp  = Regexp.new("@(.+\\.)?(#{domains})", true)
+
+    value !~ regexp
+  end
 end