two_factor_updates.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Subscriptions
  3. class User::Current::TwoFactorUpdates < BaseSubscription
  4. description 'Updates to current user two factor records'
  5. argument :user_id, GraphQL::Types::ID, 'ID of the user to receive avatar updates for', loads: Gql::Types::UserType
  6. field :configuration, Gql::Types::User::ConfigurationTwoFactorType, description: 'Configuration information for the current user.'
  7. # Instance method: allow subscriptions only for the current user
  8. def authorized?(user:)
  9. context.current_user.permissions?('user_preferences.two_factor_authentication') && user.id == context.current_user.id
  10. end
  11. def subscribe(user:)
  12. response(user)
  13. end
  14. def update(user:)
  15. response(user)
  16. end
  17. private
  18. def response(user)
  19. enabled_authentication_methods = user.two_factor_enabled_authentication_methods
  20. {
  21. configuration: {
  22. enabled_authentication_methods: enabled_authentication_methods.each { |item| item[:authentication_method] = item.delete(:method) },
  23. recovery_codes_exist: user.auth_two_factor.user_recovery_codes_exists?
  24. }
  25. }
  26. end
  27. end
  28. end