2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module TwoFactor
|
|
|
|
class DestroyService < ::TwoFactor::BaseService
|
|
|
|
def execute
|
2023-01-13 00:05:48 +05:30
|
|
|
return error(_('You are not authorized to perform this action')) unless authorized?
|
2020-11-24 15:15:51 +05:30
|
|
|
return error(_('Two-factor authentication is not enabled for this user')) unless user.two_factor_enabled?
|
|
|
|
|
|
|
|
result = disable_two_factor
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
notify_on_success(user) if result[:status] == :success
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
def authorized?
|
|
|
|
can?(current_user, :disable_two_factor, user)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def disable_two_factor
|
|
|
|
::Users::UpdateService.new(current_user, user: user).execute do |user|
|
|
|
|
user.disable_two_factor!
|
|
|
|
end
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
def notify_on_success(user)
|
|
|
|
notification_service.disabled_two_factor(user)
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
TwoFactor::DestroyService.prepend_mod_with('TwoFactor::DestroyService')
|