You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configuration.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module Configuration
  20. # Configuration default values
  21. @defaults = {
  22. 'avatar_server_url' => 'https://www.gravatar.com',
  23. 'email_delivery' => nil,
  24. 'max_concurrent_ajax_uploads' => 2
  25. }
  26. @config = nil
  27. class << self
  28. # Loads the Redmine configuration file
  29. # Valid options:
  30. # * <tt>:file</tt>: the configuration file to load (default: config/configuration.yml)
  31. # * <tt>:env</tt>: the environment to load the configuration for (default: Rails.env)
  32. def load(options={})
  33. filename = options[:file] || File.join(Rails.root, 'config', 'configuration.yml')
  34. env = options[:env] || Rails.env
  35. @config = @defaults.dup
  36. load_deprecated_email_configuration(env)
  37. if File.file?(filename)
  38. @config.merge!(load_from_yaml(filename, env))
  39. end
  40. # Compatibility mode for those who copy email.yml over configuration.yml
  41. %w(delivery_method smtp_settings sendmail_settings).each do |key|
  42. if value = @config.delete(key)
  43. @config['email_delivery'] ||= {}
  44. @config['email_delivery'][key] = value
  45. end
  46. end
  47. if @config['email_delivery']
  48. ActionMailer::Base.perform_deliveries = true
  49. @config['email_delivery'].each do |k, v|
  50. # Comprehensive error message for those who used async_smtp and async_sendmail
  51. # delivery methods that are removed in Redmine 4.0.
  52. if k == 'delivery_method' && v.to_s =~ /\Aasync_(.+)/
  53. abort "Redmine now uses ActiveJob to send emails asynchronously and the :#{v} delivery method is no longer available.\n" +
  54. "Please update your config/configuration.yml to use :#$1 delivery method instead."
  55. end
  56. v.symbolize_keys! if v.respond_to?(:symbolize_keys!)
  57. ActionMailer::Base.send("#{k}=", v)
  58. end
  59. end
  60. check_regular_expressions
  61. @config
  62. end
  63. # Returns a configuration setting
  64. def [](name)
  65. load unless @config
  66. @config[name]
  67. end
  68. # Yields a block with the specified hash configuration settings
  69. def with(settings)
  70. settings.stringify_keys!
  71. load unless @config
  72. was = settings.keys.inject({}) {|h,v| h[v] = @config[v]; h}
  73. @config.merge! settings
  74. yield if block_given?
  75. @config.merge! was
  76. end
  77. private
  78. def load_from_yaml(filename, env)
  79. yaml = nil
  80. begin
  81. yaml = YAML::load(ERB.new(File.read(filename)).result)
  82. rescue ArgumentError
  83. abort "Your Redmine configuration file located at #{filename} is not a valid YAML file and could not be loaded."
  84. rescue SyntaxError => e
  85. abort "A syntax error occurred when parsing your Redmine configuration file located at #{filename} with ERB:\n#{e.message}"
  86. end
  87. conf = {}
  88. if yaml.is_a?(Hash)
  89. if yaml['default']
  90. conf.merge!(yaml['default'])
  91. end
  92. if yaml[env]
  93. conf.merge!(yaml[env])
  94. end
  95. else
  96. abort "Your Redmine configuration file located at #{filename} is not a valid Redmine configuration file."
  97. end
  98. conf
  99. end
  100. def load_deprecated_email_configuration(env)
  101. deprecated_email_conf = File.join(Rails.root, 'config', 'email.yml')
  102. if File.file?(deprecated_email_conf)
  103. warn "Storing outgoing emails configuration in config/email.yml is deprecated. You should now store it in config/configuration.yml using the email_delivery setting."
  104. @config.merge!({'email_delivery' => load_from_yaml(deprecated_email_conf, env)})
  105. end
  106. end
  107. # Checks the validness of regular expressions set for repository paths at startup
  108. def check_regular_expressions
  109. @config.each do |name, value|
  110. if value.present? && /^scm_.+_path_regexp$/.match?(name)
  111. begin
  112. Regexp.new value.to_s.strip
  113. rescue => e
  114. abort "Invalid regular expression set as #{name} setting in your Redmine configuration file:\n#{e.message}"
  115. end
  116. end
  117. end
  118. end
  119. end
  120. end
  121. end