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
|
|
|
|
|
|
|
user_ids = params[:user_ids].split(',').uniq
|
|
|
|
|
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|
|
|
|
|
if member.errors.any?
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|