2020-11-04 19:09:16 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Members
|
2021-04-07 17:09:01 -04:00
|
|
|
class InviteService < Members::CreateService
|
|
|
|
extend ::Gitlab::Utils::Override
|
2020-11-04 19:09:16 -05:00
|
|
|
|
2021-03-04 10:11:19 -05:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
2020-11-04 19:09:16 -05:00
|
|
|
|
|
|
|
@errors = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
alias_method :formatted_errors, :errors
|
2020-11-04 19:09:16 -05:00
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
def invites_from_params
|
|
|
|
params[:email]
|
2020-11-04 19:09:16 -05:00
|
|
|
end
|
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
def validate_invites!
|
|
|
|
super
|
2020-12-08 04:09:41 -05:00
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
# 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
|
2020-12-08 04:09:41 -05:00
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
invalid.each { |email| errors[email] = s_('AddMember|Invite email is invalid') }
|
2020-12-08 04:09:41 -05:00
|
|
|
end
|
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
override :blank_invites_message
|
|
|
|
def blank_invites_message
|
|
|
|
s_('AddMember|Emails cannot be blank')
|
2020-11-04 19:09:16 -05:00
|
|
|
end
|
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
override :add_error_for_member
|
|
|
|
def add_error_for_member(member)
|
|
|
|
errors[invite_email(member)] = member.errors.full_messages.to_sentence
|
2020-11-04 19:09:16 -05:00
|
|
|
end
|
2021-03-31 14:09:19 -04:00
|
|
|
|
2021-04-07 17:09:01 -04:00
|
|
|
def invite_email(member)
|
|
|
|
member.invite_email || member.user.email
|
2021-03-31 14:09:19 -04:00
|
|
|
end
|
2020-11-04 19:09:16 -05:00
|
|
|
end
|
|
|
|
end
|