debian-mirror-gitlab/app/services/members/invite_service.rb

99 lines
2.4 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module Members
class InviteService < Members::BaseService
2021-04-17 20:07:23 +05:30
BlankEmailsError = Class.new(StandardError)
TooManyEmailsError = Class.new(StandardError)
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
def initialize(*args)
super
2021-01-29 00:20:46 +05:30
@errors = {}
2021-04-17 20:07:23 +05:30
@emails = params[:email]&.split(',')&.uniq&.flatten
2021-01-29 00:20:46 +05:30
end
def execute(source)
2021-04-17 20:07:23 +05:30
validate_emails!
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
@source = source
emails.each(&method(:process_email))
result
rescue BlankEmailsError, TooManyEmailsError => e
error(e.message)
2021-01-29 00:20:46 +05:30
end
private
2021-04-17 20:07:23 +05:30
attr_reader :source, :errors, :emails
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
def validate_emails!
raise BlankEmailsError, s_('AddMember|Email cannot be blank') if emails.blank?
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
if user_limit && emails.size > user_limit
raise TooManyEmailsError, s_("AddMember|Too many users specified (limit is %{user_limit})") % { user_limit: user_limit }
2021-01-29 00:20:46 +05:30
end
end
2021-04-17 20:07:23 +05:30
def user_limit
limit = params.fetch(:limit, Members::CreateService::DEFAULT_LIMIT)
limit < 0 ? nil : limit
2021-01-29 00:20:46 +05:30
end
2021-04-17 20:07:23 +05:30
def process_email(email)
return if existing_member?(email)
return if existing_invite?(email)
return if existing_request?(email)
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
add_member(email)
2021-01-29 00:20:46 +05:30
end
2021-04-17 20:07:23 +05:30
def existing_member?(email)
2021-01-29 00:20:46 +05:30
existing_member = source.members.with_user_by_email(email).exists?
if existing_member
2021-04-17 20:07:23 +05:30
errors[email] = s_("AddMember|Already a member of %{source_name}") % { source_name: source.name }
2021-01-29 00:20:46 +05:30
return true
end
false
end
2021-04-17 20:07:23 +05:30
def existing_invite?(email)
2021-01-29 00:20:46 +05:30
existing_invite = source.members.search_invite_email(email).exists?
if existing_invite
2021-04-17 20:07:23 +05:30
errors[email] = s_("AddMember|Member already invited to %{source_name}") % { source_name: source.name }
2021-01-29 00:20:46 +05:30
return true
end
false
end
2021-04-17 20:07:23 +05:30
def existing_request?(email)
2021-01-29 00:20:46 +05:30
existing_request = source.requesters.with_user_by_email(email).exists?
if existing_request
2021-04-17 20:07:23 +05:30
errors[email] = s_("AddMember|Member cannot be invited because they already requested to join %{source_name}") % { source_name: source.name }
2021-01-29 00:20:46 +05:30
return true
end
false
end
2021-04-17 20:07:23 +05:30
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?
2021-01-29 00:20:46 +05:30
end
2021-04-17 20:07:23 +05:30
def result
if errors.any?
error(errors)
else
success
end
2021-01-29 00:20:46 +05:30
end
end
end