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

Prevent self-assignment of work package parent

Oliver Günthercd2d736c1868
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index eb840f85902..b7dde3912c2 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -117,6 +117,7 @@ module WorkPackages
validate :validate_parent_exists
validate :validate_parent_in_same_project
validate :validate_parent_not_subtask
+ validate :validate_parent_not_self
validate :validate_status_exists
validate :validate_status_transition
@@ -231,6 +232,13 @@ module WorkPackages
end
end
+ def validate_parent_not_self
+ if model.parent == model
+
+ errors.add :parent, :cannot_be_self_assigned
+ end
+ end
+
def validate_parent_in_same_project
if parent_in_different_project?
errors.add :parent, :cannot_be_in_another_project
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 44496dcb977..4fbda91d536 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -761,6 +761,7 @@ en:
not_start_date: "is not on start date, although this is required for milestones."
parent:
cannot_be_milestone: "cannot be a milestone."
+ cannot_be_self_assigned: "cannot be assigned to itself."
cannot_be_in_another_project: "cannot be in another project."
not_a_valid_parent: "is invalid."
start_date:
diff --git a/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb b/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb
index b9e9ed21199..a95df4a8408 100644
--- a/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb
+++ b/modules/backlogs/spec/contracts/work_packages/base_contract_spec.rb
@@ -430,7 +430,7 @@ describe WorkPackages::BaseContract, type: :model do
let(:work_package) { story }
describe 'WITH a story as its parent' do
- let(:parent) { story }
+ let(:parent) { story2 }
it_behaves_like 'project id unrestricted by parent'
end
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index 3bc8d626067..2d797e2f286 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -579,6 +579,15 @@ describe WorkPackages::BaseContract do
contract.errors.symbols_for(:base)
end
+ context 'when self assigning' do
+ let(:parent) { work_package }
+
+ it 'returns an error for the aparent' do
+ expect(contract.validate).to eq false
+ expect(contract.errors.symbols_for(:parent)).to eq [:cannot_be_self_assigned]
+ end
+ end
+
context 'a relation exists between the parent and its ancestors and the work package and its descendants' do
let(:parent) { child }