setting.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Class variables are used here as performance optimization.
  3. # Technically it is not thread-safe, but it never caused issues.
  4. # rubocop:disable Style/ClassVars
  5. class Setting < ApplicationModel
  6. store :options
  7. store :state_current
  8. store :state_initial
  9. store :preferences
  10. before_validation :state_check
  11. before_create :set_initial
  12. after_save :reset_class_cache_key
  13. after_commit :reset_other_caches, :broadcast_frontend, :check_refresh
  14. validates_with Setting::Validator
  15. attr_accessor :state
  16. @@current = {}
  17. @@raw = {}
  18. @@query_cache_key = nil
  19. @@last_changed_at = nil
  20. @@lookup_at = nil
  21. @@lookup_timeout = if ENV['ZAMMAD_SETTING_TTL']
  22. ENV['ZAMMAD_SETTING_TTL'].to_i.seconds
  23. else
  24. 15.seconds
  25. end
  26. =begin
  27. set config setting
  28. Setting.set('some_config_name', some_value)
  29. =end
  30. def self.set(name, value)
  31. setting = Setting.find_by(name: name)
  32. if !setting
  33. raise "Can't find config setting '#{name}'"
  34. end
  35. setting.state_current = { value: value }
  36. setting.save!
  37. logger.info "Setting.set('#{name}', #{value.inspect})"
  38. true
  39. end
  40. =begin
  41. get config setting
  42. value = Setting.get('some_config_name')
  43. =end
  44. def self.get(name)
  45. load
  46. @@current[name].deep_dup # prevents accidental modification of settings in console
  47. end
  48. =begin
  49. reset config setting to default
  50. Setting.reset('some_config_name')
  51. Setting.reset('some_config_name', force) # true|false - force it false per default
  52. =end
  53. def self.reset(name, force = false)
  54. setting = Setting.find_by(name: name)
  55. if !setting
  56. raise "Can't find config setting '#{name}'"
  57. end
  58. return true if !force && setting.state_current == setting.state_initial
  59. setting.state_current = setting.state_initial
  60. setting.save!
  61. logger.info "Setting.reset('#{name}', #{setting.state_current.inspect})"
  62. true
  63. end
  64. =begin
  65. reload config settings
  66. Setting.reload
  67. =end
  68. def self.reload
  69. @@last_changed_at = nil
  70. load(true)
  71. end
  72. # check if cache is still valid
  73. def self.cache_valid?
  74. # Check if last last lookup was recent enough
  75. if @@lookup_at && @@lookup_at > @@lookup_timeout.ago
  76. # logger.debug "Setting.cache_valid?: cache_id has been set within last #{@@lookup_timeout} seconds"
  77. return true
  78. end
  79. if @@query_cache_key && Setting.reorder(:id).cache_key_with_version == @@query_cache_key
  80. @@lookup_at = Time.current
  81. return true
  82. end
  83. false
  84. end
  85. private
  86. # load values and cache them
  87. def self.load(force = false)
  88. # check if config is already generated
  89. return false if !force && @@current.present? && cache_valid?
  90. # read all or only changed since last read
  91. latest = Setting.maximum(:updated_at)
  92. base_query = Setting.reorder(:id)
  93. settings_query = if @@last_changed_at && @@current.present?
  94. base_query.where(updated_at: @@last_changed_at..)
  95. else
  96. base_query
  97. end
  98. settings = settings_query.pluck(:name, :state_current)
  99. @@last_changed_at = [Time.current, latest].min if latest
  100. if settings.present?
  101. settings.each do |setting|
  102. @@raw[setting[0]] = setting[1]['value']
  103. end
  104. @@raw.each do |key, value|
  105. @@current[key] = interpolate_value value
  106. end
  107. end
  108. @@query_cache_key = base_query.cache_key_with_version
  109. @@lookup_at = Time.current
  110. true
  111. end
  112. private_class_method :load
  113. def self.interpolate_value(input)
  114. return input if !input.is_a? String
  115. input.gsub(%r{\#\{config\.(.+?)\}}) do
  116. @@raw[$1].to_s
  117. end
  118. end
  119. private_class_method :interpolate_value
  120. # set initial value in state_initial
  121. def set_initial
  122. self.state_initial = state_current
  123. end
  124. def reset_class_cache_key
  125. @@lookup_at = nil
  126. @@query_cache_key = nil
  127. end
  128. # Resets caches related to the setting in question.
  129. def reset_other_caches
  130. return if preferences[:cache].blank?
  131. Array(preferences[:cache]).each do |key|
  132. Rails.cache.delete(key)
  133. end
  134. end
  135. # Convert state into hash to be able to store it as store.
  136. def state_check
  137. return if state.nil? # allow false value
  138. return if state.try(:key?, :value)
  139. self.state_current = { value: state }
  140. end
  141. # Notify clients about config changes.
  142. def broadcast_frontend
  143. return if !frontend
  144. # Some setting values use interpolation to reference other settings.
  145. # This is applied in `Setting.get`, thus direct reading of the value should be avoided.
  146. value = self.class.get(name)
  147. Sessions.broadcast(
  148. {
  149. event: 'config_update',
  150. data: { name: name, value: value }
  151. },
  152. preferences[:authentication] ? 'authenticated' : 'public'
  153. )
  154. Gql::Subscriptions::ConfigUpdates.trigger(self)
  155. end
  156. # NB: Force users to reload on SAML credentials config changes
  157. # This is needed because the setting is not frontend related,
  158. # so we can't rely on 'config_update_local' mechanism to kick in
  159. # https://github.com/zammad/zammad/issues/4263
  160. def check_refresh
  161. return if ['auth_saml_credentials'].exclude?(name)
  162. AppVersion.set(true, AppVersion::MSG_CONFIG_CHANGED)
  163. end
  164. end
  165. # rubocop:enable Style/ClassVars