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

Add validation for 'to' work package

Mir Bhatiad9bbc37e96c5
diff --git a/app/contracts/relations/base_contract.rb b/app/contracts/relations/base_contract.rb
index cf6d293eb68..59754616162 100644
--- a/app/contracts/relations/base_contract.rb
+++ b/app/contracts/relations/base_contract.rb
@@ -78,8 +78,12 @@ module Relations
def validate_user_allowed
return if model.to_id.nil? || model.from_id.nil?
- unless manage_relations?
- errors.add :base, :error_unauthorized
+ unless from_manageable?
+ errors.add :from_id, :error_not_manageable
+ end
+
+ unless to_manageable?
+ errors.add :to_id, :error_not_manageable
end
end
@@ -99,8 +103,12 @@ module Relations
::WorkPackage.visible(user)
end
- def manage_relations?
+ def from_manageable?
user.allowed_in_work_package?(:manage_work_package_relations, model.from)
end
+
+ def to_manageable?
+ user.allowed_in_work_package?(:manage_work_package_relations, model.to)
+ end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 52f97d2da2e..f503b485344 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1569,9 +1569,13 @@ en:
circular_dependency: "The relationship creates a circle of relationships."
attributes:
to_id:
+ format: "The selected work package %{message}"
error_readonly: "cannot be changed for existing relations."
+ error_not_manageable: "cannot be added because you do not have edit permissions for the selected work package."
from_id:
+ format: "The selected work package %{message}"
error_readonly: "cannot be changed for existing relations."
+ error_not_manageable: "cannot be added because you do not have edit permissions for the selected work package."
repository:
not_available: "SCM vendor is not available"
not_whitelisted: "is not allowed by the configuration."
diff --git a/spec/contracts/relations/shared_contract_examples.rb b/spec/contracts/relations/shared_contract_examples.rb
index c254130638a..be45b88573a 100644
--- a/spec/contracts/relations/shared_contract_examples.rb
+++ b/spec/contracts/relations/shared_contract_examples.rb
@@ -99,6 +99,7 @@ RSpec.shared_examples_for "relation contract" do
before do
mock_permissions_for(current_user) do |mock|
mock.allow_in_project *permissions, project: canonical_relation_from.project
+ mock.allow_in_project *permissions, project: canonical_relation_to.project
end
end
@@ -108,7 +109,17 @@ RSpec.shared_examples_for "relation contract" do
context "when lacking the necessary permission" do
let(:permissions) { [] }
- it_behaves_like "contract is invalid", base: :error_unauthorized
+ it_behaves_like "contract is invalid", from_id: :error_not_manageable
+ end
+
+ context "when lacking the necessary permission for the to work package" do
+ before do
+ mock_permissions_for(current_user) do |mock|
+ mock.allow_in_project *permissions, project: canonical_relation_from.project
+ end
+ end
+
+ it_behaves_like "contract is invalid", to_id: :error_not_manageable
end
context "when the work package for from is not visible" do
diff --git a/spec/requests/api/v3/relations/relations_api_spec.rb b/spec/requests/api/v3/relations/relations_api_spec.rb
index 549af99b330..c74f85af5ef 100644
--- a/spec/requests/api/v3/relations/relations_api_spec.rb
+++ b/spec/requests/api/v3/relations/relations_api_spec.rb
@@ -305,7 +305,7 @@ RSpec.describe "API v3 Relation resource", content_type: :json do
it "lets the user know the attribute is read-only" do
msg = JSON.parse(last_response.body)["message"]
- expect(msg).to include "Related work package cannot be changed for existing relations."
+ expect(msg).to include "The selected work package cannot be changed for existing relations."
end
end
end