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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
minimal support for user sql representer
ulferts74cb479c7494
diff --git a/app/models/user.rb b/app/models/user.rbindex 2b07d72622d..8b106f67b83 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -105,10 +105,10 @@ class User < Principalattr_accessor :password, :password_confirmation, :last_before_login_onvalidates :login,- :firstname,- :lastname,- :mail,- presence: { unless: Proc.new { |user| user.builtin? } }+ :firstname,+ :lastname,+ :mail,+ presence: { unless: Proc.new { |user| user.builtin? } }validates :login, uniqueness: { if: Proc.new { |user| !user.login.blank? }, case_sensitive: false }validates :mail, uniqueness: { allow_blank: true, case_sensitive: false }diff --git a/lib/api/decorators/sql/hal.rb b/lib/api/decorators/sql/hal.rbindex 0836121588b..a049a7a85be 100644--- a/lib/api/decorators/sql/hal.rb+++ b/lib/api/decorators/sql/hal.rb@@ -57,7 +57,20 @@ module APIoptions[:column]end- "'#{name}', #{representation}"+ if options[:render_if]+ <<-SQL.squish+ '#{name}',+ CASE WHEN #{options[:render_if].call(walker_results)} THEN+ #{representation}+ ELSE+ NULL+ END+ SQL+ else+ <<-SQL.squish+ '#{name}', #{representation}+ SQL+ endend.join(', ')enddiff --git a/lib/api/v3/principals/principals_api.rb b/lib/api/v3/principals/principals_api.rbindex c2b7840f620..a37b40df1c6 100644--- a/lib/api/v3/principals/principals_api.rb+++ b/lib/api/v3/principals/principals_api.rb@@ -34,7 +34,7 @@ module APIget &::API::V3::Utilities::Endpoints::Index.new(model: Principal,scope: -> { Principal.visible(current_user).includes(:preference) },- render_representer: ::API::V3::Users::PaginatedUserCollectionRepresenter)+ render_representer: ::API::V3::Users::UserCollectionRepresenter).mountendenddiff --git a/lib/api/v3/projects/available_assignees_api.rb b/lib/api/v3/projects/available_assignees_api.rbindex 38ab596a9ee..ceb76291eda 100644--- a/lib/api/v3/projects/available_assignees_api.rb+++ b/lib/api/v3/projects/available_assignees_api.rb@@ -41,7 +41,7 @@ module APIscope: -> {Principal.possible_assignee(@project).includes(:preference)},- render_representer: Users::UserCollectionRepresenter)+ render_representer: Users::UnpaginatedUserCollectionRepresenter).mountendenddiff --git a/lib/api/v3/projects/available_responsibles_api.rb b/lib/api/v3/projects/available_responsibles_api.rbindex 5b511756ab1..a5e789501d4 100644--- a/lib/api/v3/projects/available_responsibles_api.rb+++ b/lib/api/v3/projects/available_responsibles_api.rb@@ -41,7 +41,7 @@ module APIscope: -> {Principal.possible_assignee(@project).includes(:preference)},- render_representer: Users::UserCollectionRepresenter)+ render_representer: Users::UnpaginatedUserCollectionRepresenter).mountendenddiff --git a/lib/api/v3/users/paginated_user_collection_representer.rb b/lib/api/v3/users/unpaginated_user_collection_representer.rbsimilarity index 75%rename from lib/api/v3/users/paginated_user_collection_representer.rbrename to lib/api/v3/users/unpaginated_user_collection_representer.rbindex 09c966241a7..3ff387bff83 100644--- a/lib/api/v3/users/paginated_user_collection_representer.rb+++ b/lib/api/v3/users/unpaginated_user_collection_representer.rb@@ -28,16 +28,10 @@# See COPYRIGHT and LICENSE files for more details.#++-# TODO: this is to be removed or rather the UserCollectionRepresenter is-# to be turned into an OffsetPaginatedCollection representer.-# It is not possible to do that right now as we do not have a-# solution for an accessible autocompleter drop down widget. We therefore-# have to fetch all users when we want to present them inside of a drop down.-module APImodule V3module Users- class PaginatedUserCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection+ class UnpaginatedUserCollectionRepresenter < ::API::Decorators::UnpaginatedCollectioninclude API::V3::Principals::NotBuiltinElementsendenddiff --git a/lib/api/v3/users/user_collection_representer.rb b/lib/api/v3/users/user_collection_representer.rbindex d6e364dbf4a..bd9577b910b 100644--- a/lib/api/v3/users/user_collection_representer.rb+++ b/lib/api/v3/users/user_collection_representer.rb@@ -31,7 +31,7 @@module APImodule V3module Users- class UserCollectionRepresenter < ::API::Decorators::UnpaginatedCollection+ class UserCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollectioninclude API::V3::Principals::NotBuiltinElementsendenddiff --git a/lib/api/v3/users/user_sql_collection_representer.rb b/lib/api/v3/users/user_sql_collection_representer.rbnew file mode 100644index 00000000000..502b17a65e9--- /dev/null+++ b/lib/api/v3/users/user_sql_collection_representer.rb@@ -0,0 +1,37 @@+# OpenProject is an open source project management software.+# Copyright (C) 2010-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 API+ module V3+ module Users+ class UserSqlCollectionRepresenter < API::Decorators::SqlCollectionRepresenter+ self.embed_map = {+ elements: UserSqlRepresenter+ }.with_indifferent_access+ end+ end+ end+enddiff --git a/lib/api/v3/users/user_sql_representer.rb b/lib/api/v3/users/user_sql_representer.rbnew file mode 100644index 00000000000..7bf4277ba15--- /dev/null+++ b/lib/api/v3/users/user_sql_representer.rb@@ -0,0 +1,83 @@+# OpenProject is an open source project management software.+# Copyright (C) 2010-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 API+ module V3+ module Users+ class UserSqlRepresenter+ include API::Decorators::Sql::Hal++ class << self+ def user_name_projection(*)+ case Setting.user_format+ when :firstname_lastname+ 'concat(firstname, \' \', lastname)'+ when :firstname+ 'firstname'+ when :lastname_firstname+ 'concat(lastname, \' \', firstname)'+ when :lastname_coma_firstname+ 'concat(lastname, \', \', firstname)'+ when :lastname_n_firstname+ 'concat_ws(lastname, \'\', firstname)'+ when :username+ 'login'+ else+ raise ArgumentError, "Invalid user format"+ end+ end++ def render_if_manage_user_or_self(*)+ if User.current.allowed_to_globally?(:manage_user)+ 'TRUE'+ else+ "id = #{User.current.id}"+ end+ end+ end++ link :self,+ path: { api: :user, params: %w(id) },+ column: -> { :id },+ title: method(:user_name_projection)++ property :_type,+ representation: ->(*) { "'User'" }++ property :id++ property :name,+ representation: method(:user_name_projection)++ property :firstname,+ render_if: method(:render_if_manage_user_or_self)++ property :lastname,+ render_if: method(:render_if_manage_user_or_self)+ end+ end+ end+enddiff --git a/lib/api/v3/users/users_api.rb b/lib/api/v3/users/users_api.rbindex 401f06a45ce..3beeb4099c3 100644--- a/lib/api/v3/users/users_api.rb+++ b/lib/api/v3/users/users_api.rb@@ -26,9 +26,6 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'api/v3/users/user_representer'-require 'api/v3/users/paginated_user_collection_representer'-module APImodule V3module Users@@ -56,10 +53,10 @@ module APIauthorize_by_with_raise(current_user.allowed_to_globally?(:manage_user))end- get &::API::V3::Utilities::Endpoints::Index.new(model: User,- scope: -> { User.user.includes(:preference) },- render_representer: PaginatedUserCollectionRepresenter)- .mount+ get &::API::V3::Utilities::Endpoints::SqlFallbackedIndex+ .new(model: User,+ scope: -> { User.user.includes(:preference) })+ .mountendmount ::API::V3::Users::Schemas::UserSchemaAPIdiff --git a/lib/api/v3/utilities/sql_representer_walker.rb b/lib/api/v3/utilities/sql_representer_walker.rbindex 0735024d3e2..b30f4692d67 100644--- a/lib/api/v3/utilities/sql_representer_walker.rb+++ b/lib/api/v3/utilities/sql_representer_walker.rb@@ -41,7 +41,7 @@ module APIself.scope = scopeself.current_user = current_userself.self_path = self_path- # Hard wiring the properties ot embed is a work around until signaling the properties to embed is implemented+ # Hard wiring the properties to embed is a work around until signaling the properties to embed is implementedself.url_query = url_query.merge(embed: { 'elements' => {} })enddiff --git a/lib/api/v3/work_packages/watchers_api.rb b/lib/api/v3/work_packages/watchers_api.rbindex bf565a6516e..2fb4f359b8c 100644--- a/lib/api/v3/work_packages/watchers_api.rb+++ b/lib/api/v3/work_packages/watchers_api.rb@@ -42,7 +42,7 @@ module APIif query.valid?users = query.results.merge(@work_package.addable_watcher_users).includes(:preference)- ::API::V3::Users::PaginatedUserCollectionRepresenter.new(+ ::API::V3::Users::UserCollectionRepresenter.new(users,self_link: api_v3_paths.users,page: to_i_or_nil(params[:offset]),@@ -59,9 +59,9 @@ module APIdef watchers_collectionwatchers = @work_package.watcher_users.merge(Principal.not_locked)self_link = api_v3_paths.work_package_watchers(@work_package.id)- Users::UserCollectionRepresenter.new(watchers,- self_link: self_link,- current_user: current_user)+ Users::UnpaginatedUserCollectionRepresenter.new(watchers,+ self_link: self_link,+ current_user: current_user)endenddiff --git a/spec/lib/api/v3/users/paginated_user_collection_representer_spec.rb b/spec/lib/api/v3/users/unpaginated_user_collection_representer_spec.rbsimilarity index 65%rename from spec/lib/api/v3/users/paginated_user_collection_representer_spec.rbrename to spec/lib/api/v3/users/unpaginated_user_collection_representer_spec.rbindex 74ac169331f..fe364129b55 100644--- a/spec/lib/api/v3/users/paginated_user_collection_representer_spec.rb+++ b/spec/lib/api/v3/users/unpaginated_user_collection_representer_spec.rb@@ -28,45 +28,20 @@require 'spec_helper'-describe ::API::V3::Users::PaginatedUserCollectionRepresenter do- let(:self_base_link) { '/api/v3/users' }- let(:collection_inner_type) { 'User' }- let(:total) { 3 }- let(:page) { 1 }- let(:page_size) { 2 }- let(:actual_count) { 3 }-+describe ::API::V3::Users::UnpaginatedUserCollectionRepresenter dolet(:users) do- users = build_stubbed_list(:user,- actual_count)- allow(users)- .to receive(:per_page)- .with(page_size)- .and_return(users)-- allow(users)- .to receive(:page)- .with(page)- .and_return(users)-- allow(users)- .to receive(:count)- .and_return(total)-- users+ build_stubbed_list(:user,+ 3)end-let(:representer) dodescribed_class.new(users,- self_link: '/api/v3/users',- per_page: page_size,- page: page,+ self_link: '/api/v3/work_package/1/watchers',current_user: users.first)endcontext 'generation' dosubject(:collection) { representer.to_json }- it_behaves_like 'offset-paginated APIv3 collection'+ it_behaves_like 'unpaginated APIv3 collection', 3, 'work_package/1/watchers', 'User'endenddiff --git a/spec/lib/api/v3/users/user_collection_representer_spec.rb b/spec/lib/api/v3/users/user_collection_representer_spec.rbindex 72e9ef1f16b..7db40798ef0 100644--- a/spec/lib/api/v3/users/user_collection_representer_spec.rb+++ b/spec/lib/api/v3/users/user_collection_representer_spec.rb@@ -29,19 +29,44 @@require 'spec_helper'describe ::API::V3::Users::UserCollectionRepresenter do+ let(:self_base_link) { '/api/v3/users' }+ let(:collection_inner_type) { 'User' }+ let(:total) { 3 }+ let(:page) { 1 }+ let(:page_size) { 2 }+ let(:actual_count) { 3 }+let(:users) do- build_stubbed_list(:user,- 3)+ users = build_stubbed_list(:user,+ actual_count)+ allow(users)+ .to receive(:per_page)+ .with(page_size)+ .and_return(users)++ allow(users)+ .to receive(:page)+ .with(page)+ .and_return(users)++ allow(users)+ .to receive(:count)+ .and_return(total)++ usersend+let(:representer) dodescribed_class.new(users,- self_link: '/api/v3/work_package/1/watchers',+ self_link: '/api/v3/users',+ per_page: page_size,+ page: page,current_user: users.first)endcontext 'generation' dosubject(:collection) { representer.to_json }- it_behaves_like 'unpaginated APIv3 collection', 3, 'work_package/1/watchers', 'User'+ it_behaves_like 'offset-paginated APIv3 collection'endenddiff --git a/spec/lib/api/v3/users/user_sql_representer_rendering_spec.rb b/spec/lib/api/v3/users/user_sql_representer_rendering_spec.rbnew file mode 100644index 00000000000..e1e3e5a794b--- /dev/null+++ b/spec/lib/api/v3/users/user_sql_representer_rendering_spec.rb@@ -0,0 +1,189 @@+# OpenProject is an open source project management software.+# Copyright (C) 2010-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.++require 'spec_helper'++describe ::API::V3::Users::UserSqlRepresenter, 'rendering' do+ include ::API::V3::Utilities::PathHelper++ subject(:json) do+ ::API::V3::Utilities::SqlRepresenterWalker+ .new(scope,+ current_user: current_user,+ url_query: { select: select })+ .walk(described_class)+ .to_json+ end++ let(:scope) do+ User+ .where(id: rendered_user.id)+ end++ let(:rendered_user) { current_user }++ let(:select) { { '*' => {} } }++ current_user do+ create(:user)+ end++ context 'when rendering all supported properties' do+ let(:expected) do+ {+ _type: "User",+ id: current_user.id,+ name: current_user.name,+ firstname: current_user.firstname,+ lastname: current_user.lastname,+ _links: {+ self: {+ href: api_v3_paths.user(current_user.id),+ title: current_user.name+ }+ }+ }+ end++ it 'renders as expected' do+ expect(json)+ .to be_json_eql(expected.to_json)+ end+ end++ describe 'name property' do+ shared_examples_for 'name property depending on user format setting' do+ let(:select) { { 'name' => {} } }++ let(:expected) do+ {+ name: current_user.name+ }+ end++ it 'renders as expected' do+ expect(json)+ .to be_json_eql(expected.to_json)+ end+ end++ context 'when user_format is set to firstname', with_settings: { user_format: :firstname } do+ it_behaves_like 'name property depending on user format setting'+ end++ context 'when user_format is set to lastname_firstname', with_settings: { user_format: :lastname_firstname } do+ it_behaves_like 'name property depending on user format setting'+ end++ context 'when user_format is set to lastname_coma_firstname', with_settings: { user_format: :lastname_coma_firstname } do+ it_behaves_like 'name property depending on user format setting'+ end++ context 'when user_format is set to lastname_n_firstname', with_settings: { user_format: :lastname_n_firstname } do+ it_behaves_like 'name property depending on user format setting'+ end++ context 'when user_format is set to username', with_settings: { user_format: :username } do+ it_behaves_like 'name property depending on user format setting'+ end+ end++ describe 'firstname property' do+ let(:select) { { 'firstname' => {} } }++ context 'when the user is the current user' do+ it 'renders the firstname' do+ expect(json)+ .to be_json_eql(+ {+ firstname: rendered_user.firstname+ }.to_json+ )+ end+ end++ context 'when the user is a user not having manage_user permission' do+ let(:rendered_user) { create(:user) }++ it 'hides the firstname' do+ expect(json)+ .to be_json_eql({}.to_json)+ end+ end++ context 'when the user is a user having manage_user permission' do+ let(:current_user) { create(:user, global_permissions: [:manage_user]) }+ let(:rendered_user) { create(:user) }++ it 'renders the firstname' do+ expect(json)+ .to be_json_eql(+ {+ firstname: rendered_user.firstname+ }.to_json+ )+ end+ end+ end++ describe 'lastname property' do+ let(:select) { { 'lastname' => {} } }++ context 'when the user is the current user' do+ it 'renders the lastname' do+ expect(json)+ .to be_json_eql(+ {+ lastname: rendered_user.lastname+ }.to_json+ )+ end+ end++ context 'when the user is a user not having manage_user permission' do+ let(:rendered_user) { create(:user) }++ it 'hides the lastname' do+ expect(json)+ .to be_json_eql({}.to_json)+ end+ end++ context 'when the user is a user having manage_user permission' do+ let(:current_user) { create(:user, global_permissions: [:manage_user]) }+ let(:rendered_user) { create(:user) }++ it 'renders the lastname' do+ expect(json)+ .to be_json_eql(+ {+ lastname: rendered_user.lastname+ }.to_json+ )+ end+ end+ end+enddiff --git a/spec/requests/api/v3/user/user_resource_spec.rb b/spec/requests/api/v3/user/user_resource_spec.rbindex 7bd10661682..c9128db651d 100644--- a/spec/requests/api/v3/user/user_resource_spec.rb+++ b/spec/requests/api/v3/user/user_resource_spec.rb@@ -93,7 +93,7 @@ describe 'API v3 User resource',endend- context 'on filtering for name' do+ context 'when filtering by name' dolet(:get_path) dofilter = [{ 'name' => {'operator' => '~',@@ -116,13 +116,13 @@ describe 'API v3 User resource',endend- context 'on sorting' do+ context 'when sorting' dolet(:users_by_name_order) doUser.human.ordered_by_name(desc: true)endlet(:get_path) do- sort = [['name', 'desc']]+ sort = [%w[name desc]]"#{api_v3_paths.users}?#{{ sortBy: sort.to_json }.to_query}"end@@ -140,7 +140,7 @@ describe 'API v3 User resource',endend- context 'on an invalid filter' do+ context 'with an invalid filter' dolet(:get_path) dofilter = [{ 'name' => {'operator' => 'a',@@ -151,30 +151,57 @@ describe 'API v3 User resource',endit 'returns an error' do- expect(subject.status).to eql(400)+ expect(subject.status).to be(400)+ end+ end++ context 'when signaling desired properties' do+ let(:get_path) do+ api_v3_paths.path_for :users,+ sort_by: [%w[name desc]],+ page_size: 1,+ select: 'total,elements/name'+ end++ let(:expected) do+ {+ total: 2,+ _embedded: {+ elements: [+ {+ name: current_user.name+ }+ ]+ }+ }+ end++ it 'returns an error' do+ expect(subject.body)+ .to be_json_eql(expected.to_json)endendend- context 'admin' do+ context 'for an admin' dolet(:current_user) { admin }it_behaves_like 'flow with permitted user'end- context 'user with global manage_user permission' do+ context 'for a user with global manage_user permission' dolet(:current_user) { user_with_global_manage_user }it_behaves_like 'flow with permitted user'end- context 'locked admin' do+ context 'for a locked admin' dolet(:current_user) { locked_admin }it_behaves_like 'unauthorized access'end- context 'other user' do+ context 'for another user' doit_behaves_like 'unauthorized access'endend