Skip to content

Commit

Permalink
move shared function user code into module
Browse files Browse the repository at this point in the history
  • Loading branch information
ulferts committed Sep 24, 2024
1 parent ff45d0d commit cd615f5
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 92 deletions.
36 changes: 15 additions & 21 deletions app/models/anonymous_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 2 additions & 16 deletions app/models/deleted_user.rb
Original file line number Diff line number Diff line change
@@ -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
40 changes: 22 additions & 18 deletions app/models/system_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 3 additions & 37 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -543,46 +543,12 @@ def missing_authentication_method?

# 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
Expand Down
56 changes: 56 additions & 0 deletions app/models/users/function_user.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cd615f5

Please sign in to comment.