2016-10-28 06:55:33 -04:00
|
|
|
module Members
|
|
|
|
class CreateService < BaseService
|
2017-06-06 10:55:12 -04:00
|
|
|
DEFAULT_LIMIT = 100
|
|
|
|
|
2017-04-17 07:45:14 -04:00
|
|
|
def initialize(source, current_user, params = {})
|
|
|
|
@source = source
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
2017-06-06 10:55:12 -04:00
|
|
|
@error = nil
|
2017-04-17 07:45:14 -04:00
|
|
|
end
|
|
|
|
|
2016-10-28 06:55:33 -04:00
|
|
|
def execute
|
2017-06-06 10:55:12 -04:00
|
|
|
return error('No users specified.') if params[:user_ids].blank?
|
|
|
|
|
|
|
|
user_ids = params[:user_ids].split(',').uniq
|
|
|
|
|
|
|
|
return error("Too many users specified (limit is #{user_limit})") if
|
|
|
|
user_limit && user_ids.size > user_limit
|
2016-10-28 06:55:33 -04:00
|
|
|
|
2017-04-17 07:45:14 -04:00
|
|
|
@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
|
|
|
|
)
|
|
|
|
|
2017-06-06 10:55:12 -04:00
|
|
|
success
|
|
|
|
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
|