Class: Members::CreateService

Inherits:
BaseService show all
Defined in:
app/services/members/create_service.rb

Constant Summary collapse

DEFAULT_LIMIT =
100

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#after_execute, #initialize

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from Members::BaseService

Instance Method Details

#execute(source) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/members/create_service.rb', line 7

def execute(source)
  return error(s_('AddMember|No users specified.')) if params[:user_ids].blank?

  user_ids = params[:user_ids].split(',').uniq

  return error(s_("AddMember|Too many users specified (limit is %{user_limit})") % { user_limit: user_limit }) if
    user_limit && user_ids.size > user_limit

  members = source.add_users(
    user_ids,
    params[:access_level],
    expires_at: params[:expires_at],
    current_user: current_user
  )

  errors = []

  members.each do |member|
    if member.invalid?
      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
    else
      after_execute(member: member)
    end
  end

  return success unless errors.any?

  error(errors.to_sentence)
end