20210728103633_move_auth_backends_to_database.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class MoveAuthBackendsToDatabase < ActiveRecord::Migration[6.0]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. Setting.create_if_not_exists(
  7. title: 'Authentication via %s',
  8. name: 'auth_internal',
  9. area: 'Security::Authentication',
  10. description: 'Enables user authentication via %s.',
  11. preferences: {
  12. title_i18n: ['internal database'],
  13. description_i18n: ['internal database'],
  14. permission: ['admin.security'],
  15. },
  16. state: {
  17. priority: 1,
  18. adapter: 'Auth::Backend::Internal',
  19. },
  20. frontend: false
  21. )
  22. Setting.create_if_not_exists(
  23. title: 'Authentication via %s',
  24. name: 'auth_developer',
  25. area: 'Security::Authentication',
  26. description: 'Enables user authentication via %s.',
  27. preferences: {
  28. title_i18n: ['developer password'],
  29. description_i18n: ['developer password'],
  30. permission: ['admin.security'],
  31. },
  32. state: {
  33. priority: 2,
  34. adapter: 'Auth::Backend::Developer',
  35. },
  36. frontend: false
  37. )
  38. update_auth_ldap
  39. end
  40. private
  41. def update_auth_ldap # rubocop:disable Metrics/AbcSize
  42. begin
  43. auth_ldap = Setting.find_by(name: 'auth_ldap')
  44. auth_ldap.state_initial[:value][:priority] = 3
  45. auth_ldap.state_initial[:value][:adapter] = 'Auth::Backend::Ldap'
  46. auth_ldap.state_current[:value][:priority] = 3
  47. auth_ldap.state_current[:value][:adapter] = 'Auth::Backend::Ldap'
  48. auth_ldap.save!
  49. rescue => e
  50. Rails.logger.error "Error while updating 'auth_ldap' Setting priority and adapter"
  51. Rails.logger.error e
  52. end
  53. end
  54. end