20230531180941_two_factor_default_authentication_method.rb 742 B

12345678910111213141516171819202122
  1. # Copyright (C) 2012-2023 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 do |_user_id, two_factor_preferences|
  7. user = two_factor_preferences.first.user
  8. next if user_has_default_two_factor_authentication_method?(user)
  9. user.two_factor_update_default_method(two_factor_preferences.first.method)
  10. end
  11. end
  12. private
  13. def user_has_default_two_factor_authentication_method?(user)
  14. user.auth_two_factor.user_default_authentication_method.present?
  15. end
  16. end