20230531180941_two_factor_default_authentication_method.rb 814 B

123456789101112131415161718192021222324
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class TwoFactorDefaultAuthenticationMethod < ActiveRecord::Migration[6.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. ::User::TwoFactorPreference.authentication_methods.group_by(&:user_id).each_value do |two_factor_preferences|
  7. user = two_factor_preferences.first.user
  8. next if user_has_default_two_factor_authentication_method?(user)
  9. Service::User::TwoFactor::SetDefaultMethod
  10. .new(user: user, method_name: two_factor_preferences.first.method, force: true)
  11. .execute
  12. end
  13. end
  14. private
  15. def user_has_default_two_factor_authentication_method?(user)
  16. user.preferences.dig(:two_factor_authentication, :default).present?
  17. end
  18. end