2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Users
|
|
|
|
class DestroyService
|
2018-11-20 20:47:30 +05:30
|
|
|
DestroyError = Class.new(StandardError)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_accessor :current_user
|
|
|
|
|
|
|
|
def initialize(current_user)
|
|
|
|
@current_user = current_user
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
@scheduled_records_gauge = Gitlab::Metrics.gauge(
|
|
|
|
:gitlab_ghost_user_migration_scheduled_records_total,
|
|
|
|
'The total number of scheduled ghost user migrations'
|
|
|
|
)
|
|
|
|
@lag_gauge = Gitlab::Metrics.gauge(
|
|
|
|
:gitlab_ghost_user_migration_lag_seconds,
|
|
|
|
'The waiting time in seconds of the oldest scheduled record for ghost user migration'
|
|
|
|
)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
# Asynchronously destroys +user+
|
|
|
|
# Migrating the associated user records, and post-migration cleanup is
|
|
|
|
# handled by the Users::MigrateRecordsToGhostUserWorker cron worker.
|
2017-09-10 17:25:29 +05:30
|
|
|
#
|
|
|
|
# The operation will fail if the user is the sole owner of any groups. To
|
|
|
|
# force the groups to be destroyed, pass `delete_solo_owned_groups: true` in
|
|
|
|
# +options+.
|
|
|
|
#
|
|
|
|
# The user's contributions will be migrated to a global ghost user. To
|
|
|
|
# force the contributions to be destroyed, pass `hard_delete: true` in
|
|
|
|
# +options+.
|
|
|
|
#
|
|
|
|
# `hard_delete: true` implies `delete_solo_owned_groups: true`. To perform
|
|
|
|
# a hard deletion without destroying solo-owned groups, pass
|
|
|
|
# `delete_solo_owned_groups: false, hard_delete: true` in +options+.
|
2022-10-11 01:57:18 +05:30
|
|
|
#
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def execute(user, options = {})
|
2017-09-10 17:25:29 +05:30
|
|
|
delete_solo_owned_groups = options.fetch(:delete_solo_owned_groups, options[:hard_delete])
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
unless Ability.allowed?(current_user, :destroy_user, user) || options[:skip_authorization]
|
2017-08-17 22:00:37 +05:30
|
|
|
raise Gitlab::Access::AccessDeniedError, "#{current_user} tried to destroy user #{user}!"
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if !delete_solo_owned_groups && user.solo_owned_groups.present?
|
2021-06-08 01:23:25 +05:30
|
|
|
user.errors.add(:base, 'You must transfer ownership or delete groups before you can remove user')
|
2017-08-17 22:00:37 +05:30
|
|
|
return user
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
user.block
|
|
|
|
|
|
|
|
# Load the records. Groups are unavailable after membership is destroyed.
|
|
|
|
solo_owned_groups = user.solo_owned_groups.load
|
|
|
|
|
|
|
|
user.members.each_batch { |batch| batch.destroy_all } # rubocop:disable Style/SymbolProc, Cop/DestroyAll
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
solo_owned_groups.each do |group|
|
2017-08-17 22:00:37 +05:30
|
|
|
Groups::DestroyService.new(group, current_user).execute
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
namespace = user.namespace
|
|
|
|
namespace.prepare_for_destroy
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
user.personal_projects.each do |project|
|
2018-11-20 20:47:30 +05:30
|
|
|
success = ::Projects::DestroyService.new(project, current_user).execute
|
|
|
|
raise DestroyError, "Project #{project.id} can't be deleted" unless success
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
yield(user) if block_given?
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
hard_delete = options.fetch(:hard_delete, false)
|
2023-01-13 00:05:48 +05:30
|
|
|
Users::GhostUserMigration.create!(user: user,
|
|
|
|
initiator_user: current_user,
|
|
|
|
hard_delete: hard_delete)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
update_metrics
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :scheduled_records_gauge, :lag_gauge
|
2022-06-21 17:19:12 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
def update_metrics
|
|
|
|
update_scheduled_records_gauge
|
|
|
|
update_lag_gauge
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
def update_scheduled_records_gauge
|
|
|
|
# We do not want to issue unbounded COUNT() queries, hence we limit the
|
|
|
|
# query to count 1001 records and then approximate the result.
|
|
|
|
count = Users::GhostUserMigration.limit(1001).count
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
if count == 1001
|
|
|
|
# more than 1000 records, approximate count
|
|
|
|
min = Users::GhostUserMigration.minimum(:id) || 0
|
|
|
|
max = Users::GhostUserMigration.maximum(:id) || 0
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
scheduled_records_gauge.set({}, max - min)
|
|
|
|
else
|
|
|
|
# less than 1000 records, count is accurate
|
|
|
|
scheduled_records_gauge.set({}, count)
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
def update_lag_gauge
|
|
|
|
oldest_job = Users::GhostUserMigration.first
|
|
|
|
lag_gauge.set({}, Time.current - oldest_job.created_at)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Users::DestroyService.prepend_mod_with('Users::DestroyService')
|