##// END OF EJS Templates
Make sure that settings are unserialized as UTF-8 encoded strings (#19305)....
Jean-Philippe Lang -
r13730:ed2a3a224498
parent child
Show More
@@ -1,254 +1,276
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Setting < ActiveRecord::Base
19 19
20 20 DATE_FORMATS = [
21 21 '%Y-%m-%d',
22 22 '%d/%m/%Y',
23 23 '%d.%m.%Y',
24 24 '%d-%m-%Y',
25 25 '%m/%d/%Y',
26 26 '%d %b %Y',
27 27 '%d %B %Y',
28 28 '%b %d, %Y',
29 29 '%B %d, %Y'
30 30 ]
31 31
32 32 TIME_FORMATS = [
33 33 '%H:%M',
34 34 '%I:%M %p'
35 35 ]
36 36
37 37 ENCODINGS = %w(US-ASCII
38 38 windows-1250
39 39 windows-1251
40 40 windows-1252
41 41 windows-1253
42 42 windows-1254
43 43 windows-1255
44 44 windows-1256
45 45 windows-1257
46 46 windows-1258
47 47 windows-31j
48 48 ISO-2022-JP
49 49 ISO-2022-KR
50 50 ISO-8859-1
51 51 ISO-8859-2
52 52 ISO-8859-3
53 53 ISO-8859-4
54 54 ISO-8859-5
55 55 ISO-8859-6
56 56 ISO-8859-7
57 57 ISO-8859-8
58 58 ISO-8859-9
59 59 ISO-8859-13
60 60 ISO-8859-15
61 61 KOI8-R
62 62 UTF-8
63 63 UTF-16
64 64 UTF-16BE
65 65 UTF-16LE
66 66 EUC-JP
67 67 Shift_JIS
68 68 CP932
69 69 GB18030
70 70 GBK
71 71 ISCII91
72 72 EUC-KR
73 73 Big5
74 74 Big5-HKSCS
75 75 TIS-620)
76 76
77 77 cattr_accessor :available_settings
78 78 self.available_settings ||= {}
79 79
80 80 validates_uniqueness_of :name, :if => Proc.new {|setting| setting.new_record? || setting.name_changed?}
81 81 validates_inclusion_of :name, :in => Proc.new {available_settings.keys}
82 82 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting|
83 83 (s = available_settings[setting.name]) && s['format'] == 'int'
84 84 }
85 85 attr_protected :id
86 86
87 87 # Hash used to cache setting values
88 88 @cached_settings = {}
89 89 @cached_cleared_on = Time.now
90 90
91 91 def value
92 92 v = read_attribute(:value)
93 93 # Unserialize serialized settings
94 v = YAML::load(v) if available_settings[name]['serialized'] && v.is_a?(String)
94 if available_settings[name]['serialized'] && v.is_a?(String)
95 v = YAML::load(v)
96 v = force_utf8_strings(v)
97 end
95 98 v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
96 99 v
97 100 end
98 101
99 102 def value=(v)
100 103 v = v.to_yaml if v && available_settings[name] && available_settings[name]['serialized']
101 104 write_attribute(:value, v.to_s)
102 105 end
103 106
104 107 # Returns the value of the setting named name
105 108 def self.[](name)
106 109 v = @cached_settings[name]
107 110 v ? v : (@cached_settings[name] = find_or_default(name).value)
108 111 end
109 112
110 113 def self.[]=(name, v)
111 114 setting = find_or_default(name)
112 115 setting.value = (v ? v : "")
113 116 @cached_settings[name] = nil
114 117 setting.save
115 118 setting.value
116 119 end
117 120
118 121 # Sets a setting value from params
119 122 def self.set_from_params(name, params)
120 123 params = params.dup
121 124 params.delete_if {|v| v.blank? } if params.is_a?(Array)
122 125 params.symbolize_keys! if params.is_a?(Hash)
123 126
124 127 m = "#{name}_from_params"
125 128 if respond_to? m
126 129 self[name.to_sym] = send m, params
127 130 else
128 131 self[name.to_sym] = params
129 132 end
130 133 end
131 134
132 135 # Returns a hash suitable for commit_update_keywords setting
133 136 #
134 137 # Example:
135 138 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
136 139 # Setting.commit_update_keywords_from_params(params)
137 140 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
138 141 def self.commit_update_keywords_from_params(params)
139 142 s = []
140 143 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
141 144 attributes = params.except(:keywords).keys
142 145 params[:keywords].each_with_index do |keywords, i|
143 146 next if keywords.blank?
144 147 s << attributes.inject({}) {|h, a|
145 148 value = params[a][i].to_s
146 149 h[a.to_s] = value if value.present?
147 150 h
148 151 }.merge('keywords' => keywords)
149 152 end
150 153 end
151 154 s
152 155 end
153 156
154 157 # Helper that returns an array based on per_page_options setting
155 158 def self.per_page_options_array
156 159 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
157 160 end
158 161
159 162 # Helper that returns a Hash with single update keywords as keys
160 163 def self.commit_update_keywords_array
161 164 a = []
162 165 if commit_update_keywords.is_a?(Array)
163 166 commit_update_keywords.each do |rule|
164 167 next unless rule.is_a?(Hash)
165 168 rule = rule.dup
166 169 rule.delete_if {|k, v| v.blank?}
167 170 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
168 171 next if keywords.empty?
169 172 a << rule.merge('keywords' => keywords)
170 173 end
171 174 end
172 175 a
173 176 end
174 177
175 178 def self.openid?
176 179 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
177 180 end
178 181
179 182 # Checks if settings have changed since the values were read
180 183 # and clears the cache hash if it's the case
181 184 # Called once per request
182 185 def self.check_cache
183 186 settings_updated_on = Setting.maximum(:updated_on)
184 187 if settings_updated_on && @cached_cleared_on <= settings_updated_on
185 188 clear_cache
186 189 end
187 190 end
188 191
189 192 # Clears the settings cache
190 193 def self.clear_cache
191 194 @cached_settings.clear
192 195 @cached_cleared_on = Time.now
193 196 logger.info "Settings cache cleared." if logger
194 197 end
195 198
196 199 def self.define_plugin_setting(plugin)
197 200 if plugin.settings
198 201 name = "plugin_#{plugin.id}"
199 202 define_setting name, {'default' => plugin.settings[:default], 'serialized' => true}
200 203 end
201 204 end
202 205
203 206 # Defines getter and setter for each setting
204 207 # Then setting values can be read using: Setting.some_setting_name
205 208 # or set using Setting.some_setting_name = "some value"
206 209 def self.define_setting(name, options={})
207 210 available_settings[name.to_s] = options
208 211
209 212 src = <<-END_SRC
210 213 def self.#{name}
211 214 self[:#{name}]
212 215 end
213 216
214 217 def self.#{name}?
215 218 self[:#{name}].to_i > 0
216 219 end
217 220
218 221 def self.#{name}=(value)
219 222 self[:#{name}] = value
220 223 end
221 224 END_SRC
222 225 class_eval src, __FILE__, __LINE__
223 226 end
224 227
225 228 def self.load_available_settings
226 229 YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options|
227 230 define_setting name, options
228 231 end
229 232 end
230 233
231 234 def self.load_plugin_settings
232 235 Redmine::Plugin.all.each do |plugin|
233 236 define_plugin_setting(plugin)
234 237 end
235 238 end
236 239
237 240 load_available_settings
238 241 load_plugin_settings
239 242
240 243 private
244
245 def force_utf8_strings(arg)
246 if arg.is_a?(String)
247 arg.dup.force_encoding('UTF-8')
248 elsif arg.is_a?(Array)
249 arg.map do |a|
250 force_utf8_strings(a)
251 end
252 elsif arg.is_a?(Hash)
253 arg = arg.dup
254 arg.each do |k,v|
255 arg[k] = force_utf8_strings(v)
256 end
257 arg
258 else
259 arg
260 end
261 end
262
241 263 # Returns the Setting instance for the setting named name
242 264 # (record found in database or new record with default value)
243 265 def self.find_or_default(name)
244 266 name = name.to_s
245 267 raise "There's no setting named #{name}" unless available_settings.has_key?(name)
246 268 setting = where(:name => name).order(:id => :desc).first
247 269 unless setting
248 270 setting = new
249 271 setting.name = name
250 272 setting.value = available_settings[name]['default']
251 273 end
252 274 setting
253 275 end
254 276 end
@@ -1,104 +1,127
1 # encoding: utf-8
2 #
1 3 # Redmine - project management software
2 4 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 5 #
4 6 # This program is free software; you can redistribute it and/or
5 7 # modify it under the terms of the GNU General Public License
6 8 # as published by the Free Software Foundation; either version 2
7 9 # of the License, or (at your option) any later version.
8 10 #
9 11 # This program is distributed in the hope that it will be useful,
10 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 14 # GNU General Public License for more details.
13 15 #
14 16 # You should have received a copy of the GNU General Public License
15 17 # along with this program; if not, write to the Free Software
16 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 19
18 20 require File.expand_path('../../test_helper', __FILE__)
19 21
20 22 class SettingTest < ActiveSupport::TestCase
21 23
22 24 def teardown
23 25 Setting.clear_cache
24 26 end
25 27
26 28 def test_read_default
27 29 assert_equal "Redmine", Setting.app_title
28 30 assert Setting.self_registration?
29 31 assert !Setting.login_required?
30 32 end
31 33
32 34 def test_update
33 35 Setting.app_title = "My title"
34 36 assert_equal "My title", Setting.app_title
35 37 # make sure db has been updated (INSERT)
36 38 assert_equal "My title", Setting.find_by_name('app_title').value
37 39
38 40 Setting.app_title = "My other title"
39 41 assert_equal "My other title", Setting.app_title
40 42 # make sure db has been updated (UPDATE)
41 43 assert_equal "My other title", Setting.find_by_name('app_title').value
42 44 end
43 45
44 46 def test_setting_with_int_format_should_accept_numeric_only
45 47 with_settings :session_timeout => 30 do
46 48 Setting.session_timeout = 'foo'
47 49 assert_equal "30", Setting.session_timeout
48 50 Setting.session_timeout = 40
49 51 assert_equal "40", Setting.session_timeout
50 52 end
51 53 end
52 54
53 55 def test_setting_with_invalid_name_should_be_valid
54 56 setting = Setting.new(:name => "does_not_exist", :value => "should_not_be_allowed")
55 57 assert !setting.save
56 58 end
57 59
58 60 def test_serialized_setting
59 61 Setting.notified_events = ['issue_added', 'issue_updated', 'news_added']
60 62 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.notified_events
61 63 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.find_by_name('notified_events').value
62 64 end
63 65
64 66 def test_setting_should_be_reloaded_after_clear_cache
65 67 Setting.app_title = "My title"
66 68 assert_equal "My title", Setting.app_title
67 69
68 70 s = Setting.find_by_name("app_title")
69 71 s.value = 'New title'
70 72 s.save!
71 73 assert_equal "My title", Setting.app_title
72 74
73 75 Setting.clear_cache
74 76 assert_equal "New title", Setting.app_title
75 77 end
76 78
77 79 def test_per_page_options_array_should_be_an_empty_array_when_setting_is_blank
78 80 with_settings :per_page_options => nil do
79 81 assert_equal [], Setting.per_page_options_array
80 82 end
81 83
82 84 with_settings :per_page_options => '' do
83 85 assert_equal [], Setting.per_page_options_array
84 86 end
85 87 end
86 88
87 89 def test_per_page_options_array_should_be_an_array_of_integers
88 90 with_settings :per_page_options => '10, 25, 50' do
89 91 assert_equal [10, 25, 50], Setting.per_page_options_array
90 92 end
91 93 end
92 94
93 95 def test_per_page_options_array_should_omit_non_numerial_values
94 96 with_settings :per_page_options => 'a, 25, 50' do
95 97 assert_equal [25, 50], Setting.per_page_options_array
96 98 end
97 99 end
98 100
99 101 def test_per_page_options_array_should_be_sorted
100 102 with_settings :per_page_options => '25, 10, 50' do
101 103 assert_equal [10, 25, 50], Setting.per_page_options_array
102 104 end
103 105 end
106
107 def test_setting_serialied_as_binary_should_be_loaded_as_utf8_encoded_strings
108 yaml = <<-YAML
109 ---
110 - keywords: !binary |
111 Zml4ZXMsY2xvc2VzLNC40YHQv9GA0LDQstC70LXQvdC+LNCz0L7RgtC+0LLQ
112 vizRgdC00LXQu9Cw0L3QvixmaXhlZA==
113
114 done_ratio: "100"
115 status_id: "5"
116 YAML
117
118 Setting.commit_update_keywords = {}
119 assert_equal 1, Setting.where(:name => 'commit_update_keywords').update_all(:value => yaml)
120 Setting.clear_cache
121
122 assert_equal 'UTF-8', Setting.commit_update_keywords.first['keywords'].encoding.name
123 ensure
124 Setting.where(:name => 'commit_update_keywords').delete_all
125 Setting.clear_cache
126 end
104 127 end
General Comments 0
You need to be logged in to leave comments. Login now