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

Update project eagerloading wrapper custom field loading to check view_project_attributes permission

Dombi Attilae818d543d6d2
diff --git a/lib/api/v3/projects/project_eager_loading_wrapper.rb b/lib/api/v3/projects/project_eager_loading_wrapper.rb
index c096b34a413..f9f5379f484 100644
--- a/lib/api/v3/projects/project_eager_loading_wrapper.rb
+++ b/lib/api/v3/projects/project_eager_loading_wrapper.rb
@@ -65,9 +65,11 @@ module API
def custom_fields_from_projects
ProjectCustomFieldProjectMapping
- .eager_load(:project_custom_field)
+ .eager_load(:project_custom_field, :project)
.merge(ProjectCustomField.visible)
.each_with_object(Hash.new { |h, k| h[k] = [] }) do |mapping, acc|
+ next unless User.current.allowed_in_project?(:view_project_attributes, mapping.project)
+
acc[mapping.project_id] << mapping.project_custom_field
end
end
diff --git a/spec/requests/api/v3/projects/index_resource_spec.rb b/spec/requests/api/v3/projects/index_resource_spec.rb
index 479b229612b..a9cfc103f63 100644
--- a/spec/requests/api/v3/projects/index_resource_spec.rb
+++ b/spec/requests/api/v3/projects/index_resource_spec.rb
@@ -408,10 +408,13 @@ RSpec.describe "API v3 Project resource index", content_type: :json do
create(:work_package, project: public_wp_share_project)
end
shared_let(:project_cf) do
- create(:project_custom_field_project_mapping, project:).project_custom_field
- end
- shared_let(:other_cf) do
- create(:project_custom_field_project_mapping, project: other_project).project_custom_field
+ # This custom field is enabled in both project and other_project to test enabled
+ # custom field state overflow between 2 projects.
+ pcf = create(:project_custom_field_project_mapping, project:).project_custom_field
+ create(:project_custom_field_project_mapping,
+ project: other_project,
+ project_custom_field: pcf)
+ pcf
end
shared_let(:public_cf) do
create(:project_custom_field_project_mapping, project: public_project).project_custom_field
@@ -468,7 +471,8 @@ RSpec.describe "API v3 Project resource index", content_type: :json do
)
end
- it "does not return the project attributes for a private project without view_project_attributes" do
+ it "does not return the project attributes for a private project without view_project_attributes " \
+ "even if the same custom field is active in other_project" do
expect(subject)
.to be_json_eql(project.name.to_json)
.at_path("_embedded/elements/3/name")
@@ -478,13 +482,14 @@ RSpec.describe "API v3 Project resource index", content_type: :json do
)
end
- it "returns project attributes for other project with view_project_attributes" do
+ it "returns project attributes for other project with view_project_attributes for the same " \
+ "custom field enabled in the project too" do
expect(subject)
.to be_json_eql(other_project.name.to_json)
.at_path("_embedded/elements/4/name")
expect(subject).to have_json_path(
- "_embedded/elements/4/#{other_cf.attribute_name(:camel_case)}"
+ "_embedded/elements/4/#{project_cf.attribute_name(:camel_case)}"
)
end
end