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 setting timezone values

Oliver Günther64801fed54aa
diff --git a/app/models/setting.rb b/app/models/setting.rb
index ab6b64a12d0..6cd59e0ac10 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -356,6 +356,8 @@ class Setting < ApplicationRecord
Date.parse value
when :datetime
DateTime.parse value
+ when :timezone
+ ActiveSupport::TimeZone.lookup_timezone(value) || value
else
value
end
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index e0bb19bfe9f..d34ba4df7f5 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -400,6 +400,8 @@ module Settings
AR_BOOLEAN_TYPE.cast(value)
when :symbol
value.to_sym
+ when :timezone
+ ActiveSupport::TimeZone.lookup_timezone(value) || value
else
value
end
diff --git a/config/constants/settings/definitions.rb b/config/constants/settings/definitions.rb
index 44f4cf54c52..cd53ac531ed 100644
--- a/config/constants/settings/definitions.rb
+++ b/config/constants/settings/definitions.rb
@@ -922,7 +922,7 @@ Settings::Definition.define do
add :user_default_timezone,
default: nil,
- format: :string,
+ format: :timezone,
allowed: ActiveSupport::TimeZone.all + [nil]
add :users_deletable_by_admins,
diff --git a/lib/core_extensions.rb b/lib/core_extensions.rb
index 4da78f71a15..c44175d0d69 100644
--- a/lib/core_extensions.rb
+++ b/lib/core_extensions.rb
@@ -28,6 +28,7 @@
require 'core_extensions/string'
require 'core_extensions/time_with_zone'
+require 'core_extensions/time_zone'
::String.prepend CoreExtensions::String
::ActiveSupport::TimeWithZone.include CoreExtensions::TimeWithZone
diff --git a/lib/core_extensions/time_zone.rb b/lib/core_extensions/time_zone.rb
new file mode 100644
index 00000000000..673d9363236
--- /dev/null
+++ b/lib/core_extensions/time_zone.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module CoreExtensions
+ module TimeZone # :nodoc:
+ def lookup_timezone(value)
+ all.detect { |tz| tz.name == value || tz.to_s == value || tz.tzinfo.canonical_identifier == value }
+ end
+ end
+end
+
+::ActiveSupport::TimeZone.extend ::CoreExtensions::TimeZone
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index 5adb44ead16..55bda139ad8 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -163,6 +163,14 @@ describe Settings::Definition do
.to eql Date.parse('2222-01-01')
end
+ it 'overriding timezone configuration from ENV will cast the value' do
+ stub_const('ENV', { 'OPENPROJECT_USER__DEFAULT__TIMEZONE' => 'Europe/Berlin' })
+
+ value = all.detect { |d| d.name == 'user_default_timezone' }.value
+ expect(value).to be_a ::ActiveSupport::TimeZone
+ expect(value.name).to eq 'Berlin'
+ end
+
it 'overriding configuration from ENV will set it to non writable' do
stub_const('ENV', { 'OPENPROJECT_EDITION' => 'bim' })
@@ -975,6 +983,30 @@ describe Settings::Definition do
.to be false
end
end
+
+ context 'with a timezone' do
+ let(:instance) do
+ described_class.new 'timezone',
+ format: :timezone,
+ default: nil,
+ allowed: ActiveSupport::TimeZone.all + [nil]
+ end
+
+ it 'accepts a TZ name' do
+ instance.value = 'Europe/Berlin'
+ expect(instance).to be_valid
+ end
+
+ it 'accepts a TZ full identifier' do
+ instance.value = '(GMT+01:00) Berlin'
+ expect(instance).to be_valid
+ end
+
+ it 'rejects a bogus value' do
+ instance.value = 'foobar'
+ expect(instance).not_to be_valid
+ end
+ end
end
describe '#on_change' do