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
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.rbindex eb840f85902..b7dde3912c2 100644--- a/app/contracts/work_packages/base_contract.rb+++ b/app/contracts/work_packages/base_contract.rb@@ -117,6 +117,7 @@ module WorkPackagesvalidate :validate_parent_existsvalidate :validate_parent_in_same_projectvalidate :validate_parent_not_subtask+ validate :validate_parent_not_selfvalidate :validate_status_existsvalidate :validate_status_transition@@ -231,6 +232,13 @@ module WorkPackagesendend+ def validate_parent_not_self+ if model.parent == model++ errors.add :parent, :cannot_be_self_assigned+ end+ end+def validate_parent_in_same_projectif parent_in_different_project?errors.add :parent, :cannot_be_in_another_projectdiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 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.rbindex 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 dolet(: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'enddiff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex 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 docontract.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' dolet(:parent) { child }