2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-05-18 02:19:08 +00:00
|
|
|
# Redmine - project management software
|
2020-03-03 00:24:10 +00:00
|
|
|
# Copyright (C) 2006-2020 Jean-Philippe Lang
|
2007-01-21 11:50:22 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-05-18 02:19:08 +00:00
|
|
|
#
|
2007-01-21 11:50:22 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2011-05-18 02:19:08 +00:00
|
|
|
#
|
2007-01-21 11:50:22 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
class Setting < ActiveRecord::Base
|
2019-08-29 01:35:09 +00:00
|
|
|
PASSWORD_CHAR_CLASSES = {
|
2019-11-25 16:48:24 +00:00
|
|
|
'uppercase' => /[A-Z]/,
|
|
|
|
|
'lowercase' => /[a-z]/,
|
|
|
|
|
'digits' => /[0-9]/,
|
|
|
|
|
'special_chars' => /[[:ascii:]&&[:graph:]&&[:^alnum:]]/
|
|
|
|
|
}
|
2019-08-29 01:35:09 +00:00
|
|
|
|
2007-11-16 20:26:36 +00:00
|
|
|
DATE_FORMATS = [
|
2019-11-24 12:57:50 +00:00
|
|
|
'%Y-%m-%d',
|
|
|
|
|
'%d/%m/%Y',
|
|
|
|
|
'%d.%m.%Y',
|
|
|
|
|
'%d-%m-%Y',
|
|
|
|
|
'%m/%d/%Y',
|
|
|
|
|
'%d %b %Y',
|
|
|
|
|
'%d %B %Y',
|
|
|
|
|
'%b %d, %Y',
|
|
|
|
|
'%B %d, %Y'
|
|
|
|
|
]
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2007-11-16 20:26:36 +00:00
|
|
|
TIME_FORMATS = [
|
|
|
|
|
'%H:%M',
|
|
|
|
|
'%I:%M %p'
|
2019-11-24 12:57:50 +00:00
|
|
|
]
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2019-10-22 11:30:05 +00:00
|
|
|
ENCODINGS = %w(US-ASCII
|
2008-08-26 12:13:15 +00:00
|
|
|
windows-1250
|
|
|
|
|
windows-1251
|
|
|
|
|
windows-1252
|
|
|
|
|
windows-1253
|
|
|
|
|
windows-1254
|
|
|
|
|
windows-1255
|
|
|
|
|
windows-1256
|
|
|
|
|
windows-1257
|
|
|
|
|
windows-1258
|
|
|
|
|
windows-31j
|
|
|
|
|
ISO-2022-JP
|
|
|
|
|
ISO-2022-KR
|
|
|
|
|
ISO-8859-1
|
|
|
|
|
ISO-8859-2
|
|
|
|
|
ISO-8859-3
|
|
|
|
|
ISO-8859-4
|
|
|
|
|
ISO-8859-5
|
|
|
|
|
ISO-8859-6
|
|
|
|
|
ISO-8859-7
|
|
|
|
|
ISO-8859-8
|
|
|
|
|
ISO-8859-9
|
|
|
|
|
ISO-8859-13
|
|
|
|
|
ISO-8859-15
|
|
|
|
|
KOI8-R
|
|
|
|
|
UTF-8
|
|
|
|
|
UTF-16
|
|
|
|
|
UTF-16BE
|
|
|
|
|
UTF-16LE
|
|
|
|
|
EUC-JP
|
|
|
|
|
Shift_JIS
|
2011-02-21 09:41:34 +00:00
|
|
|
CP932
|
2008-08-26 12:13:15 +00:00
|
|
|
GB18030
|
|
|
|
|
GBK
|
|
|
|
|
ISCII91
|
|
|
|
|
EUC-KR
|
|
|
|
|
Big5
|
|
|
|
|
Big5-HKSCS
|
|
|
|
|
TIS-620)
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2007-01-21 11:50:22 +00:00
|
|
|
cattr_accessor :available_settings
|
2014-12-05 15:00:27 +00:00
|
|
|
self.available_settings ||= {}
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2020-10-05 15:47:26 +00:00
|
|
|
validates_uniqueness_of(
|
|
|
|
|
:name,
|
|
|
|
|
:if => Proc.new do |setting|
|
|
|
|
|
setting.new_record? || setting.name_changed?
|
|
|
|
|
end
|
|
|
|
|
)
|
2014-12-05 15:00:27 +00:00
|
|
|
validates_inclusion_of :name, :in => Proc.new {available_settings.keys}
|
2020-10-05 15:47:26 +00:00
|
|
|
validates_numericality_of(
|
|
|
|
|
:value, :only_integer => true,
|
|
|
|
|
:if => Proc.new do |setting|
|
|
|
|
|
(s = available_settings[setting.name]) && s['format'] == 'int'
|
|
|
|
|
end
|
|
|
|
|
)
|
2007-08-31 17:45:32 +00:00
|
|
|
|
|
|
|
|
# Hash used to cache setting values
|
|
|
|
|
@cached_settings = {}
|
|
|
|
|
@cached_cleared_on = Time.now
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2007-09-14 15:30:46 +00:00
|
|
|
def value
|
|
|
|
|
v = read_attribute(:value)
|
|
|
|
|
# Unserialize serialized settings
|
2015-03-15 14:38:46 +00:00
|
|
|
if available_settings[name]['serialized'] && v.is_a?(String)
|
|
|
|
|
v = YAML::load(v)
|
|
|
|
|
v = force_utf8_strings(v)
|
|
|
|
|
end
|
2014-12-05 15:00:27 +00:00
|
|
|
v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
|
2007-09-14 15:30:46 +00:00
|
|
|
v
|
|
|
|
|
end
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2007-09-18 17:50:17 +00:00
|
|
|
def value=(v)
|
2014-12-05 15:00:27 +00:00
|
|
|
v = v.to_yaml if v && available_settings[name] && available_settings[name]['serialized']
|
2008-01-25 10:31:06 +00:00
|
|
|
write_attribute(:value, v.to_s)
|
2007-09-18 17:50:17 +00:00
|
|
|
end
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2007-08-31 17:45:32 +00:00
|
|
|
# Returns the value of the setting named name
|
2007-01-21 11:50:22 +00:00
|
|
|
def self.[](name)
|
2019-08-26 07:16:55 +00:00
|
|
|
@cached_settings[name] ||= find_or_default(name).value
|
2007-01-21 11:50:22 +00:00
|
|
|
end
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2007-09-14 15:30:46 +00:00
|
|
|
def self.[]=(name, v)
|
2007-08-31 17:45:32 +00:00
|
|
|
setting = find_or_default(name)
|
2019-08-26 07:16:55 +00:00
|
|
|
setting.value = v || ''
|
2007-08-31 17:45:32 +00:00
|
|
|
@cached_settings[name] = nil
|
2007-01-21 11:50:22 +00:00
|
|
|
setting.save
|
|
|
|
|
setting.value
|
|
|
|
|
end
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2017-07-04 04:00:23 +00:00
|
|
|
# Updates multiple settings from params and sends a security notification if needed
|
2016-02-05 08:50:21 +00:00
|
|
|
def self.set_all_from_params(settings)
|
2016-12-13 18:46:29 +00:00
|
|
|
return nil unless settings.is_a?(Hash)
|
2019-11-25 09:08:02 +00:00
|
|
|
|
2016-07-21 18:14:13 +00:00
|
|
|
settings = settings.dup.symbolize_keys
|
2016-12-13 18:46:29 +00:00
|
|
|
|
|
|
|
|
errors = validate_all_from_params(settings)
|
|
|
|
|
return errors if errors.present?
|
|
|
|
|
|
2016-02-05 08:50:21 +00:00
|
|
|
changes = []
|
|
|
|
|
settings.each do |name, value|
|
2016-07-21 18:14:13 +00:00
|
|
|
next unless available_settings[name.to_s]
|
2019-11-25 09:08:02 +00:00
|
|
|
|
2016-02-05 08:50:21 +00:00
|
|
|
previous_value = Setting[name]
|
|
|
|
|
set_from_params name, value
|
|
|
|
|
if available_settings[name.to_s]['security_notifications'] && Setting[name] != previous_value
|
|
|
|
|
changes << name
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if changes.any?
|
2018-10-10 17:13:09 +00:00
|
|
|
Mailer.deliver_settings_updated(User.current, changes)
|
2016-02-05 08:50:21 +00:00
|
|
|
end
|
2016-12-13 18:46:29 +00:00
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.validate_all_from_params(settings)
|
|
|
|
|
messages = []
|
2019-10-22 11:30:41 +00:00
|
|
|
[
|
2019-11-24 12:57:50 +00:00
|
|
|
[:mail_handler_enable_regex_delimiters,
|
|
|
|
|
:mail_handler_body_delimiters,
|
|
|
|
|
/[\r\n]+/],
|
|
|
|
|
[:mail_handler_enable_regex_excluded_filenames,
|
|
|
|
|
:mail_handler_excluded_filenames,
|
|
|
|
|
/\s*,\s*/]
|
2018-03-03 04:29:54 +00:00
|
|
|
].each do |enable_regex, regex_field, delimiter|
|
2018-03-03 04:32:16 +00:00
|
|
|
if settings.key?(regex_field) || settings.key?(enable_regex)
|
|
|
|
|
regexp = Setting.send("#{enable_regex}?")
|
|
|
|
|
if settings.key?(enable_regex)
|
|
|
|
|
regexp = settings[enable_regex].to_s != '0'
|
|
|
|
|
end
|
|
|
|
|
if regexp
|
|
|
|
|
settings[regex_field].to_s.split(delimiter).each do |value|
|
|
|
|
|
begin
|
|
|
|
|
Regexp.new(value)
|
|
|
|
|
rescue RegexpError => e
|
|
|
|
|
messages << [regex_field, "#{l('activerecord.errors.messages.not_a_regexp')} (#{e.message})"]
|
|
|
|
|
end
|
2016-12-13 18:46:29 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-08-26 04:18:18 +00:00
|
|
|
if settings.key?(:mail_from)
|
|
|
|
|
begin
|
|
|
|
|
mail_from = Mail::Address.new(settings[:mail_from])
|
2020-10-23 00:13:38 +00:00
|
|
|
raise unless EmailAddress::EMAIL_REGEXP.match?(mail_from.address)
|
2019-08-26 04:18:18 +00:00
|
|
|
rescue
|
|
|
|
|
messages << [:mail_from, l('activerecord.errors.messages.invalid')]
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-12-13 18:46:29 +00:00
|
|
|
messages
|
2016-02-05 08:50:21 +00:00
|
|
|
end
|
|
|
|
|
|
2013-10-05 09:41:11 +00:00
|
|
|
# Sets a setting value from params
|
|
|
|
|
def self.set_from_params(name, params)
|
|
|
|
|
params = params.dup
|
|
|
|
|
params.delete_if {|v| v.blank? } if params.is_a?(Array)
|
2014-10-22 17:37:16 +00:00
|
|
|
params.symbolize_keys! if params.is_a?(Hash)
|
2013-10-05 09:41:11 +00:00
|
|
|
|
|
|
|
|
m = "#{name}_from_params"
|
|
|
|
|
if respond_to? m
|
|
|
|
|
self[name.to_sym] = send m, params
|
|
|
|
|
else
|
|
|
|
|
self[name.to_sym] = params
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Returns a hash suitable for commit_update_keywords setting
|
|
|
|
|
#
|
|
|
|
|
# Example:
|
|
|
|
|
# params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
|
|
|
|
|
# Setting.commit_update_keywords_from_params(params)
|
2013-10-13 07:37:49 +00:00
|
|
|
# # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
|
2013-10-05 09:41:11 +00:00
|
|
|
def self.commit_update_keywords_from_params(params)
|
2013-10-13 07:37:49 +00:00
|
|
|
s = []
|
2013-10-05 09:41:11 +00:00
|
|
|
if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
|
|
|
|
|
attributes = params.except(:keywords).keys
|
|
|
|
|
params[:keywords].each_with_index do |keywords, i|
|
|
|
|
|
next if keywords.blank?
|
2019-11-25 09:08:02 +00:00
|
|
|
|
2013-10-13 07:37:49 +00:00
|
|
|
s << attributes.inject({}) {|h, a|
|
2013-10-05 09:41:11 +00:00
|
|
|
value = params[a][i].to_s
|
|
|
|
|
h[a.to_s] = value if value.present?
|
|
|
|
|
h
|
2013-10-13 07:37:49 +00:00
|
|
|
}.merge('keywords' => keywords)
|
2013-10-05 09:41:11 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
s
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-29 06:37:09 +00:00
|
|
|
def self.twofa_from_params(params)
|
|
|
|
|
# unpair all current 2FA pairings when switching off 2FA
|
|
|
|
|
Redmine::Twofa.unpair_all! if params == '0' && self.twofa?
|
|
|
|
|
params
|
|
|
|
|
end
|
|
|
|
|
|
2007-12-29 11:36:30 +00:00
|
|
|
# Helper that returns an array based on per_page_options setting
|
|
|
|
|
def self.per_page_options_array
|
|
|
|
|
per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
|
|
|
|
|
end
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2013-10-05 09:41:11 +00:00
|
|
|
# Helper that returns a Hash with single update keywords as keys
|
2013-10-13 07:37:49 +00:00
|
|
|
def self.commit_update_keywords_array
|
|
|
|
|
a = []
|
|
|
|
|
if commit_update_keywords.is_a?(Array)
|
|
|
|
|
commit_update_keywords.each do |rule|
|
|
|
|
|
next unless rule.is_a?(Hash)
|
2019-11-25 09:08:02 +00:00
|
|
|
|
2013-10-13 07:37:49 +00:00
|
|
|
rule = rule.dup
|
|
|
|
|
rule.delete_if {|k, v| v.blank?}
|
|
|
|
|
keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
|
|
|
|
|
next if keywords.empty?
|
2019-11-25 09:08:02 +00:00
|
|
|
|
2013-10-13 07:37:49 +00:00
|
|
|
a << rule.merge('keywords' => keywords)
|
2013-10-05 09:41:11 +00:00
|
|
|
end
|
|
|
|
|
end
|
2013-10-13 07:37:49 +00:00
|
|
|
a
|
2013-10-05 09:41:11 +00:00
|
|
|
end
|
|
|
|
|
|
2009-02-12 21:25:50 +00:00
|
|
|
def self.openid?
|
2009-02-19 21:03:08 +00:00
|
|
|
Object.const_defined?(:OpenID) && self[:openid].to_i > 0
|
2009-02-12 21:25:50 +00:00
|
|
|
end
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2007-08-31 17:45:32 +00:00
|
|
|
# Checks if settings have changed since the values were read
|
|
|
|
|
# and clears the cache hash if it's the case
|
|
|
|
|
# Called once per request
|
|
|
|
|
def self.check_cache
|
|
|
|
|
settings_updated_on = Setting.maximum(:updated_on)
|
|
|
|
|
if settings_updated_on && @cached_cleared_on <= settings_updated_on
|
2011-11-13 15:20:31 +00:00
|
|
|
clear_cache
|
2007-08-31 17:45:32 +00:00
|
|
|
end
|
|
|
|
|
end
|
2013-03-25 13:39:56 +00:00
|
|
|
|
2011-11-13 15:20:31 +00:00
|
|
|
# Clears the settings cache
|
|
|
|
|
def self.clear_cache
|
|
|
|
|
@cached_settings.clear
|
|
|
|
|
@cached_cleared_on = Time.now
|
|
|
|
|
logger.info "Settings cache cleared." if logger
|
|
|
|
|
end
|
2011-05-18 02:19:08 +00:00
|
|
|
|
2014-12-05 15:00:27 +00:00
|
|
|
def self.define_plugin_setting(plugin)
|
|
|
|
|
if plugin.settings
|
|
|
|
|
name = "plugin_#{plugin.id}"
|
|
|
|
|
define_setting name, {'default' => plugin.settings[:default], 'serialized' => true}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Defines getter and setter for each setting
|
|
|
|
|
# Then setting values can be read using: Setting.some_setting_name
|
|
|
|
|
# or set using Setting.some_setting_name = "some value"
|
|
|
|
|
def self.define_setting(name, options={})
|
|
|
|
|
available_settings[name.to_s] = options
|
|
|
|
|
|
2019-10-13 16:22:54 +00:00
|
|
|
src = <<~END_SRC
|
|
|
|
|
def self.#{name}
|
|
|
|
|
self[:#{name}]
|
|
|
|
|
end
|
2014-12-05 15:00:27 +00:00
|
|
|
|
2019-10-13 16:22:54 +00:00
|
|
|
def self.#{name}?
|
|
|
|
|
self[:#{name}].to_i > 0
|
|
|
|
|
end
|
2014-12-05 15:00:27 +00:00
|
|
|
|
2019-10-13 16:22:54 +00:00
|
|
|
def self.#{name}=(value)
|
|
|
|
|
self[:#{name}] = value
|
|
|
|
|
end
|
|
|
|
|
END_SRC
|
2014-12-05 15:00:27 +00:00
|
|
|
class_eval src, __FILE__, __LINE__
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.load_available_settings
|
|
|
|
|
YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options|
|
|
|
|
|
define_setting name, options
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.load_plugin_settings
|
|
|
|
|
Redmine::Plugin.all.each do |plugin|
|
|
|
|
|
define_plugin_setting(plugin)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
load_available_settings
|
|
|
|
|
load_plugin_settings
|
|
|
|
|
|
2019-10-22 11:30:29 +00:00
|
|
|
private
|
2015-03-15 14:38:46 +00:00
|
|
|
|
|
|
|
|
def force_utf8_strings(arg)
|
2015-03-15 20:27:12 +00:00
|
|
|
if arg.is_a?(String)
|
2015-03-15 14:38:46 +00:00
|
|
|
arg.dup.force_encoding('UTF-8')
|
|
|
|
|
elsif arg.is_a?(Array)
|
|
|
|
|
arg.map do |a|
|
|
|
|
|
force_utf8_strings(a)
|
|
|
|
|
end
|
|
|
|
|
elsif arg.is_a?(Hash)
|
|
|
|
|
arg = arg.dup
|
2020-10-05 15:48:22 +00:00
|
|
|
arg.each do |k, v|
|
2015-03-15 14:38:46 +00:00
|
|
|
arg[k] = force_utf8_strings(v)
|
|
|
|
|
end
|
|
|
|
|
arg
|
|
|
|
|
else
|
|
|
|
|
arg
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2007-08-31 17:45:32 +00:00
|
|
|
# Returns the Setting instance for the setting named name
|
|
|
|
|
# (record found in database or new record with default value)
|
|
|
|
|
def self.find_or_default(name)
|
|
|
|
|
name = name.to_s
|
2014-12-05 15:00:27 +00:00
|
|
|
raise "There's no setting named #{name}" unless available_settings.has_key?(name)
|
2019-11-25 09:08:02 +00:00
|
|
|
|
2014-12-05 15:03:29 +00:00
|
|
|
setting = where(:name => name).order(:id => :desc).first
|
2012-02-19 15:34:06 +00:00
|
|
|
unless setting
|
2013-12-20 11:54:23 +00:00
|
|
|
setting = new
|
|
|
|
|
setting.name = name
|
2014-12-05 15:00:27 +00:00
|
|
|
setting.value = available_settings[name]['default']
|
2012-02-19 15:34:06 +00:00
|
|
|
end
|
|
|
|
|
setting
|
2007-08-31 17:45:32 +00:00
|
|
|
end
|
2019-10-16 14:36:31 +00:00
|
|
|
private_class_method :find_or_default
|
2007-01-21 11:50:22 +00:00
|
|
|
end
|