2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-28 10:55:33 +00:00
|
|
|
module Members
|
2017-10-11 14:47:08 +00:00
|
|
|
class CreateService < Members::BaseService
|
2017-06-06 14:55:12 +00:00
|
|
|
DEFAULT_LIMIT = 100
|
|
|
|
|
2018-02-16 14:10:22 +00:00
|
|
|
def execute(source)
|
2019-04-15 12:25:48 +00:00
|
|
|
return error(s_('AddMember|No users specified.')) if params[:user_ids].blank?
|
2017-06-06 14:55:12 +00:00
|
|
|
|
|
|
|
user_ids = params[:user_ids].split(',').uniq
|
|
|
|
|
2019-04-15 12:25:48 +00:00
|
|
|
return error(s_("AddMember|Too many users specified (limit is %{user_limit})") % { user_limit: user_limit }) if
|
2017-06-06 14:55:12 +00:00
|
|
|
user_limit && user_ids.size > user_limit
|
2016-10-28 10:55:33 +00:00
|
|
|
|
2017-10-11 14:47:08 +00:00
|
|
|
members = source.add_users(
|
2017-06-06 14:55:12 +00:00
|
|
|
user_ids,
|
2016-10-28 10:55:33 +00:00
|
|
|
params[:access_level],
|
|
|
|
expires_at: params[:expires_at],
|
|
|
|
current_user: current_user
|
|
|
|
)
|
|
|
|
|
2019-01-31 09:51:29 +00:00
|
|
|
errors = []
|
2017-10-11 14:47:08 +00:00
|
|
|
|
2019-01-31 09:51:29 +00:00
|
|
|
members.each do |member|
|
|
|
|
if member.errors.any?
|
2019-05-08 05:50:43 +00: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 09:51:29 +00:00
|
|
|
else
|
|
|
|
after_execute(member: member)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return success unless errors.any?
|
|
|
|
|
|
|
|
error(errors.to_sentence)
|
2017-06-06 14:55:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_limit
|
|
|
|
limit = params.fetch(:limit, DEFAULT_LIMIT)
|
|
|
|
|
|
|
|
limit && limit < 0 ? nil : limit
|
2016-10-28 10:55:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Members::CreateService.prepend_if_ee('EE::Members::CreateService')
|