method.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Auth::TwoFactor::Method
  3. attr_reader :user
  4. def initialize(user = nil)
  5. @user = user.presence
  6. end
  7. def verify(payload)
  8. raise NotImplementedError
  9. end
  10. def enabled?
  11. @enabled ||= Setting.get(related_setting_name)
  12. end
  13. def method_name(human: false)
  14. return self.class.name.demodulize.underscore if !human
  15. self.class.name.demodulize.titleize
  16. end
  17. def create_user_config(configuration)
  18. return if configuration.blank?
  19. return update_user_config(configuration) if user_two_factor_preference.present?
  20. two_factor_prefs = User::TwoFactorPreference.new(
  21. method: method_name,
  22. configuration: configuration,
  23. user_id: user.id,
  24. )
  25. two_factor_prefs.save!
  26. end
  27. def update_user_config(configuration)
  28. return if configuration.blank?
  29. return if user_two_factor_preference.blank?
  30. user_two_factor_preference.update!(configuration: user_two_factor_preference_configuration.merge(configuration))
  31. end
  32. def destroy_user_config
  33. user.two_factor_preferences.find_by(method: method_name)&.destroy!
  34. end
  35. def user_two_factor_preference
  36. raise NotImplementedError
  37. end
  38. def user_two_factor_preference_configuration
  39. user_two_factor_preference&.configuration
  40. end
  41. private
  42. def verify_result(verified, configuration: {}, new_configuration: {})
  43. return { verified: false } if !verified
  44. {
  45. **configuration,
  46. verified: true,
  47. **new_configuration,
  48. }
  49. end
  50. def related_setting_name
  51. raise NotImplementedError
  52. end
  53. end