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

[37278] Treat system_user as user type as well (#9287)

Oliver Günther93faf5b5dd10
diff --git a/lib/api/v3/principals/principal_representer_factory.rb b/lib/api/v3/principals/principal_representer_factory.rb
index 26a99738ccf..b1471efc45a 100644
--- a/lib/api/v3/principals/principal_representer_factory.rb
+++ b/lib/api/v3/principals/principal_representer_factory.rb
@@ -55,7 +55,7 @@ module API
def self.create_link_lambda(name, getter: "#{name}_id")
->(*) {
- v3_path = API::V3::Principals::PrincipalRepresenterFactory.v3_path(represented.send(name))
+ v3_path = API::V3::Principals::PrincipalType.for(represented.send(name))
instance_exec(&self.class.associated_resource_default_link(name,
v3_path: v3_path,
@@ -88,23 +88,6 @@ module API
.create(represented.send(name), current_user: current_user)
}
end
-
- def self.v3_path(instance)
- case instance
- when User
- :user
- when Group
- :group
- when PlaceholderUser
- :placeholder_user
- when NilClass
- # Fall back to user for unknown principal
- # since we do not have a principal route.
- :user
- else
- raise "undefined subclass for #{instance}"
- end
- end
end
end
end
diff --git a/lib/api/v3/principals/principal_type.rb b/lib/api/v3/principals/principal_type.rb
new file mode 100644
index 00000000000..02c70b9b593
--- /dev/null
+++ b/lib/api/v3/principals/principal_type.rb
@@ -0,0 +1,57 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Principals
+ class PrincipalType
+ ##
+ # Return the appropriate API level type
+ # that depend on the AR type of the principal passed in.
+ def self.for(principal)
+ case principal
+ when User
+ :user
+ when Group
+ :group
+ when PlaceholderUser
+ :placeholder_user
+ when NilClass
+ # Fall back to user for unknown principal
+ # since we do not have a principal route.
+ :user
+ else
+ raise "undefined subclass for #{principal}"
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb b/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb
index 94233604d12..536edcc8c23 100644
--- a/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb
+++ b/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb
@@ -93,7 +93,7 @@ module API
end
def derive_principal_path_method(custom_value)
- API::V3::Principals::PrincipalRepresenterFactory.v3_path(custom_value.typed_value)
+ API::V3::Principals::PrincipalType.for(custom_value.typed_value)
end
def link_value_title(custom_value)
diff --git a/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb b/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb
index cc2bf7a314e..b458324a831 100644
--- a/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb
+++ b/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb
@@ -89,7 +89,7 @@ AvatarHelper.class_eval do
data: {
'principal-id': user.id,
'principal-name': user.name,
- 'principal-type': user.type.underscore,
+ 'principal-type': API::V3::Principals::PrincipalType.for(user),
'size': tag_options[:size],
'hide-name': tag_options[:hide_name]
}
diff --git a/modules/avatars/spec/helpers/avatar_helper_spec.rb b/modules/avatars/spec/helpers/avatar_helper_spec.rb
index 3f05e0a3818..e9e7ece6095 100644
--- a/modules/avatars/spec/helpers/avatar_helper_spec.rb
+++ b/modules/avatars/spec/helpers/avatar_helper_spec.rb
@@ -199,4 +199,12 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
expect(helper.avatar_url(user)).to eq('')
end
end
+
+ context 'with system user' do
+ let(:user) { User.system }
+
+ it 'renders the avatar as user type (Regression #37278)' do
+ expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
+ end
+ end
end
diff --git a/spec/lib/api/v3/principals/principal_type_spec.rb b/spec/lib/api/v3/principals/principal_type_spec.rb
new file mode 100644
index 00000000000..d539f0635f9
--- /dev/null
+++ b/spec/lib/api/v3/principals/principal_type_spec.rb
@@ -0,0 +1,90 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe ::API::V3::Principals::PrincipalType do
+ let(:principal) { nil }
+ subject { described_class.for(instance) }
+
+ shared_examples 'returns api type' do |type|
+ it do
+ expect(subject).to eq type
+ end
+ end
+
+ describe 'with a user' do
+ let(:instance) { FactoryBot.build_stubbed :user }
+
+ it_behaves_like 'returns api type', :user
+ end
+
+ describe 'with a user' do
+ let(:instance) { FactoryBot.build_stubbed :user }
+
+ it_behaves_like 'returns api type', :user
+ end
+
+ describe 'with a system user' do
+ let(:instance) { User.system }
+
+ it_behaves_like 'returns api type', :user
+ end
+
+ describe 'with a system user' do
+ let(:instance) { FactoryBot.build_stubbed :deleted_user }
+
+ it_behaves_like 'returns api type', :user
+ end
+
+ describe 'with anonymous' do
+ let(:instance) { User.anonymous }
+
+ it_behaves_like 'returns api type', :user
+ end
+
+ describe 'with a group' do
+ let(:instance) { FactoryBot.build_stubbed :group }
+
+ it_behaves_like 'returns api type', :group
+ end
+
+ describe 'with a placeholder' do
+ let(:instance) { FactoryBot.build_stubbed :placeholder_user }
+
+ it_behaves_like 'returns api type', :placeholder_user
+ end
+
+ describe 'with an invalid type' do
+ let(:instance) { 'whatever' }
+
+ it 'raises an exception' do
+ expect { subject }.to raise_error "undefined subclass for whatever"
+ end
+ end
+end