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

Extend MemberQuery by filters for the entity and use that in the shares_controller

Henriette Darge3a955cec1202
diff --git a/app/components/work_packages/share/modal_body_component.html.erb b/app/components/work_packages/share/modal_body_component.html.erb
index 2ac033b81f5..1a08cad2945 100644
--- a/app/components/work_packages/share/modal_body_component.html.erb
+++ b/app/components/work_packages/share/modal_body_component.html.erb
@@ -5,7 +5,7 @@
render(WorkPackages::Share::InviteUserFormComponent.new(work_package: @work_package))
end
- if shared_principals.none?
+ if @shared_principals.none?
modal_content.with_row(mt: 3) do
render(Primer::Beta::Blankslate.new(border: true)) do |component|
component.with_visual_icon(icon: :people, size: :medium)
@@ -28,7 +28,7 @@
border_box.with_header(color: :subtle, pt: 2, pb: 2, data: { 'test-selector': 'op-share-wp-header' }) do
grid_layout('op-share-wp-modal-body--header', tag: :div, align_items: :center) do |header_grid|
header_grid.with_area(:counter, tag: :div) do
- render(WorkPackages::Share::CounterComponent.new(work_package: @work_package, count: shared_principals.size))
+ render(WorkPackages::Share::CounterComponent.new(work_package: @work_package, count: @shared_principals.size))
end
header_grid.with_area(:actions,
@@ -87,12 +87,8 @@
end
end
- shared_principals.each do |principal|
- share = principal.work_package_shares
- .where(entity: @work_package)
- .first
-
- render(WorkPackages::Share::ShareRowComponent.new(share:, container: border_box))
+ @shared_principals.each do |principal|
+ render(WorkPackages::Share::ShareRowComponent.new(share: principal, container: border_box))
end
end
end
diff --git a/app/components/work_packages/share/modal_body_component.rb b/app/components/work_packages/share/modal_body_component.rb
index 12d4fd911bb..4a810b23b16 100644
--- a/app/components/work_packages/share/modal_body_component.rb
+++ b/app/components/work_packages/share/modal_body_component.rb
@@ -34,10 +34,11 @@ module WorkPackages
include OpPrimer::ComponentHelpers
include WorkPackages::Share::Concerns::Authorization
- def initialize(work_package:)
+ def initialize(work_package:, shares:)
super
@work_package = work_package
+ @shared_principals = shares
end
def self.wrapper_key
@@ -72,15 +73,6 @@ module WorkPackages
list_container.instance_variable_set(:@list_arguments, new_list_arguments)
end
-
- def shared_principals
- @shared_principals ||= Principal
- .having_entity_membership(@work_package)
- .includes(work_package_shares: :roles)
- .where(work_package_shares: { entity: @work_package })
- .merge(MemberRole.only_non_inherited)
- .ordered_by_name
- end
end
end
end
diff --git a/app/controllers/work_packages/shares_controller.rb b/app/controllers/work_packages/shares_controller.rb
index e7b3770aca0..61c45f46878 100644
--- a/app/controllers/work_packages/shares_controller.rb
+++ b/app/controllers/work_packages/shares_controller.rb
@@ -36,7 +36,15 @@ class WorkPackages::SharesController < ApplicationController
before_action :authorize
def index
- render WorkPackages::Share::ModalBodyComponent.new(work_package: @work_package), layout: nil
+ query = load_query
+
+ unless query.valid?
+ flash.now[:error] = query.errors.full_messages
+ end
+
+ @shares = load_shares query
+
+ render WorkPackages::Share::ModalBodyComponent.new(work_package: @work_package, shares: @shares), layout: nil
end
def create
@@ -163,4 +171,21 @@ class WorkPackages::SharesController < ApplicationController
.merge(MemberRole.only_non_inherited)
.size
end
+
+ def load_query
+ @query = ParamsToQueryService.new(Member, current_user).call(params)
+
+ # Set default filter on the entity
+ @query.where('entity_id', '=', @work_package.id)
+ @query.where('entity_type', '=', WorkPackage.name)
+
+ @query.order(name: :asc) unless params[:sortBy]
+
+ @query
+ end
+
+ def load_shares(query)
+ query
+ .results
+ end
end
diff --git a/app/models/queries/members.rb b/app/models/queries/members.rb
index 903688871dd..15035ba9106 100644
--- a/app/models/queries/members.rb
+++ b/app/models/queries/members.rb
@@ -38,6 +38,8 @@ module Queries::Members
filter Filters::PrincipalFilter
filter Filters::CreatedAtFilter
filter Filters::UpdatedAtFilter
+ filter Filters::EntityIdFilter
+ filter Filters::EntityTypeFilter
order Orders::DefaultOrder
order Orders::NameOrder
diff --git a/app/models/queries/members/filters/entity_id_filter.rb b/app/models/queries/members/filters/entity_id_filter.rb
new file mode 100644
index 00000000000..4f81d1784c1
--- /dev/null
+++ b/app/models/queries/members/filters/entity_id_filter.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+class Queries::Members::Filters::EntityIdFilter < Queries::Members::Filters::MemberFilter
+ def type
+ :integer
+ end
+
+ def self.key
+ :entity_id
+ end
+end
diff --git a/app/models/queries/members/filters/entity_type_filter.rb b/app/models/queries/members/filters/entity_type_filter.rb
new file mode 100644
index 00000000000..8c3117104c5
--- /dev/null
+++ b/app/models/queries/members/filters/entity_type_filter.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+class Queries::Members::Filters::EntityTypeFilter < Queries::Members::Filters::MemberFilter
+ def type
+ :string
+ end
+
+ def self.key
+ :entity_type
+ end
+end
diff --git a/app/models/queries/members/member_query.rb b/app/models/queries/members/member_query.rb
index 63fca028e51..b7bdf9050ef 100644
--- a/app/models/queries/members/member_query.rb
+++ b/app/models/queries/members/member_query.rb
@@ -37,8 +37,6 @@ class Queries::Members::MemberQuery < Queries::BaseQuery
end
def default_scope
- # TODO: For now we exclude entity specific memberships in the API until we have updated the
- # frontend and representers to show those properly
- Member.visible(User.current).where(entity: nil)
+ Member.visible(User.current)
end
end