summaryrefslogtreecommitdiff
path: root/app/models/user.rb
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2015-05-02 06:53:32 -0700
committerStan Hu <stanhu@gmail.com>2015-05-02 09:36:52 -0700
commiteb4f1eb5f55fe3630c9191db1b9da2dc92437391 (patch)
tree6aab55f8ab6f46e3126fff4f4e7ae36712cec0f7 /app/models/user.rb
parentcfbff017d0ba1ba4fd896b4762b23853f123c60a (diff)
downloadgitlab-ce-eb4f1eb5f55fe3630c9191db1b9da2dc92437391.tar.gz
Add application setting to restrict user signups to e-mail domains
This feature was requested long ago: http://feedback.gitlab.com/forums/176466-general/suggestions/4118466-ability-to-register-only-from-ceratain-domains This MR is based off !253 but changed to use application settings and use wildcard strings to give more flexibility in pattern matching. Regexps seemed overkill and easy to get wrong. Only restrict e-mail addresses upon creation
Diffstat (limited to 'app/models/user.rb')
-rw-r--r--app/models/user.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index d6b93afe739..f22fdc28435 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -142,6 +142,7 @@ class User < ActiveRecord::Base
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
before_validation :generate_password, on: :create
+ before_validation :restricted_signup_domains, on: :create
before_validation :sanitize_attrs
before_validation :set_notification_email, if: ->(user) { user.email_changed? }
before_validation :set_public_email, if: ->(user) { user.public_email_changed? }
@@ -611,4 +612,27 @@ class User < ActiveRecord::Base
select(:project_id).
uniq.map(&:project_id)
end
+
+ def restricted_signup_domains
+ email_domains = current_application_settings.restricted_signup_domains
+
+ unless email_domains.blank?
+ match_found = email_domains.any? do |domain|
+ escaped = Regexp.escape(domain).gsub('\*','.*?')
+ regexp = Regexp.new "^#{escaped}$", Regexp::IGNORECASE
+ email_domain = Mail::Address.new(self.email).domain
+ email_domain =~ regexp
+ end
+
+ unless match_found
+ self.errors.add :email,
+ 'is not whitelisted. ' +
+ 'Email domains valid for registration are: ' +
+ email_domains.join(', ')
+ return false
+ end
+ end
+
+ true
+ end
end