Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow settings defaults to be lambdas even if format present

Oliver Günther2b05d50b980f
diff --git a/app/helpers/static_links_helper.rb b/app/helpers/static_links_helper.rb
index e0c7bac89df..facf52b9d52 100644
--- a/app/helpers/static_links_helper.rb
+++ b/app/helpers/static_links_helper.rb
@@ -32,7 +32,7 @@ module StaticLinksHelper
def static_link_to(key, label: nil)
item = OpenProject::Static::Links.links.fetch key
- link_to label || t(item[:label], default: item[:label]),
+ link_to label || t(item[:label]),
item[:href],
class: 'openproject--static-link',
target: '_blank', rel: 'noopener'
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 7775830faec..c3bd545e28f 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -381,6 +381,8 @@ module Settings
def cast(value)
return nil if value.nil?
+ value = value.call if value.respond_to?(:call)
+
case format
when :integer
value.to_i
@@ -391,11 +393,7 @@ module Settings
when :symbol
value.to_sym
else
- if value.respond_to?(:call)
- value.call
- else
- value
- end
+ value
end
end
diff --git a/config/constants/settings/definitions.rb b/config/constants/settings/definitions.rb
index fab20d6e42d..ca4ee22f88c 100644
--- a/config/constants/settings/definitions.rb
+++ b/config/constants/settings/definitions.rb
@@ -668,14 +668,14 @@ Settings::Definition.define do
# Assume we're running in an TLS terminated connection.
add :https,
format: :boolean,
- default: Rails.env.production?,
+ default: -> { Rails.env.production? },
writable: false
# Allow disabling of HSTS headers and http -> https redirects
# for non-localhost hosts
add :hsts,
format: :boolean,
- default: -> { https? },
+ default: true,
writable: false
add :registration_footer,
diff --git a/config/environments/production.rb b/config/environments/production.rb
index e8313eeea64..390bbf383c2 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -80,10 +80,10 @@ OpenProject::Application.configure do
config.ssl_options = {
# Disable redirect on the internal SYS API
redirect: {
- hsts: OpenProject::Configuration.hsts?,
+ hsts: OpenProject::Configuration.hsts_enabled?,
exclude: ->(request) do
# Disable redirects when hsts is disabled
- return true unless OpenProject::Configuration.hsts?
+ return true unless OpenProject::Configuration.hsts_enabled?
# Respect the relative URL
relative_url = Regexp.escape(OpenProject::Configuration['rails_relative_url_root'])
diff --git a/lib_static/open_project/configuration/helpers.rb b/lib_static/open_project/configuration/helpers.rb
index 7a061f7f1ec..d3cfd6344e1 100644
--- a/lib_static/open_project/configuration/helpers.rb
+++ b/lib_static/open_project/configuration/helpers.rb
@@ -38,6 +38,13 @@ module OpenProject
self['direct_uploads']
end
+ ##
+ # Is hsts really enabled? We only return true
+ # if the setting is true AND HTTPS mode is on.
+ def hsts_enabled?
+ https? && hsts?
+ end
+
##
# We only allow direct uploads to S3 as we are using the carrierwave_direct
# gem which only supports S3 for the time being.
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index 69751691b2c..613c071ef72 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -956,5 +956,18 @@ describe Settings::Definition do
.to eq 0.5
end
end
+
+ context 'with a boolean provided with a proc default' do
+ let(:instance) do
+ described_class.new 'bogus',
+ format: :boolean,
+ default: -> { false }
+ end
+
+ it 'calls the proc as a default' do
+ expect(instance.default)
+ .to be false
+ end
+ end
end
end