2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Members
|
2018-03-27 19:54:05 +05:30
|
|
|
class CreateService < Members::BaseService
|
2017-09-10 17:25:29 +05:30
|
|
|
DEFAULT_LIMIT = 100
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def execute(source)
|
2019-07-31 22:56:46 +05:30
|
|
|
return error(s_('AddMember|No users specified.')) if params[:user_ids].blank?
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
user_ids = params[:user_ids].split(',').uniq
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
return error(s_("AddMember|Too many users specified (limit is %{user_limit})") % { user_limit: user_limit }) if
|
2017-09-10 17:25:29 +05:30
|
|
|
user_limit && user_ids.size > user_limit
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
members = source.add_users(
|
2017-09-10 17:25:29 +05:30
|
|
|
user_ids,
|
2017-08-17 22:00:37 +05:30
|
|
|
params[:access_level],
|
|
|
|
expires_at: params[:expires_at],
|
|
|
|
current_user: current_user
|
|
|
|
)
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
errors = []
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
members.each do |member|
|
|
|
|
if member.errors.any?
|
2019-07-31 22:56:46 +05:30
|
|
|
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-03-02 22:35:43 +05:30
|
|
|
else
|
|
|
|
after_execute(member: member)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return success unless errors.any?
|
|
|
|
|
|
|
|
error(errors.to_sentence)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_limit
|
|
|
|
limit = params.fetch(:limit, DEFAULT_LIMIT)
|
|
|
|
|
|
|
|
limit && limit < 0 ? nil : limit
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|