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

[#55362] Shared work packages in template projects cause several flaws in new projects

Dombi Attila0c7b0405cedc
diff --git a/app/services/projects/copy/members_dependent_service.rb b/app/services/projects/copy/members_dependent_service.rb
index 564c5fc5837..3b7da7aa76f 100644
--- a/app/services/projects/copy/members_dependent_service.rb
+++ b/app/services/projects/copy/members_dependent_service.rb
@@ -32,8 +32,12 @@ module Projects::Copy
I18n.t(:"projects.copy.members")
end
+ def source_memberships
+ source.memberships.of_any_project
+ end
+
def source_count
- source.members.count
+ source_memberships.count
end
protected
@@ -41,7 +45,7 @@ module Projects::Copy
def copy_dependency(*)
# Copy users and placeholder users first,
# then groups to handle members with inherited and given roles
- source.memberships.sort_by { |m| m.principal.is_a?(Group) ? 1 : 0 }.each do |member|
+ source_memberships.sort_by { |m| m.principal.is_a?(Group) ? 1 : 0 }.each do |member|
create_membership(member)
end
end
diff --git a/app/services/projects/copy/share_copier.rb b/app/services/projects/copy/share_copier.rb
new file mode 100644
index 00000000000..7188a5c5a91
--- /dev/null
+++ b/app/services/projects/copy/share_copier.rb
@@ -0,0 +1,57 @@
+# 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 Projects::Copy
+ # Can be included in case the dependent service needs to be listed in the copy_dependency section for:
+ # * Being an option in the UI
+ # * Calculating the count
+ # but does not really need to do anything beyond that.
+ #
+ # This is currently employed to turn the work package shares services into NoCreate services as
+ # work package shares handling has been moved into the create service of WorkPackage
+ module ShareCopier
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def share_dependent_service(service_const = nil)
+ @share_dependent_service = service_const if service_const
+
+ @share_dependent_service
+ end
+
+ def copy_dependencies
+ super + [share_dependent_service]
+ end
+ end
+
+ protected
+
+ def copy_shares?
+ (params.dig(:params, :only) || [])
+ .any? { |k| k.to_s == self.class.share_dependent_service.identifier }
+ end
+ end
+end
diff --git a/app/services/projects/copy/work_package_shares_dependent_service.rb b/app/services/projects/copy/work_package_shares_dependent_service.rb
new file mode 100644
index 00000000000..58751155725
--- /dev/null
+++ b/app/services/projects/copy/work_package_shares_dependent_service.rb
@@ -0,0 +1,41 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 Projects::Copy
+ class WorkPackageSharesDependentService < Dependency
+ include ::Projects::Copy::NoCopier
+
+ def self.human_name
+ I18n.t(:"projects.copy.work_package_shares")
+ end
+
+ def source_count
+ source.members.of_any_work_package.count
+ end
+ end
+end
diff --git a/app/services/projects/copy/work_packages_dependent_service.rb b/app/services/projects/copy/work_packages_dependent_service.rb
index 15a3d6fc97a..f8380584f82 100644
--- a/app/services/projects/copy/work_packages_dependent_service.rb
+++ b/app/services/projects/copy/work_packages_dependent_service.rb
@@ -29,8 +29,10 @@
module Projects::Copy
class WorkPackagesDependentService < Dependency
include AttachmentCopier
+ include ShareCopier
attachment_dependent_service ::Projects::Copy::WorkPackageAttachmentsDependentService
+ share_dependent_service ::Projects::Copy::WorkPackageSharesDependentService
def self.human_name
I18n.t(:label_work_package_plural)
@@ -85,7 +87,11 @@ module Projects::Copy
.new(user:,
work_package: source_work_package,
contract_class: WorkPackages::CopyProjectContract)
- .call(copy_attachments: copy_attachments?, **overrides)
+ .call(
+ copy_attachments: copy_attachments?,
+ copy_share_members: copy_shares?,
+ **overrides
+ )
if service_call.success?
service_call.result
@@ -140,7 +146,7 @@ module Projects::Copy
budget_id: nil,
# We persist the setting in the job which will trigger a delayed job for potentially sending the journal notifications.
- send_notifications: params[:send_notifications]
+ send_notifications: params.dig(:params, :send_notifications)
}
end
diff --git a/app/services/work_packages/copy_service.rb b/app/services/work_packages/copy_service.rb
index 1256342f332..c96018523bf 100644
--- a/app/services/work_packages/copy_service.rb
+++ b/app/services/work_packages/copy_service.rb
@@ -41,15 +41,15 @@ class WorkPackages::CopyService
self.contract_class = contract_class
end
- def call(send_notifications: nil, copy_attachments: true, **attributes)
+ def call(send_notifications: nil, copy_attachments: true, copy_share_members: true, **attributes)
in_context(work_package, send_notifications:) do
- copy(attributes, copy_attachments, send_notifications)
+ copy(attributes, copy_attachments, copy_share_members, send_notifications)
end
end
protected
- def copy(attribute_override, copy_attachments, send_notifications)
+ def copy(attribute_override, copy_attachments, copy_share_members, send_notifications)
copied = create(work_package,
attribute_override,
send_notifications)
@@ -57,6 +57,7 @@ class WorkPackages::CopyService
remove_author_watcher(copy_call.result)
copy_watchers(copy_call.result)
copy_work_package_attachments(copy_call.result) if copy_attachments
+ copy_share_members(copy_call.result, send_notifications) if copy_share_members
end
copied.state.copied_from_work_package_id = work_package&.id
@@ -111,4 +112,29 @@ class WorkPackages::CopyService
references: %i[description]
)
end
+
+ def copy_share_members(copy, send_notifications)
+ work_package.members.each do |member|
+ create_share_membership(member, copy, send_notifications)
+ end
+ end
+
+ private
+
+ def create_share_membership(member, target, send_notifications)
+ role_ids = member.member_roles.map(&:role_id)
+
+ return if role_ids.empty?
+
+ attributes = member
+ .attributes.dup.except("id", "project_id", "entity_id", "created_at", "updated_at")
+ .merge(role_ids:,
+ project: target.project,
+ entity: target,
+ send_notifications:)
+
+ Members::CreateService
+ .new(user: User.current, contract_class: EmptyContract)
+ .call(attributes)
+ end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ca2765992e2..7babd5747aa 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -281,6 +281,7 @@ en:
work_package_attachments: "Work packages: attachments"
work_package_categories: "Work packages: categories"
work_package_file_links: "Work packages: file links"
+ work_package_shares: "Work packages: shares"
delete:
scheduled: "Deletion has been scheduled and is performed in the background. You will be notified of the result."
schedule_failed: "Project cannot be deleted: %{errors}"
diff --git a/spec/models/project_queries/scopes/allowed_to_spec.rb b/spec/models/project_queries/scopes/allowed_to_spec.rb
index 95f51a79c21..2c368a6e5da 100644
--- a/spec/models/project_queries/scopes/allowed_to_spec.rb
+++ b/spec/models/project_queries/scopes/allowed_to_spec.rb
@@ -46,12 +46,12 @@ RSpec.describe ProjectQuery, "#allowed to" do # rubocop:disable RSpec/RSpec/Spec
shared_let(:private_other_query) { create(:project_query, user: other_user) }
shared_let(:private_other_query_with_view) do
create(:project_query, user: other_user, members: [
- create(:member, user:, roles: [view_role])
+ create(:project_query_member, user:, roles: [view_role])
])
end
shared_let(:private_other_query_with_edit) do
create(:project_query, user: other_user, members: [
- create(:member, user:, roles: [edit_role])
+ create(:project_query_member, user:, roles: [edit_role])
])
end
diff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rb
index 513e95ec1d8..0bd3ef4d4ef 100644
--- a/spec/services/projects/copy_service_integration_spec.rb
+++ b/spec/services/projects/copy_service_integration_spec.rb
@@ -95,6 +95,7 @@ RSpec.describe(
categories
work_packages
work_package_attachments
+ work_package_shares
wiki
wiki_page_attachments
forums
@@ -641,6 +642,40 @@ RSpec.describe(
end
end
+ context "with shared work packages" do
+ let(:wp_role) { create(:view_work_package_role) }
+ let!(:source_wp_shared_with_user) do
+ create(:user, member_with_roles: { source_wp => wp_role })
+ end
+
+ let(:only_args) { %w[work_packages work_package_shares] }
+
+ it "copies the shared with membership for the work package" do
+ expect(subject).to be_success
+ expect(project_copy.members.count).to eq 2
+
+ shared_wp_member = project_copy.members.find_by(entity_type: "WorkPackage")
+ expect(shared_wp_member.principal).to eq(source_wp_shared_with_user)
+ expect(shared_wp_member.roles).to contain_exactly(wp_role)
+
+ copied_wp = project_copy.work_packages.find_by(subject: "source wp")
+ expect(shared_wp_member.entity).to eq(copied_wp)
+ end
+
+ context "having disabled" do
+ let(:only_args) { %w[work_packages] }
+
+ it "copies the standard membership for the work package" do
+ expect(subject).to be_success
+ expect(project_copy.members.count).to eq 1
+
+ wp_member = project_copy.members.find_by(user_id: current_user.id)
+ expect(wp_member.principal).to eq(current_user)
+ expect(wp_member).to be_project_role
+ end
+ end
+ end
+
context "with versions" do
let(:version) { create(:version, project: source) }
let(:version2) { create(:version, project: source) }