2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class MembersMapper
|
2020-01-01 13:55:28 +05:30
|
|
|
def initialize(exported_members:, user:, importable:)
|
2017-08-17 22:00:37 +05:30
|
|
|
@exported_members = user.admin? ? exported_members : []
|
2016-06-22 15:30:34 +05:30
|
|
|
@user = user
|
2020-01-01 13:55:28 +05:30
|
|
|
@importable = importable
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
# This needs to run first, as second call would be from #map
|
2020-03-13 15:44:24 +05:30
|
|
|
# which means Project/Group members already exist.
|
2016-06-22 15:30:34 +05:30
|
|
|
ensure_default_member!
|
|
|
|
end
|
|
|
|
|
|
|
|
def map
|
|
|
|
@map ||=
|
|
|
|
begin
|
2022-06-21 17:19:12 +05:30
|
|
|
@exported_members.each_with_object(missing_keys_tracking_hash) do |member, hash|
|
2016-09-13 17:45:13 +05:30
|
|
|
if member['user']
|
|
|
|
old_user_id = member['user']['id']
|
2022-04-01 21:47:47 +05:30
|
|
|
existing_user_id = existing_users_email_map[get_email(member)]
|
|
|
|
hash[old_user_id] = existing_user_id if existing_user_id && add_team_member(member, existing_user_id)
|
2016-09-13 17:45:13 +05:30
|
|
|
else
|
|
|
|
add_team_member(member)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_user_id
|
|
|
|
@user.id
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def include?(old_user_id)
|
|
|
|
map.has_key?(old_user_id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def missing_keys_tracking_hash
|
|
|
|
Hash.new do |_, key|
|
|
|
|
default_user_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_default_member!
|
2020-03-13 15:44:24 +05:30
|
|
|
return if user_already_member?
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
@importable.members.destroy_all # rubocop: disable Cop/DestroyAll
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-12-07 22:27:20 +05:30
|
|
|
relation_class.create!(user: @user, access_level: importer_access_level, source_id: @importable.id, importing: true)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2020-01-01 13:55:28 +05:30
|
|
|
raise e, "Error adding importer user to #{@importable.class} members. #{e.message}"
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2021-12-07 22:27:20 +05:30
|
|
|
def importer_access_level
|
|
|
|
if @importable.parent.is_a?(::Group) && !@user.admin?
|
|
|
|
lvl = @importable.parent.max_member_access_for_user(@user, only_concrete_membership: true)
|
|
|
|
[lvl, highest_access_level].min
|
|
|
|
else
|
|
|
|
highest_access_level
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def user_already_member?
|
|
|
|
member = @importable.members&.first
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
member&.user == @user && member.access_level >= highest_access_level
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2022-04-01 21:47:47 +05:30
|
|
|
# Returns {email => user_id} hash where user_id is an ID at current instance
|
|
|
|
def existing_users_email_map
|
|
|
|
@existing_users_email_map ||= begin
|
|
|
|
emails = @exported_members.map { |member| get_email(member) }
|
|
|
|
|
|
|
|
User.by_user_email(emails).pluck(:email, :id).to_h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns {user_id => email} hash where user_id is an ID at source "old" instance
|
|
|
|
def exported_members_email_map
|
|
|
|
@exported_members_email_map ||= begin
|
|
|
|
result = {}
|
|
|
|
@exported_members.each do |member|
|
|
|
|
email = get_email(member)
|
|
|
|
|
|
|
|
next unless email
|
|
|
|
|
|
|
|
result[member.dig('user', 'id')] = email
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_email(member_data)
|
|
|
|
return unless member_data['user']
|
|
|
|
|
|
|
|
member_data.dig('user', 'public_email') || member_data.dig('user', 'email')
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_team_member(member, existing_user_id = nil)
|
|
|
|
return true if existing_user_id && @importable.members.exists?(user_id: existing_user_id)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
member_hash = member_hash(member)
|
2022-04-01 21:47:47 +05:30
|
|
|
if existing_user_id
|
|
|
|
member_hash.delete('user')
|
|
|
|
member_hash['user_id'] = existing_user_id
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
member = relation_class.create(member_hash)
|
|
|
|
|
|
|
|
if member.persisted?
|
|
|
|
log_member_addition(member_hash)
|
|
|
|
|
|
|
|
true
|
|
|
|
else
|
|
|
|
log_member_addition_failure(member_hash, member.errors.full_messages)
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def member_hash(member)
|
2022-04-01 21:47:47 +05:30
|
|
|
result = parsed_hash(member).merge(
|
2020-01-01 13:55:28 +05:30
|
|
|
'source_id' => @importable.id,
|
2019-09-04 21:01:54 +05:30
|
|
|
'importing' => true,
|
2020-04-08 14:13:33 +05:30
|
|
|
'access_level' => [member['access_level'], highest_access_level].min
|
2019-09-04 21:01:54 +05:30
|
|
|
).except('user_id')
|
2022-04-01 21:47:47 +05:30
|
|
|
|
|
|
|
if result['created_by_id']
|
|
|
|
created_by_email = exported_members_email_map[result['created_by_id']]
|
|
|
|
|
|
|
|
result['created_by_id'] = existing_users_email_map[created_by_email]
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_hash(member)
|
2022-10-11 01:57:18 +05:30
|
|
|
Gitlab::ImportExport::AttributeCleaner.clean(relation_hash: member.deep_stringify_keys,
|
2020-01-01 13:55:28 +05:30
|
|
|
relation_class: relation_class)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def relation_class
|
|
|
|
case @importable
|
2020-04-08 14:13:33 +05:30
|
|
|
when ::Project
|
2020-01-01 13:55:28 +05:30
|
|
|
ProjectMember
|
2020-04-08 14:13:33 +05:30
|
|
|
when ::Group
|
2020-01-01 13:55:28 +05:30
|
|
|
GroupMember
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
def highest_access_level
|
|
|
|
return relation_class::OWNER if relation_class == GroupMember
|
|
|
|
|
|
|
|
relation_class::MAINTAINER
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
def log_member_addition(member_hash)
|
|
|
|
log_params = base_log_params(member_hash)
|
|
|
|
log_params[:message] = '[Project/Group Import] Added new member'
|
|
|
|
|
|
|
|
logger.info(log_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_member_addition_failure(member_hash, errors)
|
|
|
|
log_params = base_log_params(member_hash)
|
|
|
|
log_params[:message] = "[Project/Group Import] Member addition failed: #{errors&.join(', ')}"
|
|
|
|
|
|
|
|
logger.info(log_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_log_params(member_hash)
|
|
|
|
{
|
2022-04-01 21:47:47 +05:30
|
|
|
user_id: member_hash['user_id'],
|
2020-04-22 19:07:51 +05:30
|
|
|
access_level: member_hash['access_level'],
|
|
|
|
importable_type: @importable.class.to_s,
|
|
|
|
importable_id: @importable.id,
|
|
|
|
root_namespace_id: @importable.try(:root_ancestor)&.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def logger
|
|
|
|
@logger ||= Gitlab::Import::Logger.build
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|