debian-mirror-gitlab/lib/api/helpers/members_helpers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module API
module Helpers
module MembersHelpers
2020-03-13 15:44:24 +05:30
extend Grape::API::Helpers
params :optional_filter_params_ee do
end
2022-01-26 12:08:38 +05:30
params :optional_state_filter_ee do
end
2023-04-23 21:23:45 +05:30
params :optional_put_params_ee do
end
2016-09-13 17:45:13 +05:30
def find_source(source_type, id)
2018-03-17 18:26:18 +05:30
public_send("find_#{source_type}!", id) # rubocop:disable GitlabSecurity/PublicSend
2016-09-13 17:45:13 +05:30
end
2022-06-02 21:05:25 +05:30
def authorize_read_source_member!(source_type, source)
authorize! :"read_#{source_type}_member", source
end
2016-09-13 17:45:13 +05:30
def authorize_admin_source!(source_type, source)
authorize! :"admin_#{source_type}", source
end
2018-11-18 11:00:15 +05:30
2020-01-01 13:55:28 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def retrieve_members(source, params:, deep: false)
2021-06-08 01:23:25 +05:30
members = deep ? find_all_members(source) : source_members(source).connected_to_user
2020-01-01 13:55:28 +05:30
members = members.includes(:user)
2022-04-04 11:22:00 +05:30
members = members.references(:user).merge(User.search(params[:query], use_minimum_char_limit: false)) if params[:query].present?
2020-01-01 13:55:28 +05:30
members = members.where(user_id: params[:user_ids]) if params[:user_ids].present?
members
end
2021-01-29 00:20:46 +05:30
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
def source_members(source)
2023-06-20 00:43:36 +05:30
return source.namespace_members if source.is_a?(Project)
2023-03-17 16:20:25 +05:30
2021-01-29 00:20:46 +05:30
source.members
end
2020-01-01 13:55:28 +05:30
# 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-11-18 11:00:15 +05:30
end
def find_all_members_for_project(project)
2023-07-09 08:55:56 +05:30
include_relations = [:inherited, :direct, :invited_groups, :shared_into_ancestors]
MembersFinder.new(project, current_user).execute(include_relations: include_relations)
2018-11-18 11:00:15 +05:30
end
def find_all_members_for_group(group)
2022-03-02 08:16:31 +05:30
GroupMembersFinder.new(group, current_user).execute(include_relations: [:inherited, :direct, :shared_from_groups])
2018-11-18 11:00:15 +05:30
end
2020-01-01 13:55:28 +05:30
2021-11-11 11:23:49 +05:30
def present_members(members)
present members, with: Entities::Member, current_user: current_user, show_seat_info: params[:show_seat_info]
2020-03-13 15:44:24 +05:30
end
2021-11-11 11:23:49 +05:30
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)
source = create_service_params[:source]
2022-07-16 23:28:13 +05:30
user_id = create_service_params[:user_id]
2021-11-11 11:23:49 +05:30
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
2021-10-27 15:23:28 +05:30
2021-11-11 11:23:49 +05:30
member = instance.single_member
render_validation_error!(member) if member.invalid?
present_members(member)
else
not_found!('User')
2021-10-27 15:23:28 +05:30
end
end
2021-11-11 11:23:49 +05:30
def add_multiple_members?(user_id)
user_id.include?(',')
2020-01-01 13:55:28 +05:30
end
2021-01-29 00:20:46 +05:30
2021-11-11 11:23:49 +05:30
def add_single_member?(user_id)
user_id.present?
end
2023-04-23 21:23:45 +05:30
def self.member_access_levels
Gitlab::Access.all_values
end
2021-11-11 11:23:49 +05:30
private
def member_already_exists?(source, user_id)
source.members.exists?(user_id: user_id) # rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
end
2016-09-13 17:45:13 +05:30
end
end
end
2020-03-13 15:44:24 +05:30
2021-06-08 01:23:25 +05:30
API::Helpers::MembersHelpers.prepend_mod_with('API::Helpers::MembersHelpers')