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

I'm sick of looking up magic numbers

Markus Kahlcddd45c8f5ee
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index 9c314fb7824..880b1515ddd 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -502,10 +502,9 @@ class AccountController < ApplicationController
def register_user_according_to_setting(user, opts = {}, &block)
return register_automatically(user, opts, &block) if user.invited?
- case Setting.self_registration
- when '1'
+ if Setting::SelfRegistration.by_email?
register_by_email_activation(user, opts, &block)
- when '3'
+ elsif Setting::SelfRegistration.automatic?
register_automatically(user, opts, &block)
else
register_manually_by_administrator(user, opts, &block)
diff --git a/app/models/setting/self_registration.rb b/app/models/setting/self_registration.rb
new file mode 100644
index 00000000000..11b3798aad9
--- /dev/null
+++ b/app/models/setting/self_registration.rb
@@ -0,0 +1,85 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+class Setting
+ ##
+ # Shorthand to common setting aliases to avoid checking values
+ module SelfRegistration
+ VALUES = {
+ disabled: 0,
+ activation_by_email: 1,
+ manual_activation: 2,
+ automatic_activation: 3
+ }.freeze
+
+ def self.values
+ VALUES
+ end
+
+ def self.value(key:)
+ VALUES[key]
+ end
+
+ def self.key(value:)
+ VALUES.find { |k, v| v == value || v.to_s == value.to_s }&.first
+ end
+
+ def self.disabled
+ value key: :disabled
+ end
+
+ def self.disabled?
+ key(value: Setting.self_registration) == :disabled
+ end
+
+ def self.by_email
+ value key: :activation_by_email
+ end
+
+ def self.by_email?
+ key(value: Setting.self_registration) == :activation_by_email
+ end
+
+ def self.manual
+ value key: :manual_activation
+ end
+
+ def self.manual?
+ key(value: Setting.self_registration) == :manual_activation
+ end
+
+ def self.automatic
+ value key: :automatic_activation
+ end
+
+ def self.automatic?
+ key(value: Setting.self_registration) == :automatic_activation
+ end
+ end
+end
diff --git a/app/views/authentication/authentication_settings.html.erb b/app/views/authentication/authentication_settings.html.erb
index a257feef42c..71eefa1ec46 100644
--- a/app/views/authentication/authentication_settings.html.erb
+++ b/app/views/authentication/authentication_settings.html.erb
@@ -36,10 +36,10 @@ See docs/COPYRIGHT.rdoc for more details.
<legend class="form--fieldset-legend"><%= I18n.t(:general, scope: [:settings]) %></legend>
<div class="form--field"><%= setting_check_box :login_required %></div>
- <div class="form--field"><%= setting_select :self_registration, [[t(:label_disabled), "0"],
- [t(:label_registration_activation_by_email), "1"],
- [t(:label_registration_manual_activation), "2"],
- [t(:label_registration_automatic_activation), "3"]],
+ <div class="form--field"><%= setting_select :self_registration, [[t(:label_disabled), Setting::SelfRegisration.disabled.to_s],
+ [t(:label_registration_activation_by_email), Setting::SelfRegisration.by_email.to_s],
+ [t(:label_registration_manual_activation), Setting::SelfRegisration.manual.to_s],
+ [t(:label_registration_automatic_activation), Setting::SelfRegisration.automatic.to_s]],
container_class: '-middle' %>
</div>
diff --git a/spec/features/auth/omniauth_spec.rb b/spec/features/auth/omniauth_spec.rb
index 354abe6e69e..1c22ce2ced2 100644
--- a/spec/features/auth/omniauth_spec.rb
+++ b/spec/features/auth/omniauth_spec.rb
@@ -198,7 +198,7 @@ describe 'Omniauth authentication', type: :feature do
context 'registration by email',
with_settings: {
self_registration?: true,
- self_registration: '1'
+ self_registration: Setting::SelfRegistration.by_email.to_s
} do
shared_examples 'registration with registration by email' do