2018-07-18 12:03:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
module Users
|
|
|
|
class DestroyService
|
2018-08-13 07:54:31 -04:00
|
|
|
DestroyError = Class.new(StandardError)
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
attr_accessor :current_user
|
|
|
|
|
|
|
|
def initialize(current_user)
|
|
|
|
@current_user = current_user
|
|
|
|
end
|
|
|
|
|
2017-06-02 09:18:24 -04:00
|
|
|
# Synchronously destroys +user+
|
|
|
|
#
|
|
|
|
# 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+.
|
2016-08-13 08:45:31 -04:00
|
|
|
def execute(user, options = {})
|
2017-06-02 09:18:24 -04:00
|
|
|
delete_solo_owned_groups = options.fetch(:delete_solo_owned_groups, options[:hard_delete])
|
|
|
|
|
2017-02-17 09:58:12 -05:00
|
|
|
unless Ability.allowed?(current_user, :destroy_user, user)
|
2017-02-04 03:14:17 -05:00
|
|
|
raise Gitlab::Access::AccessDeniedError, "#{current_user} tried to destroy user #{user}!"
|
|
|
|
end
|
|
|
|
|
2017-06-02 09:18:24 -04:00
|
|
|
if !delete_solo_owned_groups && user.solo_owned_groups.present?
|
2016-08-13 08:45:31 -04:00
|
|
|
user.errors[:base] << 'You must transfer ownership or delete groups before you can remove user'
|
|
|
|
return user
|
|
|
|
end
|
|
|
|
|
2018-01-02 10:06:44 -05:00
|
|
|
# Calling all before/after_destroy hooks for the user because
|
|
|
|
# there is no dependent: destroy in the relationship. And the removal
|
|
|
|
# is done by a foreign_key. Otherwise they won't be called
|
|
|
|
user.members.find_each { |member| member.run_callbacks(:destroy) }
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
user.solo_owned_groups.each do |group|
|
|
|
|
Groups::DestroyService.new(group, current_user).execute
|
|
|
|
end
|
|
|
|
|
2017-08-21 09:28:07 -04:00
|
|
|
namespace = user.namespace
|
|
|
|
namespace.prepare_for_destroy
|
|
|
|
|
2017-06-15 08:06:49 -04:00
|
|
|
user.personal_projects.each do |project|
|
2018-08-13 07:54:31 -04:00
|
|
|
success = ::Projects::DestroyService.new(project, current_user).execute
|
|
|
|
raise DestroyError, "Project #{project.id} can't be deleted" unless success
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
|
2018-03-07 12:01:32 -05:00
|
|
|
yield(user) if block_given?
|
|
|
|
|
2017-04-16 11:21:40 -04:00
|
|
|
MigrateToGhostUserService.new(user).execute unless options[:hard_delete]
|
2016-11-11 01:27:43 -05:00
|
|
|
|
2020-06-04 02:08:42 -04:00
|
|
|
response = Snippets::BulkDestroyService.new(current_user, user.snippets).execute(options)
|
2020-02-27 19:09:08 -05:00
|
|
|
raise DestroyError, response.message if response.error?
|
|
|
|
|
2020-02-12 13:09:21 -05:00
|
|
|
# Rails attempts to load all related records into memory before
|
|
|
|
# destroying: https://github.com/rails/rails/issues/22510
|
|
|
|
# This ensures we delete records in batches.
|
2020-02-27 19:09:08 -05:00
|
|
|
user.destroy_dependent_associations_in_batches(exclude: [:snippets])
|
2020-01-14 10:07:55 -05:00
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
# Destroy the namespace after destroying the user since certain methods may depend on the namespace existing
|
|
|
|
user_data = user.destroy
|
2018-01-02 11:21:28 -05:00
|
|
|
namespace.destroy
|
2016-08-13 08:45:31 -04:00
|
|
|
|
|
|
|
user_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Users::DestroyService.prepend_if_ee('EE::Users::DestroyService')
|