2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-23 11:14:31 -04:00
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module MembersHelpers
|
2019-12-26 13:07:46 -05:00
|
|
|
extend Grape::API::Helpers
|
|
|
|
|
|
|
|
params :optional_filter_params_ee do
|
|
|
|
end
|
|
|
|
|
2021-11-25 13:14:13 -05:00
|
|
|
params :optional_state_filter_ee do
|
|
|
|
end
|
|
|
|
|
2016-06-23 11:14:31 -04:00
|
|
|
def find_source(source_type, id)
|
2017-08-03 22:20:34 -04:00
|
|
|
public_send("find_#{source_type}!", id) # rubocop:disable GitlabSecurity/PublicSend
|
2016-06-23 11:14:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_source!(source_type, source)
|
|
|
|
authorize! :"admin_#{source_type}", source
|
|
|
|
end
|
2018-07-25 17:45:42 -04:00
|
|
|
|
2019-11-22 01:06:20 -05:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def retrieve_members(source, params:, deep: false)
|
2021-05-18 17:10:16 -04:00
|
|
|
members = deep ? find_all_members(source) : source_members(source).connected_to_user
|
2019-11-22 01:06:20 -05:00
|
|
|
members = members.includes(:user)
|
2022-02-07 10:15:53 -05:00
|
|
|
members = members.references(:user).merge(User.search(params[:query], use_minimum_char_limit: false)) if params[:query].present?
|
2019-11-22 01:06:20 -05:00
|
|
|
members = members.where(user_id: params[:user_ids]) if params[:user_ids].present?
|
|
|
|
members
|
|
|
|
end
|
2020-11-02 10:08:52 -05:00
|
|
|
|
2020-11-16 10:09:23 -05:00
|
|
|
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
|
|
|
|
|
2020-11-02 10:08:52 -05:00
|
|
|
def source_members(source)
|
|
|
|
source.members
|
|
|
|
end
|
2019-11-22 01:06:20 -05:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
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
|
2018-07-25 17:45:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_all_members_for_project(project)
|
2020-12-02 01:09:50 -05:00
|
|
|
MembersFinder.new(project, current_user).execute(include_relations: [:inherited, :direct, :invited_groups])
|
2018-07-25 17:45:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_all_members_for_group(group)
|
2021-12-20 10:12:25 -05:00
|
|
|
GroupMembersFinder.new(group, current_user).execute(include_relations: [:inherited, :direct, :shared_from_groups])
|
2018-07-25 17:45:42 -04:00
|
|
|
end
|
2019-11-22 01:06:20 -05:00
|
|
|
|
2021-09-09 05:11:16 -04:00
|
|
|
def present_members(members)
|
|
|
|
present members, with: Entities::Member, current_user: current_user, show_seat_info: params[:show_seat_info]
|
2020-01-14 16:07:45 -05:00
|
|
|
end
|
|
|
|
|
2021-09-09 05:11:16 -04:00
|
|
|
def present_member_invitations(invitations)
|
|
|
|
present invitations, with: Entities::Invitation, current_user: current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_single_member_by_user_id(create_service_params)
|
2022-02-14 19:15:45 -05:00
|
|
|
source = create_service_params[:source]
|
2021-09-09 05:11:16 -04:00
|
|
|
user_id = create_service_params[:user_ids]
|
|
|
|
user = User.find_by(id: user_id) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
if user
|
2022-02-14 19:15:45 -05:00
|
|
|
conflict!('Member already exists') if member_already_exists?(source, user_id)
|
|
|
|
|
2021-09-09 05:11:16 -04:00
|
|
|
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
|
2021-07-22 17:09:40 -04:00
|
|
|
|
2021-09-09 05:11:16 -04:00
|
|
|
member = instance.single_member
|
|
|
|
render_validation_error!(member) if member.invalid?
|
|
|
|
|
|
|
|
present_members(member)
|
|
|
|
else
|
|
|
|
not_found!('User')
|
2021-07-22 17:09:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-09 05:11:16 -04:00
|
|
|
def add_multiple_members?(user_id)
|
|
|
|
user_id.include?(',')
|
2019-11-22 01:06:20 -05:00
|
|
|
end
|
2020-11-16 10:09:23 -05:00
|
|
|
|
2021-09-09 05:11:16 -04:00
|
|
|
def add_single_member?(user_id)
|
|
|
|
user_id.present?
|
|
|
|
end
|
2022-02-14 19:15:45 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def member_already_exists?(source, user_id)
|
|
|
|
source.members.exists?(user_id: user_id) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
end
|
2016-06-23 11:14:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-26 13:07:46 -05:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
API::Helpers::MembersHelpers.prepend_mod_with('API::Helpers::MembersHelpers')
|