summaryrefslogtreecommitdiff
path: root/app/services/members/invite_service.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-04-20 23:50:22 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-04-20 23:50:22 +0000
commit9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch)
tree70467ae3692a0e35e5ea56bcb803eb512a10bedb /app/services/members/invite_service.rb
parent4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff)
downloadgitlab-ce-9dc93a4519d9d5d7be48ff274127136236a3adb3.tar.gz
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'app/services/members/invite_service.rb')
-rw-r--r--app/services/members/invite_service.rb92
1 files changed, 20 insertions, 72 deletions
diff --git a/app/services/members/invite_service.rb b/app/services/members/invite_service.rb
index 169500d08f0..48010f9c8e7 100644
--- a/app/services/members/invite_service.rb
+++ b/app/services/members/invite_service.rb
@@ -1,98 +1,46 @@
# frozen_string_literal: true
module Members
- class InviteService < Members::BaseService
- BlankEmailsError = Class.new(StandardError)
- TooManyEmailsError = Class.new(StandardError)
+ class InviteService < Members::CreateService
+ extend ::Gitlab::Utils::Override
def initialize(*args)
super
@errors = {}
- @emails = params[:email]&.split(',')&.uniq&.flatten
- end
-
- def execute(source)
- validate_emails!
-
- @source = source
- emails.each(&method(:process_email))
- result
- rescue BlankEmailsError, TooManyEmailsError => e
- error(e.message)
end
private
- attr_reader :source, :errors, :emails
-
- def validate_emails!
- raise BlankEmailsError, s_('AddMember|Email cannot be blank') if emails.blank?
-
- if user_limit && emails.size > user_limit
- raise TooManyEmailsError, s_("AddMember|Too many users specified (limit is %{user_limit})") % { user_limit: user_limit }
- end
- end
-
- def user_limit
- limit = params.fetch(:limit, Members::CreateService::DEFAULT_LIMIT)
-
- limit < 0 ? nil : limit
- end
-
- def process_email(email)
- return if existing_member?(email)
- return if existing_invite?(email)
- return if existing_request?(email)
-
- add_member(email)
- end
-
- def existing_member?(email)
- existing_member = source.members.with_user_by_email(email).exists?
-
- if existing_member
- errors[email] = s_("AddMember|Already a member of %{source_name}") % { source_name: source.name }
- return true
- end
+ alias_method :formatted_errors, :errors
- false
+ def invites_from_params
+ params[:email]
end
- def existing_invite?(email)
- existing_invite = source.members.search_invite_email(email).exists?
+ def validate_invites!
+ super
- if existing_invite
- errors[email] = s_("AddMember|Member already invited to %{source_name}") % { source_name: source.name }
- return true
- end
+ # we need the below due to add_users hitting Member#parse_users_list and ignoring invalid emails
+ # ideally we wouldn't need this, but we can't really change the add_users method
+ valid, invalid = invites.partition { |email| Member.valid_email?(email) }
+ @invites = valid
- false
+ invalid.each { |email| errors[email] = s_('AddMember|Invite email is invalid') }
end
- def existing_request?(email)
- existing_request = source.requesters.with_user_by_email(email).exists?
-
- if existing_request
- errors[email] = s_("AddMember|Member cannot be invited because they already requested to join %{source_name}") % { source_name: source.name }
- return true
- end
-
- false
+ override :blank_invites_message
+ def blank_invites_message
+ s_('AddMember|Emails cannot be blank')
end
- def add_member(email)
- new_member = source.add_user(email, params[:access_level], current_user: current_user, expires_at: params[:expires_at])
-
- errors[email] = new_member.errors.full_messages.to_sentence if new_member.invalid?
+ override :add_error_for_member
+ def add_error_for_member(member)
+ errors[invite_email(member)] = member.errors.full_messages.to_sentence
end
- def result
- if errors.any?
- error(errors)
- else
- success
- end
+ def invite_email(member)
+ member.invite_email || member.user.email
end
end
end