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 Attila54ad3e6283d7
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rb
index 06763da1ca7..18bdf8dc577 100644
--- a/app/contracts/queries/base_contract.rb
+++ b/app/contracts/queries/base_contract.rb
@@ -60,7 +60,7 @@ module Queries
validate :validate_project
validate :user_allowed_to_make_public
- validate :allowed_timestamps
+ validate :timestamps_are_parsable
def validate_project
errors.add :project, :error_not_found if project_id.present? && !project_visible?
@@ -84,20 +84,12 @@ module Queries
end
end
- def allowed_timestamps
- valid_timestamps, invalid_timestamps = model.timestamps.partition(&:valid?)
+ def timestamps_are_parsable
+ invalid_timestamps = model.timestamps.reject(&:valid?)
if invalid_timestamps.any?
errors.add :timestamps, :invalid, values: invalid_timestamps.join(", ")
end
-
- return if EnterpriseToken.allows_to?(:baseline_comparison)
-
- forbidden_timestamps = valid_timestamps.select { |t| t.to_time < Date.yesterday }
-
- if forbidden_timestamps.any?
- errors.add :timestamps, :forbidden, values: forbidden_timestamps.join(", ")
- end
end
end
end
diff --git a/app/models/query.rb b/app/models/query.rb
index 6ec942abe6c..ff71834f2a2 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -54,6 +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) }
include Scopes::Scoped
scopes :visible,
@@ -142,6 +143,14 @@ class Query < ApplicationRecord
end
end
+ def validate_timestamps
+ forbidden_timestamps = timestamps.select { |t| t.to_time < Date.yesterday }
+
+ if forbidden_timestamps.any?
+ errors.add :timestamps, :forbidden, values: forbidden_timestamps.join(", ")
+ end
+ end
+
def hidden
views.empty?
end
@@ -251,7 +260,7 @@ class Query < ApplicationRecord
column_list = if has_default_columns?
column_list = Setting.work_package_list_default_columns.dup.map(&:to_sym)
# Adds the project column by default for cross-project lists
- column_list += [:project] if project.nil? && !column_list.include?(:project)
+ column_list += [:project] if project.nil? && column_list.exclude?(:project)
column_list
else
column_names
@@ -275,7 +284,7 @@ class Query < ApplicationRecord
end
def has_column?(column)
- column_names && column_names.include?(column.name)
+ column_names&.include?(column.name)
end
def has_default_columns?
diff --git a/docs/api/apiv3/paths/project_queries_default.yml b/docs/api/apiv3/paths/project_queries_default.yml
index db861a9b316..a471450bfcf 100644
--- a/docs/api/apiv3/paths/project_queries_default.yml
+++ b/docs/api/apiv3/paths/project_queries_default.yml
@@ -82,6 +82,7 @@ get:
"oneWeekAgo@HH:MM+HH:MM", "oneMonthAgo@HH:MM+HH:MM".
The first "HH:MM" part represents the zero paded hours and minutes.
The last "+HH:MM" part represents the timezone offset from UTC associated with the time.
+ Values older than 1 day are accepted only with valid Enterprise Token available.
example: '2023-01-01,P-1Y,PT0S,lastWorkingDay@12:00'
in: query
required: false
diff --git a/docs/api/apiv3/paths/queries_default.yml b/docs/api/apiv3/paths/queries_default.yml
index 06bd753b47b..c2088ef325a 100644
--- a/docs/api/apiv3/paths/queries_default.yml
+++ b/docs/api/apiv3/paths/queries_default.yml
@@ -75,6 +75,7 @@ get:
The first "HH:MM" part represents the zero paded hours and minutes.
The last "+HH:MM" part represents the timezone offset from UTC associated with the time,
the offset can be positive or negative e.g."oneDayAgo@01:00+01:00", "oneDayAgo@01:00-01:00".
+ Values older than 1 day are accepted only with valid Enterprise Token available.
example: '2023-01-01,P-1Y,PT0S,lastWorkingDay@12:00'
in: query
diff --git a/docs/api/apiv3/paths/query.yml b/docs/api/apiv3/paths/query.yml
index ba2870ae3ce..98176d61b22 100644
--- a/docs/api/apiv3/paths/query.yml
+++ b/docs/api/apiv3/paths/query.yml
@@ -143,6 +143,7 @@ get:
The first "HH:MM" part represents the zero paded hours and minutes.
The last "+HH:MM" part represents the timezone offset from UTC associated with the time,
the offset can be positive or negative e.g."oneDayAgo@01:00+01:00", "oneDayAgo@01:00-01:00".
+ Values older than 1 day are accepted only with valid Enterprise Token available.
example: '2023-01-01,P-1Y,PT0S,lastWorkingDay@12:00'
in: query
diff --git a/docs/api/apiv3/paths/work_package.yml b/docs/api/apiv3/paths/work_package.yml
index 2babbcdcd2c..696bb52bb53 100644
--- a/docs/api/apiv3/paths/work_package.yml
+++ b/docs/api/apiv3/paths/work_package.yml
@@ -81,6 +81,7 @@ get:
the offset can be positive or negative e.g."oneDayAgo@01:00+01:00", "oneDayAgo@01:00-01:00".
Usually, the first timestamp is the baseline date, the last timestamp is the current date.
+ Values older than 1 day are accepted only with valid Enterprise Token available.
in: query
name: timestamps
required: false
diff --git a/docs/api/apiv3/paths/work_packages.yml b/docs/api/apiv3/paths/work_packages.yml
index 82e218d73aa..697404d217b 100644
--- a/docs/api/apiv3/paths/work_packages.yml
+++ b/docs/api/apiv3/paths/work_packages.yml
@@ -134,6 +134,7 @@ get:
the offset can be positive or negative e.g."oneDayAgo@01:00+01:00", "oneDayAgo@01:00-01:00".
Usually, the first timestamp is the baseline date, the last timestamp is the current date.
+ Values older than 1 day are accepted only with valid Enterprise Token available.
example: '2022-01-01T00:00:00Z,PT0S'
in: query
name: timestamps
diff --git a/docs/api/apiv3/tags/baseline_comparisons.yml b/docs/api/apiv3/tags/baseline_comparisons.yml
index 8e0583eb75c..5e329c6fb87 100644
--- a/docs/api/apiv3/tags/baseline_comparisons.yml
+++ b/docs/api/apiv3/tags/baseline_comparisons.yml
@@ -29,6 +29,8 @@ description: |-
The timestamps should be given in ascending temporal order, such that the first timestamp is the **baseline** timestamp, and the last timestamp is the **current** timestamp.
+ Values older than 1 day are accepted only with valid Enterprise Token available.
+
## Response Overview
When providing a `timestamps` parameter, the response has several additional properties:
diff --git a/docs/api/apiv3/tags/basic_objects.yml b/docs/api/apiv3/tags/basic_objects.yml
index 7849aa1fdb4..46e406feafc 100644
--- a/docs/api/apiv3/tags/basic_objects.yml
+++ b/docs/api/apiv3/tags/basic_objects.yml
@@ -241,6 +241,7 @@ description: |-
The `"HH:MM"` part represents the zero paded hours and minutes, e.g. `"oneMonthAgo@21:00+00:00"`. The last "+HH:MM" part represents the timezone offset from UTC
associated with the time, e.g. `"oneMonthAgo@21:00+02:00"` means a +2 hour timezone offset from UTC.
The offset can be positive or negative e.g."oneDayAgo@01:00+01:00", "oneDayAgo@01:00-01:00".
+ Values older than 1 day are accepted only with valid Enterprise Token available.
## Colors
diff --git a/docs/api/apiv3/tags/queries.yml b/docs/api/apiv3/tags/queries.yml
index b6bf88dc109..2fde6fb9e4c 100644
--- a/docs/api/apiv3/tags/queries.yml
+++ b/docs/api/apiv3/tags/queries.yml
@@ -32,23 +32,23 @@ description: |
## Local Properties
- | Property | Description | Type | Constraints | Supported operations |
- | :--------------: | ------------------------------------------------------ | ----------- | ------------------------------------ | -------------------- |
- | id | Query id | Integer | x > 0 | READ |
- | name | Query name | String | | READ/WRITE |
- | filters | A set of QueryFilters which will be applied to the work packages to determine the resulting work packages | []QueryFilterInstance | | READ/WRITE |
- | sums | Should sums (of supported properties) be shown? | Boolean | | READ/WRITE |
- | timelineVisible | Should the timeline mode be shown? | Boolean | | READ/WRITE |
- | timelineLabels | Which labels are shown in the timeline, empty when default | QueryTimelineLabels | | READ/WRITE |
- | timelineZoomLevel| Which zoom level should the timeline be rendered in? | String | days, weeks, months, quarters, years | READ/WRITE |
- | timestamps | **Only with valid Enterprise Token available:** The timestamps to filter by when showing changed attributes on work packages.| []Timestamp | | READ/WRITE |
- | highlightingMode | Which highlighting mode should the table have? | String | none, inline, status, priority, type | READ/WRITE |
- | showHierarchies | Should the hierarchy mode be enabled? | Boolean | | READ/WRITE |
- | hidden | Should the query be hidden from the query list? | Boolean | | READ/WRITE |
- | public | Can users besides the owner see the query? | Boolean | | READ/WRITE |
- | starred | Should the query be highlighted to the user? | Boolean | | READ |
- | createdAt | Time of creation | DateTime | not null | READ |
- | updatedAt | Time of the most recent change to the query | DateTime | not null | READ |
+ | Property | Description | Type | Constraints | Supported operations |
+ | :--------------: | -------------------------------------------------------------------------------------------------------- | --------------------- | ----------------------------------------------------------------------------------- | -------------------- |
+ | id | Query id | Integer | x > 0 | READ |
+ | name | Query name | String | | READ/WRITE |
+ | filters | A set of QueryFilters which will be applied to the work packages to determine the resulting work packages| []QueryFilterInstance | | READ/WRITE |
+ | sums | Should sums (of supported properties) be shown? | Boolean | | READ/WRITE |
+ | timelineVisible | Should the timeline mode be shown? | Boolean | | READ/WRITE |
+ | timelineLabels | Which labels are shown in the timeline, empty when default | QueryTimelineLabels | | READ/WRITE |
+ | timelineZoomLevel| Which zoom level should the timeline be rendered in? | String | days, weeks, months, quarters, years | READ/WRITE |
+ | timestamps | The timestamps to filter by when showing changed attributes on work packages.| []Timestamp | | **Values older than 1 day are accepted only with valid Enterprise Token available.**| READ/WRITE |
+ | highlightingMode | Which highlighting mode should the table have? | String | none, inline, status, priority, type | READ/WRITE |
+ | showHierarchies | Should the hierarchy mode be enabled? | Boolean | | READ/WRITE |
+ | hidden | Should the query be hidden from the query list? | Boolean | | READ/WRITE |
+ | public | Can users besides the owner see the query? | Boolean | | READ/WRITE |
+ | starred | Should the query be highlighted to the user? | Boolean | | READ |
+ | createdAt | Time of creation | DateTime | not null | READ |
+ | updatedAt | Time of the most recent change to the query | DateTime | not null | READ |
A query that is not assigned to a project (`"project": null`) is called a global query. Global queries filter work packages regardless of the project they are assigned to. As such, a different set of filters exists for those queries.
diff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rb
index a47feca78d8..dce4c856c69 100644
--- a/spec/models/query_spec.rb
+++ b/spec/models/query_spec.rb
@@ -28,37 +28,24 @@
require 'spec_helper'
-describe Query do
+describe Query,
+ with_ee: %i[baseline_comparison conditional_highlighting work_package_query_relation_columns] do
let(:query) { build(:query) }
let(:project) { create(:project) }
- let(:relation_columns_allowed) { true }
- let(:conditional_highlighting_allowed) { true }
-
- before do
- allow(EnterpriseToken)
- .to receive(:allows_to?)
- .with(:work_package_query_relation_columns)
- .and_return(relation_columns_allowed)
-
- allow(EnterpriseToken)
- .to receive(:allows_to?)
- .with(:conditional_highlighting)
- .and_return(conditional_highlighting_allowed)
- end
describe '.new_default' do
it 'set the default sortation' do
query = described_class.new_default
expect(query.sort_criteria)
- .to match_array([['id', 'asc']])
+ .to contain_exactly(['id', 'asc'])
end
it 'does not use the default sortation if an order is provided' do
query = described_class.new_default(sort_criteria: [['id', 'asc']])
expect(query.sort_criteria)
- .to match_array([['id', 'asc']])
+ .to contain_exactly(['id', 'asc'])
end
context 'with global subprojects include', with_settings: { display_subprojects_work_packages: true } do
@@ -121,6 +108,118 @@ describe Query do
end
end
+ describe 'timestamps', with_flag: { show_changes: true } do
+ subject { build(:query, timestamps:) }
+
+ context 'with EE', with_ee: %i[baseline_comparison] do
+ Timestamp::ALLOWED_DATE_KEYWORDS.each do |timestamp_date_keyword|
+ context "when the '#{timestamp_date_keyword}' value is provided" do
+ let(:timestamps) { ["#{timestamp_date_keyword}@12:00+00:00"] }
+
+ it { is_expected.to be_valid }
+ end
+ end
+
+ context "when the shortcut value 'now' is provided" do
+ let(:timestamps) { ["now"] }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when a duration value is provided" do
+ let(:timestamps) { ["P-2D"] }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when an iso8601 datetime value is provided" do
+ let(:timestamps) { [1.week.ago.iso8601] }
+
+ it { is_expected.to be_valid }
+ end
+ end
+
+ context 'without EE', with_ee: false do
+ context "when the 'oneDayAgo' value is provided" do
+ let(:timestamps) { ["oneDayAgo@12:00+00:00"] }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when the shortcut value 'now' is provided" do
+ let(:timestamps) { ["now"] }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when the 'PT0S' duration value is provided" do
+ let(:timestamps) { ["PT0S"] }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when the 'P-1D' duration value is provided" do
+ let(:timestamps) { ["P-1D"] }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when an iso8601 datetime value from yesterday is provided" do
+ let(:timestamps) { [1.day.ago.beginning_of_day.iso8601] }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when the 'lastWorkingDay' value is provided and it's yesterday" do
+ let(:timestamps) { "lastWorkingDay@00:00+00:00" }
+
+ it { is_expected.to be_valid }
+ end
+
+ Timestamp::ALLOWED_DATE_KEYWORDS[2..].each do |timestamp_date_keyword|
+ context "when the '#{timestamp_date_keyword}' value is provided" do
+ let(:timestamps) { ["#{timestamp_date_keyword}@12:00+00:00"] }
+
+ it 'is invalid' do
+ expect(subject).not_to be_valid
+ expect(subject.errors.symbols_for(:timestamps)).to eq [:forbidden]
+ end
+ end
+ end
+
+ context "when the 'lastWorkingDay' value is provided and it's before yesterday" do
+ let(:timestamps) { "lastWorkingDay@00:00+00:00" }
+
+ before do
+ allow(Day).to receive(:last_working) { Day.first }
+ end
+
+ it 'is invalid' do
+ expect(subject).not_to be_valid
+ expect(subject.errors.symbols_for(:timestamps)).to eq [:forbidden]
+ end
+ end
+
+ context "when a duration value older than yesterday is provided" do
+ let(:timestamps) { ["P-2D"] }
+
+ it 'is invalid' do
+ expect(subject).not_to be_valid
+ expect(subject.errors.symbols_for(:timestamps)).to eq [:forbidden]
+ end
+ end
+
+ context "when an iso8601 datetime value older than yesterday is provided" do
+ let(:timestamps) { [2.days.ago.end_of_day.iso8601] }
+
+ it 'is invalid' do
+ expect(subject).not_to be_valid
+ expect(subject.errors.symbols_for(:timestamps)).to eq [:forbidden]
+ end
+ end
+ end
+ end
+
describe 'highlighting' do
context 'with EE' do
it '#highlighted_attributes accepts valid values' do
@@ -179,9 +278,7 @@ describe Query do
end
end
- context 'without EE' do
- let(:conditional_highlighting_allowed) { false }
-
+ context 'without EE', with_ee: false do
it 'always returns :none as highlighting_mode' do
query.highlighting_mode = 'status'
expect(query.highlighting_mode).to eq(:none)
@@ -316,9 +413,7 @@ describe Query do
expect(query.displayable_columns.map(&:name)).not_to include :"relations_to_type_#{type_not_in_project.id}"
end
- context 'with the enterprise token disallowing relation columns' do
- let(:relation_columns_allowed) { false }
-
+ context 'with the enterprise token disallowing relation columns', with_ee: false do
it 'excludes the relation columns' do
expect(query.displayable_columns.map(&:name)).not_to include :"relations_to_type_#{type_in_project.id}"
end
@@ -335,9 +430,7 @@ describe Query do
:"relations_to_type_#{type_not_in_project.id}")
end
- context 'with the enterprise token disallowing relation columns' do
- let(:relation_columns_allowed) { false }
-
+ context 'with the enterprise token disallowing relation columns', with_ee: false do
it 'excludes the relation columns' do
expect(query.displayable_columns.map(&:name)).not_to include(:"relations_to_type_#{type_in_project.id}",
:"relations_to_type_#{type_not_in_project.id}")
@@ -358,9 +451,7 @@ describe Query do
:relations_of_type_relation2)
end
- context 'with the enterprise token disallowing relation columns' do
- let(:relation_columns_allowed) { false }
-
+ context 'with the enterprise token disallowing relation columns', with_ee: false do
it 'excludes the relation columns' do
expect(query.displayable_columns.map(&:name)).not_to include(:relations_of_type_relation1,
:relations_of_type_relation2)
@@ -411,9 +502,7 @@ describe Query do
end
end
- context 'with the enterprise token disallowing relation columns' do
- let(:relation_columns_allowed) { false }
-
+ context 'with the enterprise token disallowing relation columns', with_ee: false do
it 'has all static columns, cf columns but no relation columns' do
expected_columns = %i(id project assigned_to author
category created_at due_date estimated_hours
@@ -444,7 +533,7 @@ describe Query do
end
it 'is not valid and creates an error' do
- expect(query.valid?).to be_falsey
+ expect(query).not_to be_valid
expect(query.errors[:base].first).to include(I18n.t('activerecord.errors.messages.blank'))
end
end
@@ -480,7 +569,7 @@ describe Query do
end
it 'is not valid' do
- expect(query.valid?).to be_falsey
+ expect(query).not_to be_valid
end
end
end
@@ -513,7 +602,7 @@ describe Query do
it 'leaves only the valid value' do
expect(query.filters[0].values)
- .to match_array [valid_status.id.to_s]
+ .to contain_exactly(valid_status.id.to_s)
end
end
@@ -597,7 +686,7 @@ describe Query do
it 'is valid' do
expect(query).to be_valid
- expect(query.sort_criteria).to match_array [['id', 'asc'], ['start_date', 'asc']]
+ expect(query.sort_criteria).to contain_exactly(['id', 'asc'], ['start_date', 'asc'])
end
end
@@ -607,7 +696,7 @@ describe Query do
it 'removes the offending values from sort' do
query.valid_subset!
- expect(query.sort_criteria).to match_array [['project', 'desc']]
+ expect(query.sort_criteria).to contain_exactly(['project', 'desc'])
end
end
end
@@ -646,7 +735,7 @@ describe Query do
query.valid_subset!
expect(query.column_names)
- .to match_array [:status]
+ .to contain_exactly(:status)
end
end
end