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

introduce cache_if option for custom field injector

ulferts8742bcb8057f
diff --git a/lib/api/decorators/linked_resource.rb b/lib/api/decorators/linked_resource.rb
index 9f8faef2562..1cc72d0acce 100644
--- a/lib/api/decorators/linked_resource.rb
+++ b/lib/api/decorators/linked_resource.rb
@@ -61,11 +61,12 @@ module API
setter:,
link:,
uncacheable_link: false,
+ link_cache_if: nil,
show_if: ->(*) { true },
skip_render: nil,
embedded: true)
- link(link_attr(name, uncacheable_link), &link)
+ link(link_attr(name, uncacheable_link, link_cache_if), &link)
property name,
exec_context: :decorator,
@@ -83,11 +84,12 @@ module API
setter:,
link:,
uncacheable_link: false,
+ link_cache_if: nil,
show_if: ->(*) { true },
skip_render: nil,
embedded: true)
- links(link_attr(name, uncacheable_link), &link)
+ links(link_attr(name, uncacheable_link, link_cache_if), &link)
property name,
exec_context: :decorator,
@@ -144,9 +146,10 @@ module API
skip_render:)
end
- def link_attr(name, uncacheable)
+ def link_attr(name, uncacheable, link_cache_if)
links_attr = { rel: name.to_s.camelize(:lower) }
links_attr[:uncacheable] = true if uncacheable
+ links_attr[:cache_if] = link_cache_if if link_cache_if
links_attr
end
diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rb
index eef19572a3f..82bf0cf2bf6 100644
--- a/lib/api/v3/projects/project_representer.rb
+++ b/lib/api/v3/projects/project_representer.rb
@@ -39,6 +39,12 @@ module API
include API::Decorators::FormattableProperty
extend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass
+ def self.current_user_view_allowed_lambda
+ ->(*) { current_user.allowed_in_project?(:view_project, represented) || current_user.allowed_globally?(:add_project) }
+ end
+
+ custom_field_injector cache_if: current_user_view_allowed_lambda
+
cached_representer disabled: false
self_link
@@ -183,15 +189,18 @@ module API
property :public
formattable_property :description,
- cache_if: -> { current_user.allowed_in_project?(:view_project, represented) }
+ cache_if: current_user_view_allowed_lambda
date_time_property :created_at
date_time_property :updated_at
resource :status,
- show_if: ->(*) { !current_user.allowed_in_project?(:view_project, represented) },
- uncacheable_link: true,
+ skip_render: ->(*) {
+ !current_user.allowed_in_project?(:view_project, represented) &&
+ !current_user.allowed_globally?(:add_project)
+ },
+ link_cache_if: current_user_view_allowed_lambda,
getter: ->(*) {
next unless represented.status_code
@@ -199,9 +208,7 @@ module API
.create(represented.status_code, current_user:, embed_links:)
},
link: ->(*) {
- if !current_user.allowed_in_project?(:view_project, represented)
- { href: nil }
- elsif represented.status_code
+ if represented.status_code
{
href: api_v3_paths.project_status(represented.status_code),
title: I18n.t(:"activerecord.attributes.project.status_codes.#{represented.status_code}",
@@ -222,7 +229,7 @@ module API
}
formattable_property :status_explanation,
- cache_if: -> { current_user.allowed_in_project?(:view_project, represented) }
+ cache_if: current_user_view_allowed_lambda
def _type
'Project'
diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rb
index 4895ff7d7de..3c61e835050 100644
--- a/lib/api/v3/utilities/custom_field_injector.rb
+++ b/lib/api/v3/utilities/custom_field_injector.rb
@@ -61,7 +61,7 @@ module API
def create_value_representer(custom_fields, representer)
new_representer_class_with(representer) do |injector|
custom_fields.each do |custom_field|
- injector.inject_value(custom_field)
+ injector.inject_value(custom_field, representer.custom_field_injector_config)
end
end
end
@@ -116,12 +116,12 @@ module API
end
end
- def inject_value(custom_field)
+ def inject_value(custom_field, config)
case custom_field.field_format
when *LINK_FORMATS
- inject_link_value(custom_field)
+ inject_link_value(custom_field, config)
else
- inject_property_value(custom_field)
+ inject_property_value(custom_field, config)
end
end
@@ -181,7 +181,7 @@ module API
options: cf_options(custom_field)
end
- def inject_link_value(custom_field)
+ def inject_link_value(custom_field, config)
name = property_name(custom_field)
expected_namespace = NAMESPACE_MAP[custom_field.field_format]
@@ -197,6 +197,8 @@ module API
@class.send(method,
property_name(custom_field),
+ link_cache_if: config[:cache_if],
+ skip_render: config[:cache_if] ? ->(*) { !instance_exec(&config[:cache_if]) } : nil,
link:,
setter:,
getter:)
@@ -242,11 +244,12 @@ module API
end
end
- def inject_property_value(custom_field)
+ def inject_property_value(custom_field, config)
@class.property custom_field.attribute_name.to_sym,
as: property_name(custom_field),
getter: property_value_getter_for(custom_field),
setter: property_value_setter_for(custom_field),
+ cache_if: config[:cache_if],
render_nil: true
end
diff --git a/lib/api/v3/utilities/custom_field_sum_injector.rb b/lib/api/v3/utilities/custom_field_sum_injector.rb
index 5b3c94fe246..58bb65fdfdf 100644
--- a/lib/api/v3/utilities/custom_field_sum_injector.rb
+++ b/lib/api/v3/utilities/custom_field_sum_injector.rb
@@ -34,7 +34,7 @@ module API
inject_basic_schema(custom_field)
end
- def inject_basic_schema(custom_field)
+ def inject_basic_schema(custom_field, _options = {})
@class.schema property_name(custom_field),
type: TYPE_MAP[custom_field.field_format],
name_source: ->(*) { custom_field.name },
@@ -43,7 +43,7 @@ module API
show_if: ->(*) { custom_field.summable? }
end
- def inject_property_value(custom_field)
+ def inject_property_value(custom_field, _options = {})
@class.property property_name(custom_field),
getter: property_value_getter_for(custom_field),
setter: property_value_setter_for(custom_field),
diff --git a/spec/lib/api/v3/projects/project_collection_representer_spec.rb b/spec/lib/api/v3/projects/project_collection_representer_spec.rb
index 37372ab8c73..0e5556b210a 100644
--- a/spec/lib/api/v3/projects/project_collection_representer_spec.rb
+++ b/spec/lib/api/v3/projects/project_collection_representer_spec.rb
@@ -75,8 +75,8 @@ RSpec.describe API::V3::Projects::ProjectCollectionRepresenter do
end
describe '.checked_permissions' do
- it 'lists add_work_packages' do
- expect(described_class.checked_permissions).to match_array([:add_work_packages])
+ it 'lists add_work_packages and view_projects' do
+ expect(described_class.checked_permissions).to contain_exactly(:add_work_packages, :view_project)
end
end
end
diff --git a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
index a3d2c634eae..b4754658a9b 100644
--- a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
@@ -160,6 +160,29 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' do
expect(subject).not_to have_json_path("customField#{int_custom_field.id}")
end
end
+
+ context 'if the user is no admin and the field is visible' do
+ before do
+ int_custom_field.visible = true
+ end
+
+ it "has a property for the int custom field" do
+ expect(subject).to be_json_eql(int_custom_value.value.to_json)
+ .at_path("customField#{int_custom_field.id}")
+ end
+ end
+
+ context 'if the user lacks the :view_project permission' do
+ let(:permissions) { [] }
+
+ before do
+ int_custom_field.visible = true
+ end
+
+ it "has no property for the int custom field" do
+ expect(subject).not_to have_json_path("customField#{int_custom_field.id}")
+ end
+ end
end
end
@@ -359,12 +382,9 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' do
context 'if the user does not have the view_project permission' do
let(:permissions) { [] }
- it_behaves_like 'has an untitled link' do
+ it_behaves_like 'has no link' do
let(:link) { 'status' }
- let(:href) { nil }
end
-
- it_behaves_like 'no property', 'status'
end
end
@@ -505,7 +525,7 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' do
version_custom_field.visible = false
end
- it "has no property for the int custom field" do
+ it "does not link the custom field" do
expect(subject).not_to have_json_path("links/customField#{version_custom_field.id}")
end
end
@@ -516,6 +536,14 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' do
.at_path("_links/customField#{version_custom_field.id}/href")
end
end
+
+ context 'if the user lacks the :view_project permission' do
+ let(:permissions) { [] }
+
+ it 'does not link the custom field' do
+ expect(subject).not_to have_json_path("links/customField#{version_custom_field.id}")
+ end
+ end
end
describe 'update' do
@@ -613,6 +641,38 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, 'rendering' do
end
end
end
+
+ describe 'status' do
+ let(:embedded_path) { '_embedded/status' }
+
+ it 'has the status embedded' do
+ expect(generated)
+ .to be_json_eql('ProjectStatus'.to_json)
+ .at_path("#{embedded_path}/_type")
+
+ expect(generated)
+ .to be_json_eql(I18n.t("activerecord.attributes.project.status_codes.#{project.status_code}").to_json)
+ .at_path("#{embedded_path}/name")
+ end
+
+ context 'if the status_code is nil' do
+ before { project.status_code = nil }
+
+ it 'has no status embedded' do
+ expect(generated)
+ .not_to have_json_path(embedded_path)
+ end
+ end
+
+ context 'if the user does not have the view_project permission' do
+ let(:permissions) { [] }
+
+ it 'has no status embedded' do
+ expect(generated)
+ .not_to have_json_path(embedded_path)
+ end
+ end
+ end
end
describe 'caching' do
diff --git a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb b/spec/lib/api/v3/utilities/custom_field_injector_spec.rb
index 27ac3a4a65b..642641db58f 100644
--- a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb
+++ b/spec/lib/api/v3/utilities/custom_field_injector_spec.rb
@@ -340,7 +340,13 @@ RSpec.describe API::V3::Utilities::CustomFieldInjector do
end
end
- let(:base_class) { Class.new(API::Decorators::Single) }
+ let(:base_class) do
+ Class.new(API::Decorators::Single) do
+ def self.custom_field_injector_config
+ {}
+ end
+ end
+ end
let(:modified_class) { described_class.create_value_representer([custom_field], base_class) }
let(:represented) do
double('represented',
@@ -522,7 +528,13 @@ RSpec.describe API::V3::Utilities::CustomFieldInjector do
end
describe '#inject_patchable_link_value' do
- let(:base_class) { Class.new(API::Decorators::Single) }
+ let(:base_class) do
+ Class.new(API::Decorators::Single) do
+ def self.custom_field_injector_config
+ {}
+ end
+ end
+ end
let(:modified_class) do
described_class.create_value_representer([custom_field], base_class)
end