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

fixup! [#48001] Restrict timestamps outside enterprise to "yesterday"in query timestamps backend

Dombi Attilae83eb05ea1e1
diff --git a/app/models/query.rb b/app/models/query.rb
index ff71834f2a2..0879f703c29 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -54,7 +54,7 @@ class Query < ApplicationRecord
validate :validate_sort_criteria
validate :validate_group_by
validate :validate_show_hierarchies
- validate :validate_timestamps, unless: -> { EnterpriseToken.allows_to?(:baseline_comparison) }
+ validate :validate_timestamps
include Scopes::Scoped
scopes :visible,
@@ -144,8 +144,7 @@ class Query < ApplicationRecord
end
def validate_timestamps
- forbidden_timestamps = timestamps.select { |t| t.to_time < Date.yesterday }
-
+ forbidden_timestamps = timestamps - allowed_timestamps
if forbidden_timestamps.any?
errors.add :timestamps, :forbidden, values: forbidden_timestamps.join(", ")
end
@@ -175,6 +174,7 @@ class Query < ApplicationRecord
valid_group_by_subset!
valid_sort_criteria_subset!
valid_column_subset!
+ valid_timestamps_subset!
end
def add_filter(field, operator, values)
@@ -425,6 +425,12 @@ class Query < ApplicationRecord
end
end
+ def allowed_timestamps
+ return timestamps if EnterpriseToken.allows_to?(:baseline_comparison)
+
+ timestamps.reject { |t| t.to_time < Date.yesterday }
+ end
+
def valid_filter_subset!
filters.each(&:valid_values!).select! do |filter|
filter.available? && filter.valid?
@@ -450,4 +456,8 @@ class Query < ApplicationRecord
self.column_names &= available_names
end
+
+ def valid_timestamps_subset!
+ self.timestamps &= allowed_timestamps
+ end
end
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index c825d9dea6f..e76d83cf74a 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -1418,7 +1418,7 @@ describe API::V3::WorkPackages::WorkPackageRepresenter do
member_with_permissions: %i[view_work_packages view_file_links])
end
- describe 'attributesByTimestamp', with_flag: { show_changes: true } do
+ describe 'attributesByTimestamp', with_ee: %i[baseline_comparison], with_flag: { show_changes: true } do
it 'states whether the work package matches the query filters at the timestamp' do
expect(subject)
.to be_json_eql(true.to_json)
diff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rb
index 32dcd7ca42a..7e973018510 100644
--- a/spec/models/journable/with_historic_attributes_spec.rb
+++ b/spec/models/journable/with_historic_attributes_spec.rb
@@ -28,7 +28,8 @@
require 'spec_helper'
-describe Journable::WithHistoricAttributes, with_flag: { show_changes: true } do
+describe Journable::WithHistoricAttributes,
+ with_ee: %i[baseline_comparison], with_flag: { show_changes: true } do
create_shared_association_defaults_for_work_package_factory
shared_let(:baseline_time) { "2022-01-01".to_time }
diff --git a/spec/models/query/results_filter_on_historic_data_spec.rb b/spec/models/query/results_filter_on_historic_data_spec.rb
index 86e8a4016da..cad5a905199 100644
--- a/spec/models/query/results_filter_on_historic_data_spec.rb
+++ b/spec/models/query/results_filter_on_historic_data_spec.rb
@@ -30,6 +30,7 @@ require 'spec_helper'
describe Query::Results,
'Filter on historic data',
+ with_ee: %i[baseline_comparison],
with_flag: { show_changes: true },
with_mail: false do
let(:historic_time) { "2022-08-01".to_datetime }
diff --git a/spec/models/query/timestamps_spec.rb b/spec/models/query/timestamps_spec.rb
index ce96173dd44..5aaf6fff699 100644
--- a/spec/models/query/timestamps_spec.rb
+++ b/spec/models/query/timestamps_spec.rb
@@ -28,7 +28,8 @@
require 'spec_helper'
-describe Query::Timestamps, with_flag: { show_changes: true } do
+describe Query::Timestamps,
+ with_ee: %i[baseline_comparison], with_flag: { show_changes: true } do
describe "#timestamps" do
subject { query.timestamps }
diff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rb
index dce4c856c69..fe99934f936 100644
--- a/spec/models/query_spec.rb
+++ b/spec/models/query_spec.rb
@@ -577,7 +577,7 @@ describe Query,
describe '#valid_subset!' do
let(:valid_status) { build_stubbed(:status) }
- context 'filters' do
+ context 'with filters' do
before do
allow(Status)
.to receive(:all)
@@ -630,7 +630,7 @@ describe Query,
end
end
- context 'group_by' do
+ context 'with group_by' do
before do
query.group_by = group_by
end
@@ -656,7 +656,7 @@ describe Query,
end
end
- context 'sort_criteria' do
+ context 'with sort_criteria' do
before do
query.sort_criteria = sort_by
end
@@ -701,7 +701,7 @@ describe Query,
end
end
- context 'columns' do
+ context 'with columns' do
before do
query.column_names = columns
end
@@ -740,7 +740,7 @@ describe Query,
end
end
- context 'highlighted_attributes' do
+ context 'with highlighted_attributes' do
let(:highlighted_attributes) { %i{status priority due_date foo} }
before do
@@ -754,6 +754,31 @@ describe Query,
.to match_array %i{status priority due_date}
end
end
+
+ context 'with timestamps', with_flag: { show_changes: true } do
+ let(:timestamps) do
+ [3.weeks.ago.iso8601, 'oneWeekAgo@12:00+00:00', 'oneDayAgo@12:00+00:00', 'PT0S']
+ end
+
+ before do
+ query.timestamps = timestamps
+ query.valid_subset!
+ end
+
+ context 'without EE', with_ee: false do
+ it 'removes the forbidden values' do
+ expect(query.timestamps)
+ .to match_array %w{oneDayAgo@12:00+00:00 PT0S}
+ end
+ end
+
+ context 'with EE', with_ee: %i[baseline_comparison] do
+ it 'allows all valid values' do
+ expect(query.timestamps)
+ .to match_array timestamps
+ end
+ end
+ end
end
describe '#filter_for' do