Class: Users::DeactivateService

Inherits:
BaseService show all
Defined in:
app/services/users/deactivate_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#params, #project

Instance Method Summary collapse

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(current_user, skip_authorization: false) ⇒ DeactivateService

Returns a new instance of DeactivateService.



5
6
7
8
# File 'app/services/users/deactivate_service.rb', line 5

def initialize(current_user, skip_authorization: false)
  @current_user = current_user
  @skip_authorization = skip_authorization
end

Instance Method Details

#execute(user) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/users/deactivate_service.rb', line 10

def execute(user)
  unless allowed?
    return ::ServiceResponse.error(message: _('You are not authorized to perform this action'),
      reason: :forbidden)
  end

  if user.blocked?
    return ::ServiceResponse.error(message: _('Error occurred. A blocked user cannot be deactivated'),
      reason: :forbidden)
  end

  if user.internal?
    return ::ServiceResponse.error(message: _('Internal users cannot be deactivated'),
      reason: :forbidden)
  end

  return ::ServiceResponse.success(message: _('User has already been deactivated')) if user.deactivated?

  unless user.can_be_deactivated?
    message = _(
      'The user you are trying to deactivate has been active in the past %{minimum_inactive_days} days ' \
      'and cannot be deactivated')

    deactivation_error_message = format(message,
      minimum_inactive_days: Gitlab::CurrentSettings.deactivate_dormant_users_period)
    return ::ServiceResponse.error(message: deactivation_error_message, reason: :forbidden)
  end

  unless user.deactivate
    return ::ServiceResponse.error(message: user.errors.full_messages.to_sentence,
      reason: :bad_request)
  end

  log_event(user)

  ::ServiceResponse.success
end