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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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. require 'redmine'
  19. module Redmine
  20. module Configuration
  21. # Configuration default values
  22. @defaults = {
  23. 'avatar_server_url' => 'https://www.gravatar.com',
  24. 'email_delivery' => nil,
  25. 'max_concurrent_ajax_uploads' => 2,
  26. 'common_mark_enable_hardbreaks' => true
  27. }
  28. @config = nil
  29. class << self
  30. # Loads the Redmine configuration file
  31. # Valid options:
  32. # * <tt>:file</tt>: the configuration file to load (default: config/configuration.yml)
  33. # * <tt>:env</tt>: the environment to load the configuration for (default: Rails.env)
  34. def load(options={})
  35. filename = options[:file] || File.join(Rails.root, 'config', 'configuration.yml')
  36. env = options[:env] || Rails.env
  37. @config = @defaults.dup
  38. load_deprecated_email_configuration(env)
  39. if File.file?(filename)
  40. @config.merge!(load_from_yaml(filename, env))
  41. end
  42. # Compatibility mode for those who copy email.yml over configuration.yml
  43. %w(delivery_method smtp_settings sendmail_settings).each do |key|
  44. if value = @config.delete(key)
  45. @config['email_delivery'] ||= {}
  46. @config['email_delivery'][key] = value
  47. end
  48. end
  49. if @config['email_delivery']
  50. ActionMailer::Base.perform_deliveries = true
  51. @config['email_delivery'].each do |k, v|
  52. # Comprehensive error message for those who used async_smtp and async_sendmail
  53. # delivery methods that are removed in Redmine 4.0.
  54. if k == 'delivery_method' && v.to_s =~ /\Aasync_(.+)/
  55. abort "Redmine now uses ActiveJob to send emails asynchronously and the :#{v} delivery method is no longer available.\n" +
  56. "Please update your config/configuration.yml to use :#$1 delivery method instead."
  57. end
  58. v.symbolize_keys! if v.respond_to?(:symbolize_keys!)
  59. ActionMailer::Base.send("#{k}=", v)
  60. end
  61. end
  62. check_regular_expressions
  63. @config
  64. end
  65. # Returns a configuration setting
  66. def [](name)
  67. load unless @config
  68. @config[name]
  69. end
  70. # Yields a block with the specified hash configuration settings
  71. def with(settings)
  72. settings.stringify_keys!
  73. load unless @config
  74. was = settings.keys.inject({}) {|h,v| h[v] = @config[v]; h}
  75. @config.merge! settings
  76. yield if block_given?
  77. @config.merge! was
  78. end
  79. private
  80. def load_from_yaml(filename, env)
  81. yaml = nil
  82. begin
  83. yaml = YAML::load(ERB.new(File.read(filename)).result)
  84. rescue ArgumentError, Psych::SyntaxError => e
  85. abort "Your Redmine configuration file located at #{filename} is not a valid YAML file and could not be loaded:\n#{e.message}"
  86. rescue SyntaxError => e
  87. abort "A syntax error occurred when parsing your Redmine configuration file located at #{filename} with ERB:\n#{e.message}"
  88. end
  89. conf = {}
  90. if yaml.is_a?(Hash)
  91. if yaml['default']
  92. conf.merge!(yaml['default'])
  93. end
  94. if yaml[env]
  95. conf.merge!(yaml[env])
  96. end
  97. else
  98. abort "Your Redmine configuration file located at #{filename} is not a valid Redmine configuration file."
  99. end
  100. conf
  101. end
  102. def load_deprecated_email_configuration(env)
  103. deprecated_email_conf = File.join(Rails.root, 'config', 'email.yml')
  104. if File.file?(deprecated_email_conf)
  105. 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."
  106. @config.merge!({'email_delivery' => load_from_yaml(deprecated_email_conf, env)})
  107. end
  108. end
  109. # Checks the validness of regular expressions set for repository paths at startup
  110. def check_regular_expressions
  111. @config.each do |name, value|
  112. if value.present? && /^scm_.+_path_regexp$/.match?(name)
  113. begin
  114. Regexp.new value.to_s.strip
  115. rescue => e
  116. abort "Invalid regular expression set as #{name} setting in your Redmine configuration file:\n#{e.message}"
  117. end
  118. end
  119. end
  120. end
  121. end
  122. end
  123. end