2016-08-13 08:45:31 -04:00
|
|
|
module Users
|
|
|
|
class DestroyService
|
|
|
|
attr_accessor :current_user
|
|
|
|
|
|
|
|
def initialize(current_user)
|
|
|
|
@current_user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(user, options = {})
|
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
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
if !options[:delete_solo_owned_groups] && user.solo_owned_groups.present?
|
|
|
|
user.errors[:base] << 'You must transfer ownership or delete groups before you can remove user'
|
|
|
|
return user
|
|
|
|
end
|
|
|
|
|
|
|
|
user.solo_owned_groups.each do |group|
|
|
|
|
Groups::DestroyService.new(group, current_user).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
user.personal_projects.each do |project|
|
|
|
|
# Skip repository removal because we remove directory with namespace
|
|
|
|
# that contain all this repositories
|
|
|
|
::Projects::DestroyService.new(project, current_user, skip_repo: true).async_execute
|
|
|
|
end
|
|
|
|
|
2016-11-11 01:27:43 -05:00
|
|
|
move_issues_to_ghost_user(user)
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
# Destroy the namespace after destroying the user since certain methods may depend on the namespace existing
|
|
|
|
namespace = user.namespace
|
|
|
|
user_data = user.destroy
|
|
|
|
namespace.really_destroy!
|
|
|
|
|
|
|
|
user_data
|
|
|
|
end
|
2016-11-11 01:27:43 -05:00
|
|
|
|
|
|
|
private
|
2017-02-16 02:05:10 -05:00
|
|
|
|
2016-11-11 01:27:43 -05:00
|
|
|
def move_issues_to_ghost_user(user)
|
2017-02-16 02:05:10 -05:00
|
|
|
# Block the user before moving issues to prevent a data race.
|
|
|
|
# If the user creates an issue after `move_issues_to_ghost_user`
|
|
|
|
# runs and before the user is destroyed, the destroy will fail with
|
|
|
|
# an exception. We block the user so that issues can't be created
|
|
|
|
# after `move_issues_to_ghost_user` runs and before the destroy happens.
|
|
|
|
user.block
|
|
|
|
|
2016-11-11 01:27:43 -05:00
|
|
|
ghost_user = User.ghost
|
|
|
|
|
2017-02-16 02:05:10 -05:00
|
|
|
user.issues.update_all(author_id: ghost_user.id)
|
2016-11-11 01:27:43 -05:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
end
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
end
|