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 4.9KB

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