2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
class Setting < ApplicationModel
|
2012-04-13 13:51:10 +00:00
|
|
|
store :options
|
2015-09-25 14:37:55 +00:00
|
|
|
store :state_current
|
2012-04-13 13:51:10 +00:00
|
|
|
store :state_initial
|
2015-07-12 02:12:27 +00:00
|
|
|
store :preferences
|
2013-01-24 00:37:27 +00:00
|
|
|
before_create :state_check, :set_initial
|
|
|
|
before_update :state_check
|
2015-08-31 12:36:57 +00:00
|
|
|
after_create :reset_cache
|
|
|
|
after_update :reset_cache
|
|
|
|
after_destroy :reset_cache
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-09-25 14:37:55 +00:00
|
|
|
attr_accessor :state
|
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
@@current = {} # rubocop:disable Style/ClassVars
|
|
|
|
@@change_id = nil # rubocop:disable Style/ClassVars
|
|
|
|
@@lookup_at = nil # rubocop:disable Style/ClassVars
|
2016-01-15 17:22:57 +00:00
|
|
|
@@lookup_timeout = if ENV['ZAMMAD_SETTING_TTL'] # rubocop:disable Style/ClassVars
|
|
|
|
ENV['ZAMMAD_SETTING_TTL'].to_i.seconds
|
|
|
|
else
|
|
|
|
2.minutes
|
|
|
|
end
|
2013-03-19 00:46:49 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
=begin
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
set config setting
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
Setting.set('some_config_name', some_value)
|
2015-05-07 09:49:46 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
=end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-12-24 13:55:43 +00:00
|
|
|
def self.set(name, value)
|
2015-05-07 10:15:40 +00:00
|
|
|
setting = Setting.find_by( name: name )
|
2012-12-24 13:55:43 +00:00
|
|
|
if !setting
|
2015-05-07 11:27:07 +00:00
|
|
|
fail "Can't find config setting '#{name}'"
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
2015-09-25 18:35:36 +00:00
|
|
|
setting.state_current = { value: value }
|
2012-12-24 13:55:43 +00:00
|
|
|
setting.save
|
2015-08-31 12:36:57 +00:00
|
|
|
logger.info "Setting.set(#{name}, #{value.inspect})"
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get config setting
|
|
|
|
|
|
|
|
value = Setting.get('some_config_name')
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.get(name)
|
2015-09-06 11:41:31 +00:00
|
|
|
if load
|
|
|
|
logger.debug "Setting.get(#{name.inspect}) # no cache"
|
|
|
|
else
|
|
|
|
logger.debug "Setting.get(#{name.inspect}) # from cache"
|
|
|
|
end
|
2015-04-30 17:20:27 +00:00
|
|
|
@@current[:settings_config][name]
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
reset config setting to default
|
|
|
|
|
|
|
|
Setting.reset('some_config_name')
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-07-07 05:56:31 +00:00
|
|
|
def self.reset(name)
|
|
|
|
setting = Setting.find_by( name: name )
|
|
|
|
if !setting
|
|
|
|
fail "Can't find config setting '#{name}'"
|
|
|
|
end
|
2015-09-25 18:35:36 +00:00
|
|
|
setting.state_current = setting.state_initial
|
|
|
|
setting.save
|
|
|
|
logger.info "Setting.reset(#{name}, #{setting.state_current.inspect})"
|
2015-07-07 05:56:31 +00:00
|
|
|
load
|
|
|
|
@@current[:settings_config][name]
|
|
|
|
end
|
|
|
|
|
2016-01-13 19:45:51 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
reload config settings
|
|
|
|
|
|
|
|
Setting.reload
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.reload
|
|
|
|
load(true)
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
private
|
2015-05-01 12:31:46 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
# load values and cache them
|
2016-01-13 19:45:51 +00:00
|
|
|
def self.load(force = false)
|
2015-08-31 12:36:57 +00:00
|
|
|
|
|
|
|
# check if config is already generated
|
2016-01-13 19:45:51 +00:00
|
|
|
if !force && @@current[:settings_config]
|
2015-09-06 11:41:31 +00:00
|
|
|
return false if cache_valid?
|
2015-08-31 12:36:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# read all config settings
|
|
|
|
config = {}
|
2015-09-25 18:35:36 +00:00
|
|
|
Setting.select('name, state_current').order(:id).each { |setting|
|
|
|
|
config[setting.name] = setting.state_current[:value]
|
|
|
|
}
|
2015-08-31 12:36:57 +00:00
|
|
|
|
|
|
|
# config lookups
|
|
|
|
config.each { |key, value|
|
|
|
|
next if value.class.to_s != 'String'
|
|
|
|
|
|
|
|
config[key].gsub!( /\#\{config\.(.+?)\}/ ) {
|
|
|
|
config[$1].to_s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# store for class requests
|
|
|
|
cache(config)
|
2015-09-06 11:41:31 +00:00
|
|
|
true
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2016-01-15 17:22:57 +00:00
|
|
|
private_class_method :load
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
# set initial value in state_initial
|
2013-06-12 15:59:58 +00:00
|
|
|
def set_initial
|
2015-09-25 18:35:36 +00:00
|
|
|
self.state_initial = state_current
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2015-08-31 12:36:57 +00:00
|
|
|
# set new cache
|
|
|
|
def self.cache(config)
|
|
|
|
@@change_id = Cache.get('Setting::ChangeId') # rubocop:disable Style/ClassVars
|
|
|
|
@@current[:settings_config] = config
|
|
|
|
logger.debug "Setting.cache: set cache, #{@@change_id}"
|
|
|
|
@@lookup_at = Time.zone.now # rubocop:disable Style/ClassVars
|
|
|
|
end
|
2016-01-15 17:22:57 +00:00
|
|
|
private_class_method :cache
|
2015-08-31 12:36:57 +00:00
|
|
|
|
|
|
|
# reset cache
|
|
|
|
def reset_cache
|
|
|
|
@@change_id = rand(999_999_999).to_s # rubocop:disable Style/ClassVars
|
|
|
|
logger.debug "Setting.reset_cache: set new cache, #{@@change_id}"
|
|
|
|
|
|
|
|
Cache.write('Setting::ChangeId', @@change_id, { expires_in: 24.hours })
|
|
|
|
@@current[:settings_config] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if cache is still valid
|
|
|
|
def self.cache_valid?
|
|
|
|
if @@lookup_at && @@lookup_at > Time.zone.now - @@lookup_timeout
|
2015-09-06 11:41:31 +00:00
|
|
|
#logger.debug 'Setting.cache_valid?: cache_id has beed set within last 2 minutes'
|
2015-08-31 12:36:57 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
change_id = Cache.get('Setting::ChangeId')
|
|
|
|
if change_id == @@change_id
|
|
|
|
@@lookup_at = Time.zone.now # rubocop:disable Style/ClassVars
|
|
|
|
logger.debug "Setting.cache_valid?: cache still valid, #{@@change_id}/#{change_id}"
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
logger.debug "Setting.cache_valid?: cache has changed, #{@@change_id}/#{change_id}"
|
|
|
|
false
|
|
|
|
end
|
2016-01-15 17:22:57 +00:00
|
|
|
private_class_method :cache_valid?
|
2015-08-31 12:36:57 +00:00
|
|
|
|
2015-09-25 21:35:17 +00:00
|
|
|
# convert state into hash to be able to store it as store
|
2013-06-12 15:59:58 +00:00
|
|
|
def state_check
|
2015-09-25 21:35:17 +00:00
|
|
|
return if !state
|
2015-09-25 14:37:55 +00:00
|
|
|
return if state && state.respond_to?('has_key?') && state.key?(:value)
|
2015-09-25 18:35:36 +00:00
|
|
|
self.state_current = { value: state }
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|