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
Put bulk behavior into the SharesController
Klaus Zanders5727f1bec603
diff --git a/app/controllers/shares/bulk_controller.rb b/app/controllers/shares/bulk_controller.rbdeleted file mode 100644index 9dbf7fe6c4e..00000000000--- a/app/controllers/shares/bulk_controller.rb+++ /dev/null@@ -1,134 +0,0 @@-# frozen_string_literal: true--# -- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2010-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 Shares::BulkController < ApplicationController- include OpTurbo::ComponentStream- include MemberHelper-- before_action :find_work_package- before_action :find_selected_shares- before_action :find_role_ids_from_params, only: :update- before_action :find_project- before_action :authorize-- def update- @selected_shares.each do |share|- WorkPackageMembers::CreateOrUpdateService- .new(user: current_user)- .call(entity: @work_package,- user_id: share.principal.id,- role_ids: @role_ids).result- end-- respond_with_update_permission_buttons- end-- def destroy- @selected_shares.each do |share|- WorkPackageMembers::DeleteService- .new(user: current_user, model: share)- .call- end-- if current_visible_member_count.zero?- respond_with_replace_modal- else- respond_with_remove_shares- end- end-- private-- def respond_with_update_permission_buttons- @selected_shares.each do |share|- replace_via_turbo_stream(- component: Shares::PermissionButtonComponent.new(share:,- data: { "test-selector": "op-share-wp-update-role" })- )- end-- respond_with_turbo_streams- end-- def respond_with_replace_modal- replace_via_turbo_stream(- component: Shares::ModalBodyComponent.new(work_package: @work_package, shares: find_shares)- )-- respond_with_turbo_streams- end-- def respond_with_remove_shares- @selected_shares.each do |share|- remove_via_turbo_stream(- component: Shares::ShareRowComponent.new(share:)- )- end-- update_via_turbo_stream(- component: Shares::CounterComponent.new(work_package: @work_package, count: current_visible_member_count)- )-- respond_with_turbo_streams- end-- def find_work_package- @work_package = WorkPackage.find(params[:work_package_id])- end-- def find_project- @project = @work_package.project- end-- def find_shares- @shares = Member.includes(:principal, :member_roles)- .references(:member_roles)- .of_work_package(@work_package)- .merge(MemberRole.only_non_inherited)- end-- def find_selected_shares- @selected_shares = Member.includes(:principal)- .of_work_package(@work_package)- .where(id: params[:share_ids])- end-- def find_role_ids_from_params- @role_ids = find_role_ids(params[:role_ids])- end-- def current_visible_member_count- @current_visible_member_count ||= Member- .joins(:member_roles)- .of_work_package(@work_package)- .merge(MemberRole.only_non_inherited)- .size- end-enddiff --git a/app/controllers/shares_controller.rb b/app/controllers/shares_controller.rbindex 6464a16798f..9b50ea5b77b 100644--- a/app/controllers/shares_controller.rb+++ b/app/controllers/shares_controller.rb@@ -32,6 +32,7 @@ class SharesController < ApplicationControllerbefore_action :load_entitybefore_action :load_shares, only: %i[index]+ before_action :load_selected_shares, only: %i[bulk_update bulk_destroy]before_action :load_share, only: %i[destroy update resend_invite]before_action :authorizebefore_action :enterprise_check, only: %i[index]@@ -114,6 +115,32 @@ class SharesController < ApplicationControllerrespond_with_update_user_detailsend+ def bulk_update+ @selected_shares.each do |share|+ WorkPackageMembers::CreateOrUpdateService+ .new(user: current_user)+ .call(entity: @entity,+ user_id: share.principal.id,+ role_ids: params[:role_ids]).result+ end++ respond_with_bulk_updated_permission_buttons+ end++ def bulk_destroy+ @selected_shares.each do |share|+ WorkPackageMembers::DeleteService+ .new(user: current_user, model: share)+ .call+ end++ if current_visible_member_count.zero?+ respond_with_replace_modal+ else+ respond_with_bulk_removed_shares+ end+ end+privatedef enterprise_check@@ -184,8 +211,33 @@ class SharesController < ApplicationControllerrespond_with_turbo_streamsend+ def respond_with_bulk_updated_permission_buttons+ @selected_shares.each do |share|+ replace_via_turbo_stream(+ component: Shares::PermissionButtonComponent.new(share:,+ available_roles:,+ data: { "test-selector": "op-share-wp-update-role" })+ )+ end++ respond_with_turbo_streams+ end++ def respond_with_bulk_removed_shares+ @selected_shares.each do |share|+ remove_via_turbo_stream(+ component: Shares::ShareRowComponent.new(share:, available_roles:)+ )+ end++ update_via_turbo_stream(+ component: Shares::CounterComponent.new(entity: @entity, count: current_visible_member_count)+ )++ respond_with_turbo_streams+ end+def load_entity- puts "*" * 100, "Loading Entity", params.to_unsafe_h, "*" * 100@entity = if params["work_package_id"]WorkPackage.visible.find(params["work_package_id"])# TODO: Add support for other entities@@ -196,7 +248,6 @@ class SharesController < ApplicationControllerERRORend- puts "**** Entity: #{@entity} ****"if @entity.respond_to?(:project)@project = @entity.projectenddiff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex 9dd015f9283..8b4ab0de971 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -331,8 +331,7 @@ Rails.application.reloader.to_prepare domap.permission :share_work_packages,{members: %i[destroy_by_principal],- shares: %i[index create destroy update resend_invite],- "shares/bulk": %i[update destroy]+ shares: %i[index create destroy update resend_invite bulk_update bulk_destroy]},permissible_on: :project,dependencies: %i[edit_work_packages view_shared_work_packages],diff --git a/config/routes.rb b/config/routes.rbindex c8329727ee6..10d63e1decb 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -85,9 +85,9 @@ Rails.application.routes.draw doendcollection do- patch :bulk, to: "shares/bulk#update"- put :bulk, to: "shares/bulk#update"- delete :bulk, to: "shares/bulk#destroy"+ patch :bulk, to: "shares#bulk_update"+ put :bulk, to: "shares#bulk_update"+ delete :bulk, to: "shares#bulk_destroy"endendend