Module: API::Helpers::MembersHelpers

Extended by:
Grape::API::Helpers
Included in:
BadgesHelpers
Defined in:
lib/api/helpers/members_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_multiple_members?(user_id) ⇒ Boolean

Returns:

  • (Boolean)

86
87
88
# File 'lib/api/helpers/members_helpers.rb', line 86

def add_multiple_members?(user_id)
  user_id.include?(',')
end

#add_single_member?(user_id) ⇒ Boolean

Returns:

  • (Boolean)

90
91
92
# File 'lib/api/helpers/members_helpers.rb', line 90

def add_single_member?(user_id)
  user_id.present?
end

#add_single_member_by_user_id(create_service_params) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/api/helpers/members_helpers.rb', line 64

def add_single_member_by_user_id(create_service_params)
  source = create_service_params[:source]
  user_id = create_service_params[:user_ids]
  user = User.find_by(id: user_id) # rubocop: disable CodeReuse/ActiveRecord

  if user
    conflict!('Member already exists') if member_already_exists?(source, user_id)

    instance = ::Members::CreateService.new(current_user, create_service_params)
    instance.execute

    not_allowed! if instance.membership_locked # This currently can only be reached in EE if group membership is locked

    member = instance.single_member
    render_validation_error!(member) if member.invalid?

    present_members(member)
  else
    not_found!('User')
  end
end

#authorize_admin_source!(source_type, source) ⇒ Object


18
19
20
# File 'lib/api/helpers/members_helpers.rb', line 18

def authorize_admin_source!(source_type, source)
  authorize! :"admin_#{source_type}", source
end

#find_all_members(source) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord


43
44
45
46
# File 'lib/api/helpers/members_helpers.rb', line 43

def find_all_members(source)
  members = source.is_a?(Project) ? find_all_members_for_project(source) : find_all_members_for_group(source)
  members.non_invite.non_request
end

#find_all_members_for_group(group) ⇒ Object


52
53
54
# File 'lib/api/helpers/members_helpers.rb', line 52

def find_all_members_for_group(group)
  GroupMembersFinder.new(group, current_user).execute(include_relations: [:inherited, :direct, :shared_from_groups])
end

#find_all_members_for_project(project) ⇒ Object


48
49
50
# File 'lib/api/helpers/members_helpers.rb', line 48

def find_all_members_for_project(project)
  MembersFinder.new(project, current_user).execute(include_relations: [:inherited, :direct, :invited_groups])
end

#find_source(source_type, id) ⇒ Object


14
15
16
# File 'lib/api/helpers/members_helpers.rb', line 14

def find_source(source_type, id)
  public_send("find_#{source_type}!", id) # rubocop:disable GitlabSecurity/PublicSend
end

#present_member_invitations(invitations) ⇒ Object


60
61
62
# File 'lib/api/helpers/members_helpers.rb', line 60

def present_member_invitations(invitations)
  present invitations, with: Entities::Invitation, current_user: current_user
end

#present_members(members) ⇒ Object


56
57
58
# File 'lib/api/helpers/members_helpers.rb', line 56

def present_members(members)
  present members, with: Entities::Member, current_user: current_user, show_seat_info: params[:show_seat_info]
end

#retrieve_member_invitations(source, query = nil) ⇒ Object


31
32
33
34
35
36
# File 'lib/api/helpers/members_helpers.rb', line 31

def retrieve_member_invitations(source, query = nil)
  members = source_members(source).where.not(invite_token: nil)
  members = members.includes(:user)
  members = members.where(invite_email: query) if query.present?
  members
end

#retrieve_members(source, params:, deep: false) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


23
24
25
26
27
28
29
# File 'lib/api/helpers/members_helpers.rb', line 23

def retrieve_members(source, params:, deep: false)
  members = deep ? find_all_members(source) : source_members(source).connected_to_user
  members = members.includes(:user)
  members = members.references(:user).merge(User.search(params[:query], use_minimum_char_limit: false)) if params[:query].present?
  members = members.where(user_id: params[:user_ids]) if params[:user_ids].present?
  members
end

#source_members(source) ⇒ Object


38
39
40
# File 'lib/api/helpers/members_helpers.rb', line 38

def source_members(source)
  source.members
end