remove_method_credentials.rb 954 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::User::TwoFactor::RemoveMethodCredentials < Service::User::TwoFactor::Base
  3. attr_reader :credential_id
  4. def initialize(credential_id:, **)
  5. super(**)
  6. @credential_id = credential_id
  7. end
  8. def execute
  9. validate
  10. credentials.delete_if { |elem| elem[:public_key] == credential_id }
  11. if credentials.blank?
  12. user_preference.destroy!
  13. else
  14. user_preference.save!
  15. end
  16. end
  17. private
  18. def credentials
  19. @credentials ||= user_preference.configuration[:credentials]
  20. end
  21. def validate
  22. if !user_preference
  23. raise Exceptions::UnprocessableEntity, __('The given two-factor method is not configured yet.')
  24. end
  25. return if credentials&.find { |elem| elem[:public_key] == credential_id }
  26. raise Exceptions::UnprocessableEntity, __('The two-factor credentials you\'re trying to delete do not exist')
  27. end
  28. end