diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-24 09:09:25 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-24 09:09:25 +0000 |
commit | 6f7881ee9dcec34141a8f34fc814b56b366d2b48 (patch) | |
tree | 25f72a06874b32b1049b79a9d7f4f1b7bca43b9b /app/services/users | |
parent | 8c8bf44fa64f98114f7439f751c92d59a44b3218 (diff) | |
download | gitlab-ce-6f7881ee9dcec34141a8f34fc814b56b366d2b48.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/users')
-rw-r--r-- | app/services/users/build_service.rb | 2 | ||||
-rw-r--r-- | app/services/users/update_canonical_email_service.rb | 56 | ||||
-rw-r--r-- | app/services/users/update_service.rb | 7 |
3 files changed, 65 insertions, 0 deletions
diff --git a/app/services/users/build_service.rb b/app/services/users/build_service.rb index 4c3ae2d204d..6f9f307c322 100644 --- a/app/services/users/build_service.rb +++ b/app/services/users/build_service.rb @@ -30,6 +30,8 @@ module Users build_identity(user) + Users::UpdateCanonicalEmailService.new(user: user).execute + user end diff --git a/app/services/users/update_canonical_email_service.rb b/app/services/users/update_canonical_email_service.rb new file mode 100644 index 00000000000..1400fd58eb4 --- /dev/null +++ b/app/services/users/update_canonical_email_service.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module Users + class UpdateCanonicalEmailService + extend ActiveSupport::Concern + + INCLUDED_DOMAINS_PATTERN = [/gmail.com/].freeze + + def initialize(user:) + raise ArgumentError.new("Please provide a user") unless user&.is_a?(User) + + @user = user + end + + def execute + return unless user.email + return unless user.email.match? Devise.email_regexp + + canonical_email = canonicalize_email + + unless canonical_email + # the canonical email doesn't exist, probably because the domain doesn't match + # destroy any UserCanonicalEmail record associated with this user + user.user_canonical_email&.delete + # nothing else to do here + return + end + + if user.user_canonical_email + # update to the new value + user.user_canonical_email.canonical_email = canonical_email + else + user.build_user_canonical_email(canonical_email: canonical_email) + end + end + + private + + attr_reader :user + + def canonicalize_email + email = user.email + + portions = email.split('@') + username = portions.shift + rest = portions.join + + regex = Regexp.union(INCLUDED_DOMAINS_PATTERN) + return unless regex.match?(rest) + + no_dots = username.tr('.', '') + before_plus = no_dots.split('+')[0] + "#{before_plus}@#{rest}" + end + end +end diff --git a/app/services/users/update_service.rb b/app/services/users/update_service.rb index 57209043e3b..f0e9f2b7656 100644 --- a/app/services/users/update_service.rb +++ b/app/services/users/update_service.rb @@ -21,6 +21,7 @@ module Users discard_read_only_attributes assign_attributes assign_identity + build_canonical_email if @user.save(validate: validate) && update_status notify_success(user_exists) @@ -40,6 +41,12 @@ module Users private + def build_canonical_email + return unless @user.email_changed? + + Users::UpdateCanonicalEmailService.new(user: @user).execute + end + def update_status return true unless @status_params |