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

Forbid setting the parent to a work package the user does not have access to

Klaus Zanders769f4c9d9d60
diff --git a/app/contracts/work_packages/update_contract.rb b/app/contracts/work_packages/update_contract.rb
index f8e54351e9b..9e9d490b8dc 100644
--- a/app/contracts/work_packages/update_contract.rb
+++ b/app/contracts/work_packages/update_contract.rb
@@ -43,6 +43,8 @@ module WorkPackages
validate :can_move_to_milestone
+ validate :user_allowed_to_change_parent
+
default_attribute_permission :edit_work_packages
attribute_permission :project_id, :move_work_packages
@@ -78,5 +80,14 @@ module WorkPackages
errors.add :type, :cannot_be_milestone_due_to_children
end
end
+
+ def user_allowed_to_change_parent
+ return if model.parent_id.nil?
+ return unless model.parent_id_changed?
+
+ unless model.parent.visible?
+ errors.add :parent_id, :error_unauthorized
+ end
+ end
end
end
diff --git a/spec/contracts/work_packages/update_contract_spec.rb b/spec/contracts/work_packages/update_contract_spec.rb
index 33752715c18..d98772abd06 100644
--- a/spec/contracts/work_packages/update_contract_spec.rb
+++ b/spec/contracts/work_packages/update_contract_spec.rb
@@ -291,8 +291,11 @@ RSpec.describe WorkPackages::UpdateContract do
describe 'parent_id' do
shared_let(:parent) { create(:work_package) }
+ let(:parent_visible) { true }
+
before do
work_package.parent_id = parent.id
+ allow(work_package.parent).to receive(:visible?).and_return(parent_visible)
contract.validate
end
@@ -339,6 +342,12 @@ RSpec.describe WorkPackages::UpdateContract do
it { expect(contract.errors.symbols_for(:subject)).to include(:error_readonly) }
end
end
+
+ context 'when the user does not have access to the parent' do
+ let(:parent_visible) { false }
+
+ it { expect(contract.errors.symbols_for(:parent_id)).to include(:error_unauthorized) }
+ end
end
describe '#writable_attributes' do