setting.rb 5.8 KB

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