timezone.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Setting::Processed::Timezone < Setting::Processed::Backend
  3. def process_settings!
  4. setting = @input.find { |name, _| name == 'timezone_default' }
  5. return if !setting
  6. value = setting.last['value']
  7. @input.append ['timezone_default_sanitized', { 'value' => sanitize_timezone(value) }]
  8. end
  9. def process_frontend_settings!
  10. value = @input['timezone_default']
  11. @input['timezone_default_sanitized'] = sanitize_timezone(value)
  12. end
  13. def sanitize_timezone(input)
  14. return input if timezone_exists? input
  15. 'UTC'
  16. end
  17. def timezone_exists?(input)
  18. ActiveSupport::TimeZone.find_tzinfo input
  19. true
  20. rescue TZInfo::InvalidTimezoneIdentifier
  21. log_warning(input)
  22. false
  23. end
  24. def log_warning(input)
  25. message = if input.blank?
  26. 'Setting "timezone_default" is empty. Using UTC instead. Please set system timezone.' # rubocop:disable Zammad/DetectTranslatableString
  27. else
  28. "Setting \"timezone_default\" is invalid. Using UTC instead of \"#{input}\". Please use valid IANA timezone."
  29. end
  30. Rails.logger.warn message
  31. end
  32. end