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
check visibility at timestamps
ulferts915e4cf94ec7
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rbindex 08efe3bb2c6..0d638eafc36 100644--- a/app/models/journable/with_historic_attributes.rb+++ b/app/models/journable/with_historic_attributes.rb@@ -71,6 +71,11 @@# work_package.baseline_attributes.subject # => "Subject at 2022-01-01 (baseline time)"# work_package.subject # => "Subject at PT0S (current time)"#+# Visibility (permissions) of the work packages at the timestamps is checked with the following rules:+# * If the work package is visible currently, it is visible at all timestamps.+# * If the work package is not visible currently, visibility is checked at the individual timestamps.+# The reason for this discrepancy lies in the needs of the presentation layer. A client should be able to see+# the full history of a work package if it is currently visible.class Journable::WithHistoricAttributes < SimpleDelegatorattr_accessor :timestamps,:query,diff --git a/app/models/journable/with_historic_attributes/loader.rb b/app/models/journable/with_historic_attributes/loader.rbindex cbc24887068..73375f2d8c9 100644--- a/app/models/journable/with_historic_attributes/loader.rb+++ b/app/models/journable/with_historic_attributes/loader.rb@@ -38,7 +38,7 @@ class Journable::WithHistoricAttributesdef at_timestamp(timestamp)@at_timestamp ||= Hash.new do |h, t|- h[t] = journables.first.class.at_timestamp(t).where(id: journables.map(&:id)).index_by(&:id)+ h[t] = (currently_invisible_journalized_at_timestamp(t) + currently_visible_journalized_at_timestamp(t)).index_by(&:id)end@at_timestamp[timestamp]@@ -46,8 +46,8 @@ class Journable::WithHistoricAttributesdef work_package_ids_of_query_at_timestamp(query:, timestamp: nil)@work_package_ids_of_query_at_timestamp ||= Hash.new do |qh, q|- qh[q] = Hash.new do |ht, t|- ht[t] = work_package_ids_of_query_at_timestamp_calculation(q, t)+ qh[q] = Hash.new do |th, t|+ th[t] = work_package_ids_of_query_at_timestamp_calculation(q, t)endend@@ -63,6 +63,29 @@ class Journable::WithHistoricAttributesquery.results.work_packages.where(id: journables.map(&:id)).pluck(:id)end+ def currently_visible_journables+ @currently_visible_journables ||= begin+ visible_ids = journalized_class.visible.where(id: journables.map(&:id)).pluck(:id)+ journables.select { |j| visible_ids.include?(j.id) }+ end+ end++ def currently_invisible_journables+ @currently_invisible_journables ||= journables - currently_visible_journables+ end++ def currently_invisible_journalized_at_timestamp(timestamp)+ journalized_class.visible.at_timestamp(timestamp).where(id: currently_invisible_journables)+ end++ def currently_visible_journalized_at_timestamp(timestamp)+ journalized_class.at_timestamp(timestamp).where(id: currently_visible_journables)+ end++ def journalized_class+ journables.first.class+ end+attr_accessor :journablesendprivate_constant :Loaderdiff --git a/app/models/query/results.rb b/app/models/query/results.rbindex 836c345054b..99f613b461c 100644--- a/app/models/query/results.rb+++ b/app/models/query/results.rb@@ -46,8 +46,12 @@ class ::Query::Resultsendend+ private+def sorted_work_packages_matching_the_filters_today- sorted_work_packages.merge(filtered_work_packages)+ sorted_work_packages+ .visible+ .merge(filtered_work_packages)enddef sorted_work_packages_matching_the_filters_at_any_of_the_given_timestamps@@ -55,24 +59,16 @@ class ::Query::Results.where(id: work_packages_matching_the_filters_at_any_of_the_given_timestamps)end- def order_option- order_option = [group_by_sort].compact_blank.join(', ')-- if order_option.blank?- nil- else- Arel.sql(order_option)- end- end-- private-# For filtering on historic data, this returns the work packages# matching the filters for any of the timestamps provided in the query.- #+ # Visibility (permissions) are checked at all of the times. In combination with the `or`+ # concatenation that means that a user has to have no permission to see a work package+ # at any of the timestamps. This has to be used with care. Callers will have to+ # ensure to not reveal information.def work_packages_matching_the_filters_at_any_of_the_given_timestampsquery.timestamps.collect do |timestamp|- WorkPackage.where(id: filtered_work_packages.at_timestamp(timestamp))+ WorkPackage+ .where(id: filtered_work_packages.visible.at_timestamp(timestamp))end.reduce(:or)end@@ -97,9 +93,18 @@ class ::Query::Results.order(sort_criteria_array)end+ def order_option+ order_option = [group_by_sort].compact_blank.join(', ')++ if order_option.blank?+ nil+ else+ Arel.sql(order_option)+ end+ end+def work_package_scopeWorkPackage- .visible.merge(filter_merges).includes(all_includes).references(:projects)diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rbindex 34932874aee..5bf9bb65d26 100644--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb@@ -65,7 +65,7 @@ module API::V3::WorkPackages::EagerLoading# work package of the collection (the full path could then be e.g. _embedded/elements/5/_embedded/attributesAtTimestamp)## Attributes are:- # * those associated with the timestamp functionality, e.g. the whether the work package existed.+ # * those associated with the timestamp functionality, e.g. whether the work package existed.# * the timestamp the work package is at. Mostly necessary in case the work package did not exist at that time as a# stand-in work package is created in this case.# * stand-in blank values for custom values/fields which are currently not used and thus do not need to be loaded.@@ -114,6 +114,7 @@ module API::V3::WorkPackages::EagerLoadingwork_package.matches_filters_at_timestamp = source.matches_query_filters_at_timestamps.include?(timestamp)work_package.exists_at_timestamp = source.exists_at_timestamps.include?(timestamp)work_package.attributes_changed_to_baseline = source.changed_at_timestamp(timestamp)+ work_package.exists_at_current_timestamp = source.exists_at_timestamps.include?(source.current_timestamp)work_package.with_query = source.query.present?endend@@ -127,10 +128,12 @@ module API::V3::WorkPackages::EagerLoadingattr_writer :with_query,:exists_at_timestamp,- :matches_filters_at_timestamp+ :matches_filters_at_timestamp,+ :exists_at_current_timestampdef with_query?; @with_query; enddef exists_at_timestamp?; @exists_at_timestamp; end+ def exists_at_current_timestamp?; @exists_at_current_timestamp; enddef matches_filters_at_timestamp?; @matches_filters_at_timestamp; endenddiff --git a/lib/api/v3/work_packages/timestamped_representer.rb b/lib/api/v3/work_packages/timestamped_representer.rbnew file mode 100644index 00000000000..40898da4d97--- /dev/null+++ b/lib/api/v3/work_packages/timestamped_representer.rb@@ -0,0 +1,73 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++module API::V3::WorkPackages+ module TimestampedRepresenter+ extend ActiveSupport::Concern++ included do+ property :_meta,+ if: ->(*) {+ timestamps_active?+ },+ getter: ->(*) {+ {+ # This meta property states whether the work package exists at time.+ # https://github.com/opf/openproject/pull/11783#issuecomment-1374897874+ exists: represented.exists_at_timestamp?,++ # This meta property holds the timestamp of the data of the work package.+ #+ timestamp: timestamps.last.to_s,++ # This meta property states whether the attributes of the work package at the+ # timestamp match the filters of the query.+ # https://github.com/opf/openproject/pull/11783+ matchesFilters: represented.with_query? ? represented.matches_filters_at_timestamp? : nil+ }.compact+ },+ uncacheable: true,+ exec_context: :decorator++ property :attributes_by_timestamp,+ if: ->(*) {+ timestamps_active?+ },+ getter: ->(*) do+ represented.at_timestamps.map do |work_package_at_timestamp|+ API::V3::WorkPackages::WorkPackageAtTimestampRepresenter+ .create(work_package_at_timestamp,+ current_user:)+ end+ end,+ embedded: true,+ uncacheable: true,+ exec_context: :decorator+ end+ end+enddiff --git a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rbindex 153662404d6..918255b208c 100644--- a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb+++ b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb@@ -90,16 +90,20 @@ module API# This conversion is good enough for the set of supported properties as it# * Converts assigned_to_id to assignee# * does not mess with `start_date` and `due_date`- represented- .attributes_changed_to_baseline- .flat_map do |property|- if property.ends_with?('_id')- API::Utilities::PropertyNameConverter.from_ar_name(property)- elsif %w[start_date due_date].include?(property)- ['date', property]- else- property+ if represented.exists_at_current_timestamp?+ represented+ .attributes_changed_to_baseline+ .flat_map do |property|+ if property.ends_with?('_id')+ API::Utilities::PropertyNameConverter.from_ar_name(property)+ elsif %w[start_date due_date].include?(property)+ ['date', property]+ else+ property+ endend+ else+ SUPPORTED_PROPERTIESendendenddiff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rbindex 0a32fe6e8bc..a400a5521db 100644--- a/lib/api/v3/work_packages/work_package_collection_representer.rb+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb@@ -145,7 +145,15 @@ module APIrep_class = element_decorator.custom_field_class(all_fields)represented.map do |model|- rep_class.send(:new, model, current_user:, timestamps:, query:)+ # In case the work package is no longer visible (moved to a project the user+ # lacks permission in) we treat it as if the work package were deleted.+ representer = if model.visible?(current_user)+ rep_class+ else+ WorkPackageDeletedRepresenter+ end++ representer.send(:new, model, current_user:, timestamps:, query:)end},exec_context: :decorator,diff --git a/lib/api/v3/work_packages/work_package_deleted_representer.rb b/lib/api/v3/work_packages/work_package_deleted_representer.rbnew file mode 100644index 00000000000..3c605b8c799--- /dev/null+++ b/lib/api/v3/work_packages/work_package_deleted_representer.rb@@ -0,0 +1,62 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++module API+ module V3+ module WorkPackages+ class WorkPackageDeletedRepresenter < ::API::Decorators::Single+ include TimestampedRepresenter+ attr_accessor :timestamps, :query++ def initialize(model, current_user:, embed_links: false, timestamps: nil, query: nil)+ @query = query+ @timestamps = timestamps || query.try(:timestamps) || []++ super(model, current_user:, embed_links:)+ end++ def self_v3_path(*)+ api_v3_paths.work_package(represented.id, timestamps:)+ end++ self_link title_getter: ->(*) {}++ property :id,+ render_nil: true++ def _type+ 'WorkPackage'+ end++ def timestamps_active?+ timestamps.any?(&:historic?)+ end+ end+ end+ end+enddiff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex 5b304a3a109..59565ddeef9 100644--- a/lib/api/v3/work_packages/work_package_representer.rb+++ b/lib/api/v3/work_packages/work_package_representer.rb@@ -26,10 +26,6 @@# See COPYRIGHT and LICENSE files for more details.#++-# rubocop:disable Lint/SymbolConversion-# because some of the json attributes are written in 'singleQuotes' instead of symbols-# for better reading.-module APImodule V3module WorkPackages@@ -41,6 +37,7 @@ module APIinclude ::API::V3::Attachments::AttachableRepresenterMixininclude ::API::V3::FileLinks::FileLinkRelationRepresenterextend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass+ include TimestampedRepresentercached_representer key_parts: %i(project),disabled: false@@ -330,29 +327,6 @@ module APIendend- property :_meta,- if: ->(*) {- timestamps_active?- },- getter: ->(*) {- {- # This meta property states whether the work package exists at time.- # https://github.com/opf/openproject/pull/11783#issuecomment-1374897874- 'exists': represented.exists_at_timestamp?,-- # This meta property holds the timestamp of the data of the work package.- #- 'timestamp': timestamps.last.to_s,-- # This meta property states whether the attributes of the work package at the- # timestamp match the filters of the query.- # https://github.com/opf/openproject/pull/11783- 'matchesFilters': represented.with_query? ? represented.matches_filters_at_timestamp? : nil- }.compact- },- uncacheable: true,- exec_context: :decorator-property :id,render_nil: true@@ -474,21 +448,6 @@ module APIstatus_id && status.is_readonly?end- property :attributes_by_timestamp,- if: ->(*) {- timestamps_active?- },- getter: ->(*) do- represented.at_timestamps.map do |work_package_at_timestamp|- API::V3::WorkPackages::WorkPackageAtTimestampRepresenter- .create(work_package_at_timestamp,- current_user:)- end- end,- embedded: true,- uncacheable: true,- exec_context: :decorator-associated_resource :categoryassociated_resource :type@@ -708,5 +667,3 @@ module APIendendend--# rubocop:enable Lint/SymbolConversiondiff --git a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rbindex 3250e06d43c..a0cb67ba2f8 100644--- a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb@@ -89,6 +89,7 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' dlet(:exists_at_timestamp) { true }let(:with_query) { true }let(:matches_filters_at_timestamp) { true }+ let(:exists_at_current_timestamp) { true }let(:model) dowork_package.tap do |model|@@ -108,6 +109,9 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' dallow(model).to receive(:matches_filters_at_timestamp?).and_return(matches_filters_at_timestamp)+ allow(model)+ .to receive(:exists_at_current_timestamp?)+ .and_return(exists_at_current_timestamp)endend@@ -313,4 +317,61 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' d.to be_json_eql(expected_json)endend++ context 'with only one attribute changed to baseline but with the work package not existing (not visible) at current time' do+ let(:attributes_changed_to_baseline) { %w[start_date] }+ let(:exists_at_current_timestamp) { false }++ let(:expected_json) do+ {+ 'subject' => work_package.subject,+ 'startDate' => work_package.start_date,+ 'dueDate' => work_package.due_date,+ '_meta' => {+ 'matchesFilters' => true,+ 'exists' => true,+ 'timestamp' => timestamp.to_s+ },+ '_links' => {+ 'assignee' => {+ 'href' => api_v3_paths.user(assigned_to.id),+ 'title' => assigned_to.name+ },+ 'responsible' => {+ 'href' => api_v3_paths.user(responsible.id),+ 'title' => responsible.name+ },+ 'project' => {+ 'href' => api_v3_paths.project(project.id),+ 'title' => project.name+ },+ 'status' => {+ 'href' => api_v3_paths.status(status.id),+ 'title' => status.name+ },+ 'type' => {+ 'href' => api_v3_paths.type(type.id),+ 'title' => type.name+ },+ 'priority' => {+ 'href' => api_v3_paths.priority(priority.id),+ 'title' => priority.name+ },+ 'version' => {+ 'href' => api_v3_paths.version(version.id),+ 'title' => version.name+ },+ 'self' => {+ 'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),+ 'title' => work_package.subject+ }+ }+ }.to_json+ end++ it 'renders as expected' do+ expect(subject)+ .to be_json_eql(expected_json)+ end+ endenddiff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rbindex 90ddf5dfe03..085f5716735 100644--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb@@ -47,6 +47,8 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter dolet(:embed_schemas) { false }let(:timestamps) { nil }let(:query) { nil }+ # nil in this context means "don't care". The user will be allowed any action+ let(:permissions) { nil }let(:representer) dodescribed_class.new(@@ -66,10 +68,13 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter doendlet(:collection_inner_type) { 'WorkPackage' }+ current_user { user }+before doallow(user)- .to receive(:allowed_to?)- .and_return(true)+ .to receive(:allowed_to?) do |permission|+ permissions.nil? || permissions.include?(permission)+ endcreate_list(:work_package, total)end@@ -253,6 +258,29 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter doendend+ describe '_embedded' do+ describe 'elements' do+ context 'for a work package that is not visible' do+ let(:total) { 1 }+ let(:permissions) { [] }++ it 'renders a reduced WorkPackage element' do+ expect(collection)+ .to be_json_eql(+ {+ _type: 'WorkPackage',+ _links: {+ self: {+ href: api_v3_paths.work_package(work_packages.first.id)+ }+ }+ }.to_json+ ).at_path('_embedded/elements/0')+ end+ end+ end+ end+it 'does not render groups' doexpect(collection).not_to have_json_path('groups')end@@ -503,7 +531,6 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter doendcontext 'when passing timestamps' do- let(:work_pacakges) { WorkPackage.where(id: work_package.id) }let(:work_package) donew_work_package = create(:work_package, subject: "The current work package", project:)new_work_package.update_columns created_at: baseline_time - 1.day@@ -521,6 +548,13 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter doendlet(:baseline_time) { "2022-01-01".to_time }let(:project) { create(:project) }+ let(:current_user) do+ create(:user,+ firstname: 'user',+ lastname: '1',+ member_in_project: project,+ member_with_permissions: %i[view_work_packages])+ enddef create_journal(journable:, version:, timestamp:, attributes: {})work_package_attributes = work_package.attributes.except("id")@@ -620,20 +654,12 @@ describe API::V3::WorkPackages::WorkPackageCollectionRepresenter docontext 'when passing a query' dolet(:search_term) { 'original' }let(:query) do- login_as(current_user)build(:query, user: current_user, project: nil).tap do |query|query.filters.clearquery.add_filter 'subject', '~', search_termquery.timestamps = timestampsendend- let(:current_user) do- create(:user,- firstname: 'user',- lastname: '1',- member_in_project: project,- member_with_permissions: %i[view_work_packages view_file_links])- endcontext 'with baseline and current timestamps', with_flag: { show_changes: true } dolet(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }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.rbindex c825d9dea6f..aed7516b45a 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@@ -1329,6 +1329,14 @@ describe API::V3::WorkPackages::WorkPackageRepresenter doendlet(:project) { create(:project) }+ current_user do+ create(:user,+ firstname: 'user',+ lastname: '1',+ member_in_project: project,+ member_with_permissions: %i[view_work_packages view_file_links])+ end+def create_journal(journable:, version:, timestamp:, attributes: {})work_package_attributes = work_package.attributes.except("id")journal_attributes = work_package_attributes \@@ -1403,20 +1411,12 @@ describe API::V3::WorkPackages::WorkPackageRepresenter docontext 'when passing a query' dolet(:search_term) { 'original' }let(:query) do- login_as(current_user)build(:query, user: current_user, project: nil).tap do |query|query.filters.clearquery.add_filter 'subject', '~', search_termquery.timestamps = timestampsendend- let(:current_user) do- create(:user,- firstname: 'user',- lastname: '1',- member_in_project: project,- member_with_permissions: %i[view_work_packages view_file_links])- enddescribe 'attributesByTimestamp', with_flag: { show_changes: true } doit 'states whether the work package matches the query filters at the timestamp' dodiff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rbindex 32dcd7ca42a..25aff6350f1 100644--- a/spec/models/journable/with_historic_attributes_spec.rb+++ b/spec/models/journable/with_historic_attributes_spec.rb@@ -34,8 +34,11 @@ describe Journable::WithHistoricAttributes, with_flag: { show_changes: true } doshared_let(:baseline_time) { "2022-01-01".to_time }shared_let(:created_at) { baseline_time - 1.day }+ shared_let(:project) { create(:project) }shared_let(:work_package1) do- new_work_package = create(:work_package, subject: "The current work package 1")+ new_work_package = create(:work_package,+ subject: "The current work package 1",+ project:)new_work_package.update_columns(created_at:)new_work_packageend@@ -44,18 +47,18 @@ describe Journable::WithHistoricAttributes, with_flag: { show_changes: true } docreate_journal(journable: work_package1,timestamp: created_at,version: 1,- attributes: { subject: "The original work package 1" })+ attributes: { subject: "The original work package 1", project: })end- shared_let(:current_journal_wp2) do+ shared_let(:current_journal_wp1) docreate_journal(journable: work_package1,timestamp: 1.day.ago,version: 2,- attributes: { subject: "The current work package 1" })+ attributes: { subject: "The current work package 1", project: })endshared_let(:work_package2) donew_work_package = create(:work_package,- project: work_package1.project,+ project:,subject: "The current work package 2",start_date: created_at - 3.days)new_work_package.update_columns(created_at:)@@ -67,25 +70,26 @@ describe Journable::WithHistoricAttributes, with_flag: { show_changes: true } dotimestamp: created_at,version: 1,attributes: { start_date: created_at - 5.days,- subject: "The original work package 2" })+ subject: "The original work package 2",+ project: })endshared_let(:current_journal_wp2) docreate_journal(journable: work_package2,timestamp: 1.day.ago,version: 2,attributes: { start_date: created_at - 3.days,- subject: "The current work package 2" })+ subject: "The current work package 2",+ project: })endlet(:user1) docreate(:user,firstname: 'user',lastname: '1',- member_in_project: work_package1.project,+ member_in_project: project,member_with_permissions: %i[view_work_packages view_file_links])endlet(:build_query) do- login_as(user1)build(:query, user: nil, project: nil).tap do |query|query.filters.clearquery.add_filter 'subject', '~', search_term@@ -96,6 +100,8 @@ describe Journable::WithHistoricAttributes, with_flag: { show_changes: true } dolet(:query) { nil }let(:include_only_changed_attributes) { nil }+ current_user { user1 }+def create_journal(journable:, version:, timestamp:, attributes: {})work_package_attributes = journable.attributes.except("id")journal_attributes = work_package_attributes \@@ -316,6 +322,33 @@ describe Journable::WithHistoricAttributes, with_flag: { show_changes: true } doexpect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")end+ context 'with the work package not being visible currently' do+ let(:other_project) { create(:project) }++ before do+ current_journal_wp1.data.update_column(:project_id, other_project.id)+ work_package1.update_column(:project_id, other_project.id)+ end++ it "reports the work package to only exist in former times" do+ expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")+ expect(subject.exists_at_timestamps).not_to include Timestamp.parse("PT0S")+ end+ end++ context 'with the work package not having been visible before but being visible now' do+ let(:other_project) { create(:project) }++ before do+ original_journal_wp1.data.update_column(:project_id, other_project.id)+ end++ it "reports the work package to not exist at that point in time" do+ expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")+ expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")+ end+ end+describe "when providing a query" dolet(:query) { build_query }let(:search_term) { "original" }@@ -367,6 +400,69 @@ describe Journable::WithHistoricAttributes, with_flag: { show_changes: true } doendend+ describe '#at_timestamp' do+ context "with a single work package" do+ let(:work_packages) { work_package1 }++ it "returns the journable at a former time it existed with the attributes set to the former values" do+ expect(subject.at_timestamp(Timestamp.parse("2022-01-01T00:00:00Z")).attributes.slice('subject', 'id'))+ .to eq('subject' => "The original work package 1",+ 'id' => work_package1.id)+ end++ it "returns the journable at the current time" do+ expect(subject.at_timestamp(Timestamp.parse("PT0S")).attributes.slice('subject', 'id'))+ .to eq('subject' => "The current work package 1",+ 'id' => work_package1.id)+ end++ it "returns nil for a time it did not exist yet" do+ expect(subject.at_timestamp(Timestamp.parse("2021-01-01T00:00:00Z")))+ .to be_nil+ end++ context 'with the work package not being visible currently' do+ let(:other_project) { create(:project) }++ before do+ current_journal_wp1.data.update_column(:project_id, other_project.id)+ work_package1.update_column(:project_id, other_project.id)+ end++ it "returns the journable at a former time it existed with the attributes set to the former values" do+ expect(subject.at_timestamp(Timestamp.parse("2022-01-01T00:00:00Z")).attributes.slice('subject', 'id'))+ .to eq('subject' => "The original work package 1",+ 'id' => work_package1.id)+ end++ it "returns nil at the current time" do+ expect(subject.at_timestamp(Timestamp.parse("PT0S")))+ .to be_nil+ end+ end++ context 'with the work package not having been visible before but being visible now' do+ let(:other_project) { create(:project) }++ before do+ original_journal_wp1.data.update_column(:project_id, other_project.id)+ end++ it "returns the journable at a former time it existed (and was invisible) with the attributes set to the former values" do+ expect(subject.at_timestamp(Timestamp.parse("2022-01-01T00:00:00Z")).attributes.slice('subject', 'id'))+ .to eq('subject' => "The original work package 1",+ 'id' => work_package1.id)+ end++ it "returns the journable at the current time" do+ expect(subject.at_timestamp(Timestamp.parse("PT0S")).attributes.slice('subject', 'id'))+ .to eq('subject' => "The current work package 1",+ 'id' => work_package1.id)+ end+ end+ end+ end+describe '#historic?' docontext "with a single work package" dolet(:work_packages) { work_package1 }diff --git a/spec/models/query/results_filter_on_historic_data_spec.rb b/spec/models/query/results_filter_on_historic_data_spec.rbindex 86e8a4016da..4fbce5ab5bd 100644--- a/spec/models/query/results_filter_on_historic_data_spec.rb+++ b/spec/models/query/results_filter_on_historic_data_spec.rb@@ -36,8 +36,9 @@ describe Query::Results,let(:pre_historic_time) { historic_time - 1.day }let(:recent_time) { 1.hour.ago }let!(:work_package) do- new_work_package = create(:work_package, description: "This is the original description of the work package",- project: project1)+ new_work_package = create(:work_package,+ description: "This is the original description of the work package",+ project: project_with_member)new_work_package.update_columns created_at: historic_timenew_work_package.journals.first.update_columns created_at: historic_time, updated_at: historic_timenew_work_package.reload@@ -48,8 +49,9 @@ describe Query::Results,endlet!(:work_package2) do- new_work_package = create(:work_package, description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit",- project: project1)+ new_work_package = create(:work_package,+ description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit",+ project: project_with_member)new_work_package.update_columns created_at: historic_timenew_work_package.journals.first.update_columns created_at: historic_time, updated_at: historic_timenew_work_package.reload@@ -59,15 +61,23 @@ describe Query::Results,new_work_packageend- let(:project1) { create(:project) }+ let(:project_with_member) { create(:project) }+ let(:project_without_member) { create(:project) }+let(:user1) docreate(:user,firstname: 'user',lastname: '1',- member_in_project: project1,+ member_in_project: project_with_member,member_with_permissions: %i[view_work_packages view_file_links])end+ def move_work_package_to_project(work_package, project, time)+ work_package.update project:, updated_at: time+ work_package.journals.last.update_columns(created_at: time, updated_at: time)+ work_package.reload+ end+describe "[prelims]" dospecify "the work package has a journal entry with the historic description" doexpect(work_package.journals.count).to eq 2@@ -221,7 +231,7 @@ describe Query::Results,endendlet(:file_link1) { create(:file_link, creator: user1, container: work_package, storage: storage1) }- let(:project_storage1) { create(:project_storage, project: project1, storage: storage1) }+ let(:project_storage1) { create(:project_storage, project: project_with_member, storage: storage1) }before doproject_storage1@@ -238,8 +248,7 @@ describe Query::Results,describe "when having second reference to the same external file" dolet(:storage2) { create(:storage, creator: user1) }- let(:project_storage2) { create(:project_storage, project: project2, storage: storage2) }- let(:project2) { create(:project) }+ let(:project_storage2) { create(:project_storage, project: project_without_member, storage: storage2) }let(:file_link2) docreate(:file_link, creator: user1, container: work_package, storage: storage2, origin_id: file_link1.origin_id)end@@ -258,5 +267,90 @@ describe Query::Results,endendend++ context 'when the work package is moved to a project the user has no permissions in' do+ current_user { user1 }++ let(:query) do+ build(:query, user: user1, project: nil).tap do |query|+ query.filters.clear+ query.timestamps = [historic_time, Time.zone.now]+ end+ end++ before do+ move_work_package_to_project(work_package, project_without_member, 3.minutes.ago)+ end++ it "includes the work package (since the user was able to see it)" do+ expect(subject).to include work_package+ end+ end++ context 'when the work package is moved to a project the user has no permissions in ' \+ 'and also has no permission in the old project' do+ current_user { user1 }++ let(:query) do+ build(:query, user: user1, project: nil).tap do |query|+ query.filters.clear+ query.timestamps = [historic_time, Time.zone.now]+ end+ end++ before do+ move_work_package_to_project(work_package, project_without_member, 3.minutes.ago)+ project_with_member.members.destroy_all+ end++ it "does not include the work package" do+ expect(subject).not_to include work_package+ end+ end++ context 'when the work package is moved to a project the user has permissions in ' \+ 'and looses permission in the former project' do+ current_user { user1 }++ let(:query) do+ build(:query, user: user1, project: nil).tap do |query|+ query.filters.clear+ query.timestamps = [historic_time, Time.zone.now]+ end+ end++ before do+ move_work_package_to_project(work_package, project_without_member, 3.minutes.ago)+ create(:member,+ principal: user1,+ project: project_without_member,+ roles: [create(:role, permissions: %w[view_work_packages])])+ project_with_member.members.destroy_all+ end++ it "includes the work package" do+ expect(subject).to include work_package+ end+ end++ context 'when the work package is moved to a project the user has no permissions in' \+ 'and the comparison time is after the move' do+ current_user { user1 }++ let(:query) do+ build(:query, user: user1, project: nil).tap do |query|+ query.filters.clear+ query.timestamps = [2.minutes.ago, Time.zone.now]+ end+ end++ before do+ move_work_package_to_project(work_package, project_without_member, 3.minutes.ago)+ end++ it "does not include the work package since the user is not allowed to see the work package at the timestamps" do+ expect(subject).not_to include work_package+ end+ endendenddiff --git a/spec/requests/api/v3/work_packages/index_resource_spec.rb b/spec/requests/api/v3/work_packages/index_resource_spec.rbindex 48442feb039..09862d8e098 100644--- a/spec/requests/api/v3/work_packages/index_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/index_resource_spec.rb@@ -251,19 +251,26 @@ describe 'API v3 Work package resource',let(:created_at) { baseline_time - 1.day }let(:work_package) do- new_work_package = create(:work_package, subject: "The current work package", project:)+ new_work_package = create(:work_package,+ subject: "The current work package",+ assigned_to: current_user,+ project:)new_work_package.update_columns(created_at:)new_work_packageendlet(:original_journal) docreate_journal(journable: work_package, timestamp: created_at,version: 1,- attributes: { subject: "The original work package" })+ attributes: { subject: "The original work package",+ assigned_to: current_user,+ project: })endlet(:current_journal) docreate_journal(journable: work_package, timestamp: 1.day.ago,version: 2,- attributes: { subject: "The current work package" })+ attributes: { subject: "The current work package",+ assigned_to: current_user,+ project: })enddef create_journal(journable:, version:, timestamp:, attributes: {})@@ -356,7 +363,7 @@ describe 'API v3 Work package resource',enddescribe "when filtering such that the filters do not match at all timestamps" do- let(:path) { "#{api_v3_paths.path_for(:work_packages, filters:)}×tamps=#{timestamps_param}" }+ let(:path) { api_v3_paths.path_for(:work_packages, filters:, timestamps:) }let(:filters) do[{@@ -706,6 +713,191 @@ describe 'API v3 Work package resource',endend+ describe "when no longer being allowed to see the work package but used to in the past (moved to different project)" do+ let(:project2) { create(:project) }+ let(:work_packages) { [work_package] }++ before do+ work_package.update_column(:project_id, project2.id)+ current_journal.data.update_column(:project_id, project2.id)+ end++ it 'finds the work package' do+ expect(subject.body)+ .to be_json_eql(work_package.id.to_json)+ .at_path('_embedded/elements/0/id')+ end++ it "has no attributes in the main object" do+ expect(subject.body)+ .not_to have_json_path('_embedded/elements/0/subject')+ expect(subject.body)+ .not_to have_json_path('_embedded/elements/0/_links/project')+ end++ describe "_meta" do+ it 'marks the work package as not matching the filters' do+ expect(subject.body)+ .to be_json_eql(false.to_json)+ .at_path('_embedded/elements/0/_meta/matchesFilters')+ end++ it 'marks the work package as not existing today' do+ expect(subject.body)+ .to be_json_eql(false.to_json)+ .at_path('_embedded/elements/0/_meta/exists')+ end+ end++ describe "attributesByTimestamp/0 (baseline attributes)" do+ describe "_meta" do+ describe "matchesFilters" do+ it 'marks the work package as matching the filters at the baseline time' do+ expect(subject.body)+ .to be_json_eql(true.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/matchesFilters')+ end+ end++ describe "exists" do+ it 'marks the work package as existing at the baseline time' do+ expect(subject.body)+ .to be_json_eql(true.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/exists')+ end+ end+ end++ it "has all the supported attributes including those that did not change" do+ expect(subject.body)+ .to be_json_eql("The original work package".to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/subject')+ expect(subject.body)+ .to be_json_eql(project.name.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_links/project/title')+ expect(subject.body)+ .to be_json_eql(current_user.name.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_links/assignee/title')+ end+ end++ describe "attributesByTimestamp/1 (current attributes)" do+ describe "_meta" do+ describe "matchesFilters" do+ it 'marks the work package as not matching the filters today' do+ expect(subject.body)+ .to be_json_eql(false.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/matchesFilters')+ end+ end++ describe "exists" do+ it 'marks the work package as not existing today' do+ expect(subject.body)+ .to be_json_eql(false.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/exists')+ end+ end+ end+ end+ end++ describe "when now being allowed to see the work package but not in the past (moved to different project)" do+ let(:project2) { create(:project) }+ let(:work_packages) { [work_package] }++ before do+ # Move the old journal entry into another project where the user has no access+ original_journal.data.update_columns(project_id: project2.id)+ end++ it 'finds the work package' do+ expect(subject.body)+ .to be_json_eql(work_package.id.to_json)+ .at_path('_embedded/elements/0/id')+ end++ it "has the current attributes in the main object" do+ expect(subject.body)+ .to be_json_eql(work_package.subject.to_json)+ .at_path('_embedded/elements/0/subject')+ expect(subject.body)+ .to be_json_eql(api_v3_paths.project(project.id).to_json)+ .at_path('_embedded/elements/0/_links/project/href')+ end++ describe "_meta" do+ it 'marks the work package as matching the filters' do+ expect(subject.body)+ .to be_json_eql(true.to_json)+ .at_path('_embedded/elements/0/_meta/matchesFilters')+ end++ it 'marks the work package as existing today' do+ expect(subject.body)+ .to be_json_eql(true.to_json)+ .at_path('_embedded/elements/0/_meta/exists')+ end++ describe "timestamp" do+ it 'has the current timestamp, which is the second timestamp, in the same format as given in the request parameter' do+ expect(subject.body)+ .to be_json_eql("PT0S".to_json)+ .at_path('_embedded/elements/0/_meta/timestamp')+ end+ end+ end++ describe "attributesByTimestamp/0 (baseline attributes)" do+ describe "_meta" do+ describe "matchesFilters" do+ it 'marks the work package as not matching the filters at the baseline time' do+ expect(subject.body)+ .to be_json_eql(false.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/matchesFilters')+ end+ end++ describe "exists" do+ it 'marks the work package as existing at the baseline time' do+ expect(subject.body)+ .to be_json_eql(true.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/exists')+ end+ end+ end++ it "has all the supported attribute change" do+ expect(subject.body)+ .to be_json_eql("The original work package".to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/subject')+ expect(subject.body)+ .to be_json_eql(project2.name.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_links/project/title')+ end+ end++ describe "attributesByTimestamp/1 (current attributes)" do+ describe "_meta" do+ describe "matchesFilters" do+ it 'marks the work package as matching the filters today' do+ expect(subject.body)+ .to be_json_eql(true.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/matchesFilters')+ end+ end++ describe "exists" do+ it 'marks the work package as existing today' do+ expect(subject.body)+ .to be_json_eql(true.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/exists')+ end+ end+ end+ end+ end+describe "for multiple work packages" dolet!(:work_package2) donew_work_package = create(:work_package, subject: "Other work package", project:)