1 2 3 4 5 6 7 8 9 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 47 48 49 50 51 52 53 54 55 56 57 58
|
# frozen_string_literal: true
module Users
class ActivateService < BaseService
def initialize(current_user)
@current_user = current_user
end
def execute(user)
return error(_('You are not authorized to perform this action'), :forbidden) unless allowed?
return error(_('Error occurred. A blocked user must be unblocked to be activated'), :forbidden) if user.blocked?
return success(_('Successfully activated')) if user.active?
if user.activate
after_activate_hook(user)
log_event(user)
success(_('Successfully activated'))
else
error(user.errors.full_messages.to_sentence, :unprocessable_entity)
end
end
private
attr_reader :current_user
def allowed?
can?(current_user, :admin_all_resources)
end
def after_activate_hook(user)
# overridden by EE module
end
def log_event(user)
Gitlab::AppLogger.info(
message: 'User activated',
username: user.username.to_s,
user_id: user.id,
email: user.email.to_s,
activated_by: current_user.username.to_s,
ip_address: current_user.current_sign_in_ip.to_s
)
end
def success(message)
::ServiceResponse.success(message: message)
end
def error(message, reason)
::ServiceResponse.error(message: message, reason: reason)
end
end
end
Users::ActivateService.prepend_mod_with('Users::ActivateService')
|