Class: Setting
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Setting
- Defined in:
- app/models/setting.rb
Overview
Redmine - project management software Copyright © 2006- Jean-Philippe Lang
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.
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.
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.
Constant Summary collapse
- PASSWORD_CHAR_CLASSES =
{ 'uppercase' => /[A-Z]/, 'lowercase' => /[a-z]/, 'digits' => /[0-9]/, 'special_chars' => /[[:ascii:]&&[:graph:]&&[:^alnum:]]/ }
- DATE_FORMATS =
[ '%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' ]
- TIME_FORMATS =
[ '%H:%M', '%I:%M %p' ]
- ENCODINGS =
%w(US-ASCII windows-1250 windows-1251 windows-1252 windows-1253 windows-1254 windows-1255 windows-1256 windows-1257 windows-1258 windows-31j windows-874 ISO-2022-JP 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 CP932 CP949 GB18030 GBK EUC-KR Big5 Big5-HKSCS TIS-620)
Class Method Summary collapse
-
.[](name) ⇒ Object
Returns the value of the setting named name.
- .[]=(name, v) ⇒ Object
-
.check_cache ⇒ Object
Checks if settings have changed since the values were read and clears the cache hash if it’s the case Called once per request.
-
.clear_cache ⇒ Object
Clears the settings cache.
-
.commit_update_keywords_array ⇒ Object
Helper that returns a Hash with single update keywords as keys.
-
.commit_update_keywords_from_params(params) ⇒ Object
Returns a hash suitable for commit_update_keywords setting.
- .define_plugin_setting(plugin) ⇒ Object
-
.define_setting(name, options = {}) ⇒ Object
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”.
- .load_available_settings ⇒ Object
- .load_plugin_settings ⇒ Object
-
.per_page_options_array ⇒ Object
Helper that returns an array based on per_page_options setting.
-
.set_all_from_params(settings) ⇒ Object
Updates multiple settings from params and sends a security notification if needed.
-
.set_from_params(name, params) ⇒ Object
Sets a setting value from params.
- .twofa_from_params(params) ⇒ Object
- .twofa_optional? ⇒ Boolean
- .twofa_required? ⇒ Boolean
- .twofa_required_for_administrators? ⇒ Boolean
- .validate_all_from_params(settings) ⇒ Object
Instance Method Summary collapse
Methods inherited from ApplicationRecord
Class Method Details
.[](name) ⇒ Object
Returns the value of the setting named name
124 125 126 |
# File 'app/models/setting.rb', line 124 def self.[](name) @cached_settings[name] ||= find_or_default(name).value end |
.[]=(name, v) ⇒ Object
128 129 130 131 132 133 134 |
# File 'app/models/setting.rb', line 128 def self.[]=(name, v) setting = find_or_default(name) setting.value = v || '' @cached_settings[name] = nil setting.save setting.value end |
.check_cache ⇒ Object
Checks if settings have changed since the values were read and clears the cache hash if it’s the case Called once per request
279 280 281 282 283 284 |
# File 'app/models/setting.rb', line 279 def self.check_cache settings_updated_on = Setting.maximum(:updated_on) if settings_updated_on && @cached_cleared_on <= settings_updated_on clear_cache end end |
.clear_cache ⇒ Object
Clears the settings cache
287 288 289 290 291 |
# File 'app/models/setting.rb', line 287 def self.clear_cache @cached_settings.clear @cached_cleared_on = Time.now logger.info "Settings cache cleared." if logger end |
.commit_update_keywords_array ⇒ Object
Helper that returns a Hash with single update keywords as keys
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'app/models/setting.rb', line 259 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) 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? a << rule.merge('keywords' => keywords) end end a end |
.commit_update_keywords_from_params(params) ⇒ Object
Returns a hash suitable for commit_update_keywords setting
Example: params = => [‘fixes’, ‘closes’], :status_id => [“3”, “5”], :done_ratio => [“”, “100”] Setting.commit_update_keywords_from_params(params) # => [=> ’fixes’, ‘status_id’ => “3”, => ’closes’, ‘status_id’ => “5”, ‘done_ratio’ => “100”]
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/models/setting.rb', line 218 def self.commit_update_keywords_from_params(params) s = [] if params.is_a?(Hash) && params.key?(:keywords) && params.values.all?(Array) attributes = params.except(:keywords).keys params[:keywords].each_with_index do |keywords, i| next if keywords.blank? s << attributes.inject({}) do |h, a| value = params[a][i].to_s h[a.to_s] = value if value.present? h end.merge('keywords' => keywords) end end s end |
.define_plugin_setting(plugin) ⇒ Object
293 294 295 296 297 298 |
# File 'app/models/setting.rb', line 293 def self.define_plugin_setting(plugin) if plugin.settings name = "plugin_#{plugin.id}" define_setting name, {'default' => plugin.settings[:default], 'serialized' => true} end end |
.define_setting(name, options = {}) ⇒ Object
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”
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'app/models/setting.rb', line 303 def self.define_setting(name, ={}) available_settings[name.to_s] = src = <<~END_SRC def self.#{name} self[:#{name}] end def self.#{name}? self[:#{name}].to_i > 0 end def self.#{name}=(value) self[:#{name}] = value end END_SRC class_eval src, __FILE__, __LINE__ end |
.load_available_settings ⇒ Object
322 323 324 325 326 |
# File 'app/models/setting.rb', line 322 def self.load_available_settings YAML.load_file(Rails.root.join('config/settings.yml')).each do |name, | define_setting name, end end |
.load_plugin_settings ⇒ Object
328 329 330 331 332 |
# File 'app/models/setting.rb', line 328 def self.load_plugin_settings Redmine::Plugin.all.each do |plugin| define_plugin_setting(plugin) end end |
.per_page_options_array ⇒ Object
Helper that returns an array based on per_page_options setting
254 255 256 |
# File 'app/models/setting.rb', line 254 def self. .split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort end |
.set_all_from_params(settings) ⇒ Object
Updates multiple settings from params and sends a security notification if needed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'app/models/setting.rb', line 137 def self.set_all_from_params(settings) return nil unless settings.is_a?(Hash) settings = settings.dup.symbolize_keys errors = validate_all_from_params(settings) return errors if errors.present? changes = [] settings.each do |name, value| next unless available_settings[name.to_s] 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? Mailer.deliver_settings_updated(User.current, changes) end nil end |
.set_from_params(name, params) ⇒ Object
Sets a setting value from params
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'app/models/setting.rb', line 199 def self.set_from_params(name, params) params = params.dup params.delete_if {|v| v.blank?} if params.is_a?(Array) params.symbolize_keys! if params.is_a?(Hash) m = "#{name}_from_params" if respond_to? m self[name.to_sym] = send m, params else self[name.to_sym] = params end end |
.twofa_from_params(params) ⇒ Object
235 236 237 238 239 |
# File 'app/models/setting.rb', line 235 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 |
.twofa_optional? ⇒ Boolean
245 246 247 |
# File 'app/models/setting.rb', line 245 def self.twofa_optional? %w[1 3].include? twofa end |
.twofa_required? ⇒ Boolean
241 242 243 |
# File 'app/models/setting.rb', line 241 def self.twofa_required? twofa == '2' end |
.twofa_required_for_administrators? ⇒ Boolean
249 250 251 |
# File 'app/models/setting.rb', line 249 def self.twofa_required_for_administrators? twofa == '3' end |
.validate_all_from_params(settings) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/models/setting.rb', line 161 def self.validate_all_from_params(settings) = [] [ [:mail_handler_enable_regex_delimiters, :mail_handler_body_delimiters, /[\r\n]+/], [:mail_handler_enable_regex_excluded_filenames, :mail_handler_excluded_filenames, /\s*,\s*/] ].each do |enable_regex, regex_field, delimiter| 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 << [regex_field, "#{l('activerecord.errors.messages.not_a_regexp')} (#{e.})"] end end end end end if settings.key?(:mail_from) begin mail_from = Mail::Address.new(settings[:mail_from]) raise unless URI::MailTo::EMAIL_REGEXP.match?(mail_from.address) rescue << [:mail_from, l('activerecord.errors.messages.invalid')] end end end |
Instance Method Details
#value ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/setting.rb', line 107 def value v = read_attribute(:value) # Unserialize serialized settings if available_settings[name]['serialized'] && v.is_a?(String) v = YAML.safe_load(v, permitted_classes: Rails.configuration.active_record.yaml_column_permitted_classes) v = force_utf8_strings(v) end v = v.to_sym if available_settings[name]['format'] == 'symbol' && v.present? v end |
#value=(v) ⇒ Object
118 119 120 121 |
# File 'app/models/setting.rb', line 118 def value=(v) v = v.to_yaml if v && available_settings[name] && available_settings[name]['serialized'] write_attribute(:value, v.to_s) end |