2020-11-05 00:09:16 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Members
|
2021-04-07 21:09:01 +00:00
|
|
|
class InviteService < Members::CreateService
|
|
|
|
extend ::Gitlab::Utils::Override
|
2020-11-05 00:09:16 +00:00
|
|
|
|
2021-03-04 15:11:19 +00:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
2020-11-05 00:09:16 +00:00
|
|
|
|
|
|
|
@errors = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
alias_method :formatted_errors, :errors
|
2020-11-05 00:09:16 +00:00
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
def invites_from_params
|
|
|
|
params[:email]
|
2020-11-05 00:09:16 +00:00
|
|
|
end
|
|
|
|
|
2021-09-09 09:11:16 +00:00
|
|
|
def validate_invitable!
|
2021-04-07 21:09:01 +00:00
|
|
|
super
|
2020-12-08 09:09:41 +00:00
|
|
|
|
2021-06-28 18:08:39 +00:00
|
|
|
# we need the below due to add_users hitting Members::CreatorService.parse_users_list and ignoring invalid emails
|
2021-04-07 21:09:01 +00:00
|
|
|
# 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
|
2020-12-08 09:09:41 +00:00
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
invalid.each { |email| errors[email] = s_('AddMember|Invite email is invalid') }
|
2020-12-08 09:09:41 +00:00
|
|
|
end
|
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
override :blank_invites_message
|
|
|
|
def blank_invites_message
|
|
|
|
s_('AddMember|Emails cannot be blank')
|
2020-11-05 00:09:16 +00:00
|
|
|
end
|
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
override :add_error_for_member
|
|
|
|
def add_error_for_member(member)
|
|
|
|
errors[invite_email(member)] = member.errors.full_messages.to_sentence
|
2020-11-05 00:09:16 +00:00
|
|
|
end
|
2021-03-31 18:09:19 +00:00
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
def invite_email(member)
|
|
|
|
member.invite_email || member.user.email
|
2021-03-31 18:09:19 +00:00
|
|
|
end
|
2020-11-05 00:09:16 +00:00
|
|
|
end
|
|
|
|
end
|