Class: Users::DestroyService

Inherits:
Object
  • Object
show all
Defined in:
app/services/users/destroy_service.rb

Constant Summary collapse

DestroyError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user) ⇒ DestroyService

Returns a new instance of DestroyService.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/services/users/destroy_service.rb', line 9

def initialize(current_user)
  @current_user = current_user

  @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'
  )
end

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



7
8
9
# File 'app/services/users/destroy_service.rb', line 7

def current_user
  @current_user
end

Instance Method Details

#execute(user, options = {}) {|user| ... } ⇒ Object

Asynchronously destroys user Migrating the associated user records, and post-migration cleanup is handled by the Users::MigrateRecordsToGhostUserWorker cron worker.

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.

Yields:

  • (user)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/users/destroy_service.rb', line 39

def execute(user, options = {})
  delete_solo_owned_groups = options.fetch(:delete_solo_owned_groups, options[:hard_delete])

  unless Ability.allowed?(current_user, :destroy_user, user) || options[:skip_authorization]
    raise Gitlab::Access::AccessDeniedError, "#{current_user} tried to destroy user #{user}!"
  end

  if !delete_solo_owned_groups && user.solo_owned_groups.present?
    user.errors.add(:base, 'You must transfer ownership or delete groups before you can remove user')
    return user
  end

  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

  solo_owned_groups.each do |group|
    Groups::DestroyService.new(group, current_user).execute
  end

  user.personal_projects.each do |project|
    success = ::Projects::DestroyService.new(project, current_user).execute
    raise DestroyError, "Project #{project.id} can't be deleted" unless success
  end

  yield(user) if block_given?

  hard_delete = options.fetch(:hard_delete, false)
  Users::GhostUserMigration.create!(
    user: user,
    initiator_user: current_user,
    hard_delete: hard_delete
  )

  update_metrics
end