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

move shared function user code into module

ulfertscd615f510803
diff --git a/app/models/anonymous_user.rb b/app/models/anonymous_user.rb
index e3627be53b5..b55057d0e92 100644
--- a/app/models/anonymous_user.rb
+++ b/app/models/anonymous_user.rb
@@ -27,31 +27,25 @@
#++
class AnonymousUser < User
- validate :validate_unique_anonymous_user, on: :create
-
- # There should be only one AnonymousUser in the database
- def validate_unique_anonymous_user
- errors.add :base, "An anonymous user already exists." if AnonymousUser.any?
- end
-
- def available_custom_fields
- []
- end
-
- # Overrides a few properties
- def logged?; false end
-
- def builtin?; true end
-
- def admin; false end
+ include Users::FunctionUser
def name(*_args); I18n.t(:label_user_anonymous) end
- def mail; nil end
+ def self.first
+ anonymous_user = super
- def time_zone; ActiveSupport::TimeZone[Setting.user_default_timezone.presence || "Etc/UTC"] end
+ if anonymous_user.nil?
+ (anonymous_user = new.tap do |u|
+ u.lastname = "Anonymous"
+ u.login = ""
+ u.firstname = ""
+ u.mail = ""
+ u.status = User.statuses[:active]
+ end).save
- def rss_key; nil end
+ raise "Unable to create the anonymous user." if anonymous_user.new_record?
+ end
- def destroy; false end
+ anonymous_user
+ end
end
diff --git a/app/models/deleted_user.rb b/app/models/deleted_user.rb
index b59a6f122cb..ad89f12e71e 100644
--- a/app/models/deleted_user.rb
+++ b/app/models/deleted_user.rb
@@ -1,23 +1,9 @@
class DeletedUser < User
- validate :validate_unique_deleted_user, on: :create
-
- # There should be only one DeletedUser in the database
- def validate_unique_deleted_user
- errors.add :base, "A DeletedUser already exists." if DeletedUser.any?
- end
-
def self.first
super || create(type: to_s, status: statuses[:locked])
end
- # Overrides a few properties
- def available_custom_fields = []
- def logged? = false
- def builtin? = true
- def admin = false
+ include Users::FunctionUser
+
def name(*_args) = I18n.t("user.deleted")
- def mail = nil
- def time_zone; ActiveSupport::TimeZone[Setting.user_default_timezone.presence || "Etc/UTC"] end
- def rss_key = nil
- def destroy = false
end
diff --git a/app/models/system_user.rb b/app/models/system_user.rb
index 4250e8fdb23..a840ef477bb 100644
--- a/app/models/system_user.rb
+++ b/app/models/system_user.rb
@@ -31,31 +31,35 @@
#
class SystemUser < User
- validate :validate_unique_system_user, on: :create
-
- # There should be only one SystemUser in the database
- def validate_unique_system_user
- errors.add :base, "A SystemUser already exists." if SystemUser.any?
- end
-
- # Overrides a few properties
- def logged?; false end
-
- def builtin?; true end
+ include Users::FunctionUser
def name(*_args); "System" end
- def mail; nil end
+ def run_given
+ User.execute_as(self) do
+ yield self
+ end
+ end
- def time_zone; ActiveSupport::TimeZone[Setting.user_default_timezone.presence || "Etc/UTC"] end
+ def self.first
+ system_user = super
- def rss_key; nil end
+ if system_user.nil?
+ system_user = new(
+ firstname: "",
+ lastname: "System",
+ login: "",
+ mail: "",
+ admin: true,
+ status: User.statuses[:active],
+ first_login: false
+ )
- def destroy; false end
+ system_user.save
- def run_given
- User.execute_as(self) do
- yield self
+ raise "Unable to create the system user." unless system_user.persisted?
end
+
+ system_user
end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 4563f1086b9..d1ac0dc4f27 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -543,46 +543,12 @@ class User < Principal
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
# one anonymous user per database.
- def self.anonymous # rubocop:disable Metrics/AbcSize
- RequestStore[:anonymous_user] ||=
- begin
- anonymous_user = AnonymousUser.first
-
- if anonymous_user.nil?
- (anonymous_user = AnonymousUser.new.tap do |u|
- u.lastname = "Anonymous"
- u.login = ""
- u.firstname = ""
- u.mail = ""
- u.status = User.statuses[:active]
- end).save
-
- raise "Unable to create the anonymous user." if anonymous_user.new_record?
- end
- anonymous_user
- end
+ def self.anonymous
+ RequestStore[:anonymous_user] ||= AnonymousUser.first
end
def self.system
- system_user = SystemUser.first
-
- if system_user.nil?
- system_user = SystemUser.new(
- firstname: "",
- lastname: "System",
- login: "",
- mail: "",
- admin: true,
- status: User.statuses[:active],
- first_login: false
- )
-
- system_user.save(validate: false)
-
- raise "Unable to create the automatic migration user." unless system_user.persisted?
- end
-
- system_user
+ SystemUser.first
end
protected
diff --git a/app/models/users/function_user.rb b/app/models/users/function_user.rb
new file mode 100644
index 00000000000..fe8309243f5
--- /dev/null
+++ b/app/models/users/function_user.rb
@@ -0,0 +1,56 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 Users::FunctionUser
+ extend ActiveSupport::Concern
+
+ included do
+ validate :validate_unique_function_user, on: :create
+
+ # There should be only one such user in the database
+ def validate_unique_function_user
+ errors.add :base, "A #{self.class.name} already exists." if self.class.any?
+ end
+
+ def available_custom_fields = []
+
+ def logged? = false
+
+ def builtin? = true
+
+ def name(*_args); raise NotImplementedError end
+
+ def mail = nil
+
+ def time_zone; ActiveSupport::TimeZone[Setting.user_default_timezone.presence || "Etc/UTC"] end
+
+ def rss_key = nil
+
+ def destroy = false
+ end
+end