2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-28 06:55:33 -04:00
|
|
|
module Members
|
2017-10-11 10:47:08 -04:00
|
|
|
class CreateService < Members::BaseService
|
2017-06-06 10:55:12 -04:00
|
|
|
DEFAULT_LIMIT = 100
|
|
|
|
|
2018-02-16 09:10:22 -05:00
|
|
|
def execute(source)
|
2019-04-15 08:25:48 -04:00
|
|
|
return error(s_('AddMember|No users specified.')) if params[:user_ids].blank?
|
2017-06-06 10:55:12 -04:00
|
|
|
|
2020-10-13 08:08:41 -04:00
|
|
|
user_ids = params[:user_ids].split(',').uniq.flatten
|
2017-06-06 10:55:12 -04:00
|
|
|
|
2019-04-15 08:25:48 -04:00
|
|
|
return error(s_("AddMember|Too many users specified (limit is %{user_limit})") % { user_limit: user_limit }) if
|
2017-06-06 10:55:12 -04:00
|
|
|
user_limit && user_ids.size > user_limit
|
2016-10-28 06:55:33 -04:00
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
members = source.add_users(
|
2017-06-06 10:55:12 -04:00
|
|
|
user_ids,
|
2016-10-28 06:55:33 -04:00
|
|
|
params[:access_level],
|
|
|
|
expires_at: params[:expires_at],
|
|
|
|
current_user: current_user
|
|
|
|
)
|
|
|
|
|
2019-01-31 04:51:29 -05:00
|
|
|
errors = []
|
2017-10-11 10:47:08 -04:00
|
|
|
|
2019-01-31 04:51:29 -05:00
|
|
|
members.each do |member|
|
2020-07-16 11:09:38 -04:00
|
|
|
if member.invalid?
|
2019-05-08 01:50:43 -04:00
|
|
|
current_error =
|
|
|
|
# Invited users may not have an associated user
|
|
|
|
if member.user.present?
|
|
|
|
"#{member.user.username}: "
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
current_error += member.errors.full_messages.to_sentence
|
|
|
|
errors << current_error
|
2019-01-31 04:51:29 -05:00
|
|
|
else
|
|
|
|
after_execute(member: member)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-15 19:09:58 -05:00
|
|
|
enqueue_onboarding_progress_action(source) if members.size > errors.size
|
|
|
|
|
2019-01-31 04:51:29 -05:00
|
|
|
return success unless errors.any?
|
|
|
|
|
|
|
|
error(errors.to_sentence)
|
2017-06-06 10:55:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_limit
|
|
|
|
limit = params.fetch(:limit, DEFAULT_LIMIT)
|
|
|
|
|
|
|
|
limit && limit < 0 ? nil : limit
|
2016-10-28 06:55:33 -04:00
|
|
|
end
|
2020-12-15 19:09:58 -05:00
|
|
|
|
|
|
|
def enqueue_onboarding_progress_action(source)
|
2020-12-29 07:10:19 -05:00
|
|
|
namespace_id = source.is_a?(Project) ? source.namespace_id : source.id
|
|
|
|
Namespaces::OnboardingUserAddedWorker.perform_async(namespace_id)
|
2020-12-15 19:09:58 -05:00
|
|
|
end
|
2016-10-28 06:55:33 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Members::CreateService.prepend_if_ee('EE::Members::CreateService')
|