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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 06763da1ca7..18bdf8dc577 100644--- a/app/contracts/queries/base_contract.rb+++ b/app/contracts/queries/base_contract.rb@@ -60,7 +60,7 @@ module Queriesvalidate :validate_projectvalidate :user_allowed_to_make_public- validate :allowed_timestamps+ validate :timestamps_are_parsabledef validate_projecterrors.add :project, :error_not_found if project_id.present? && !project_visible?@@ -84,20 +84,12 @@ module Queriesendend- 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(", ")- endendendenddiff --git a/app/models/query.rb b/app/models/query.rbindex 6ec942abe6c..ff71834f2a2 100644--- a/app/models/query.rb+++ b/app/models/query.rb@@ -54,6 +54,7 @@ class Query < ApplicationRecordvalidate :validate_sort_criteriavalidate :validate_group_byvalidate :validate_show_hierarchies+ validate :validate_timestamps, unless: -> { EnterpriseToken.allows_to?(:baseline_comparison) }include Scopes::Scopedscopes :visible,@@ -142,6 +143,14 @@ class Query < ApplicationRecordendend+ 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 hiddenviews.empty?end@@ -251,7 +260,7 @@ class Query < ApplicationRecordcolumn_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_listelsecolumn_names@@ -275,7 +284,7 @@ class Query < ApplicationRecordenddef has_column?(column)- column_names && column_names.include?(column.name)+ column_names&.include?(column.name)enddef has_default_columns?diff --git a/docs/api/apiv3/paths/project_queries_default.yml b/docs/api/apiv3/paths/project_queries_default.ymlindex 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: queryrequired: falsediff --git a/docs/api/apiv3/paths/queries_default.yml b/docs/api/apiv3/paths/queries_default.ymlindex 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: querydiff --git a/docs/api/apiv3/paths/query.yml b/docs/api/apiv3/paths/query.ymlindex 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: querydiff --git a/docs/api/apiv3/paths/work_package.yml b/docs/api/apiv3/paths/work_package.ymlindex 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: queryname: timestampsrequired: falsediff --git a/docs/api/apiv3/paths/work_packages.yml b/docs/api/apiv3/paths/work_packages.ymlindex 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: queryname: timestampsdiff --git a/docs/api/apiv3/tags/baseline_comparisons.yml b/docs/api/apiv3/tags/baseline_comparisons.ymlindex 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 OverviewWhen 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.ymlindex 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 UTCassociated 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.## Colorsdiff --git a/docs/api/apiv3/tags/queries.yml b/docs/api/apiv3/tags/queries.ymlindex 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.rbindex 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] dolet(: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)- enddescribe '.new_default' doit 'set the default sortation' doquery = described_class.new_defaultexpect(query.sort_criteria)- .to match_array([['id', 'asc']])+ .to contain_exactly(['id', 'asc'])endit 'does not use the default sortation if an order is provided' doquery = described_class.new_default(sort_criteria: [['id', 'asc']])expect(query.sort_criteria)- .to match_array([['id', 'asc']])+ .to contain_exactly(['id', 'asc'])endcontext 'with global subprojects include', with_settings: { display_subprojects_work_packages: true } do@@ -121,6 +108,118 @@ describe Query doendend+ 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' docontext 'with EE' doit '#highlighted_attributes accepts valid values' do@@ -179,9 +278,7 @@ describe Query doendend- context 'without EE' do- let(:conditional_highlighting_allowed) { false }-+ context 'without EE', with_ee: false doit 'always returns :none as highlighting_mode' doquery.highlighting_mode = 'status'expect(query.highlighting_mode).to eq(:none)@@ -316,9 +413,7 @@ describe Query doexpect(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 doit 'excludes the relation columns' doexpect(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 doit 'excludes the relation columns' doexpect(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 doit 'excludes the relation columns' doexpect(query.displayable_columns.map(&:name)).not_to include(:relations_of_type_relation1,:relations_of_type_relation2)@@ -411,9 +502,7 @@ describe Query doendend- 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 doit 'has all static columns, cf columns but no relation columns' doexpected_columns = %i(id project assigned_to authorcategory created_at due_date estimated_hours@@ -444,7 +533,7 @@ describe Query doendit 'is not valid and creates an error' do- expect(query.valid?).to be_falsey+ expect(query).not_to be_validexpect(query.errors[:base].first).to include(I18n.t('activerecord.errors.messages.blank'))endend@@ -480,7 +569,7 @@ describe Query doendit 'is not valid' do- expect(query.valid?).to be_falsey+ expect(query).not_to be_validendendend@@ -513,7 +602,7 @@ describe Query doit 'leaves only the valid value' doexpect(query.filters[0].values)- .to match_array [valid_status.id.to_s]+ .to contain_exactly(valid_status.id.to_s)endend@@ -597,7 +686,7 @@ describe Query doit 'is valid' doexpect(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'])endend@@ -607,7 +696,7 @@ describe Query doit 'removes the offending values from sort' doquery.valid_subset!- expect(query.sort_criteria).to match_array [['project', 'desc']]+ expect(query.sort_criteria).to contain_exactly(['project', 'desc'])endendend@@ -646,7 +735,7 @@ describe Query doquery.valid_subset!expect(query.column_names)- .to match_array [:status]+ .to contain_exactly(:status)endendend