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

[bugfix] Added additional visibility check for file links

Eric Schubertfa150d44e165
diff --git a/modules/storages/app/models/queries/storages/work_packages/filter/file_link_origin_id_filter.rb b/modules/storages/app/models/queries/storages/work_packages/filter/file_link_origin_id_filter.rb
index 2578997a6b2..d1a3c182135 100644
--- a/modules/storages/app/models/queries/storages/work_packages/filter/file_link_origin_id_filter.rb
+++ b/modules/storages/app/models/queries/storages/work_packages/filter/file_link_origin_id_filter.rb
@@ -41,5 +41,13 @@ module Queries::Storages::WorkPackages::Filter
def permission
:view_file_links
end
+
+ def joins
+ %i[file_links storages]
+ end
+
+ def additional_where_condition
+ "AND #{::Storages::FileLink.table_name}.storage_id = #{::Storages::Storage.table_name}.id"
+ end
end
end
diff --git a/modules/storages/app/models/queries/storages/work_packages/filter/storage_id_filter.rb b/modules/storages/app/models/queries/storages/work_packages/filter/storage_id_filter.rb
index b3a24593cfc..f0733a41b77 100644
--- a/modules/storages/app/models/queries/storages/work_packages/filter/storage_id_filter.rb
+++ b/modules/storages/app/models/queries/storages/work_packages/filter/storage_id_filter.rb
@@ -41,5 +41,13 @@ module Queries::Storages::WorkPackages::Filter
def permission
:view_file_links
end
+
+ def joins
+ [:file_links, { project: :projects_storages }]
+ end
+
+ def additional_where_condition
+ "AND #{::Storages::FileLink.table_name}.storage_id = #{::Storages::ProjectStorage.table_name}.storage_id"
+ end
end
end
diff --git a/modules/storages/app/models/queries/storages/work_packages/filter/storage_url_filter.rb b/modules/storages/app/models/queries/storages/work_packages/filter/storage_url_filter.rb
index 56de183c698..12b2c464192 100644
--- a/modules/storages/app/models/queries/storages/work_packages/filter/storage_url_filter.rb
+++ b/modules/storages/app/models/queries/storages/work_packages/filter/storage_url_filter.rb
@@ -47,7 +47,11 @@ module Queries::Storages::WorkPackages::Filter
end
def joins
- { file_links: :storage }
+ [{ file_links: :storage }, { project: :projects_storages }]
+ end
+
+ def additional_where_condition
+ "AND #{::Storages::FileLink.table_name}.storage_id = #{::Storages::ProjectStorage.table_name}.storage_id"
end
end
end
diff --git a/modules/storages/app/models/queries/storages/work_packages/filter/storages_filter_mixin.rb b/modules/storages/app/models/queries/storages/work_packages/filter/storages_filter_mixin.rb
index 69b26ca71eb..7bffb8123e3 100644
--- a/modules/storages/app/models/queries/storages/work_packages/filter/storages_filter_mixin.rb
+++ b/modules/storages/app/models/queries/storages/work_packages/filter/storages_filter_mixin.rb
@@ -55,6 +55,7 @@ module Queries::Storages::WorkPackages::Filter::StoragesFilterMixin
<<-SQL.squish
#{::Queries::Operators::Equals.sql_for_field(where_values, filter_model.table_name, filter_column)}
AND work_packages.project_id IN (#{Project.allowed_to(User.current, permission).select(:id).to_sql})
+ #{additional_where_condition}
SQL
end
@@ -62,6 +63,10 @@ module Queries::Storages::WorkPackages::Filter::StoragesFilterMixin
values
end
+ def additional_where_condition
+ ''
+ end
+
def joins
filter_model.table_name.to_sym
end
diff --git a/modules/storages/app/models/storages/file_link.rb b/modules/storages/app/models/storages/file_link.rb
index d411fc70911..79317938a9f 100644
--- a/modules/storages/app/models/storages/file_link.rb
+++ b/modules/storages/app/models/storages/file_link.rb
@@ -64,9 +64,10 @@ class Storages::FileLink < ApplicationRecord
# join projects through the container, and filter on projects visible from
# the user
includes(:container)
- .includes(container: :project)
+ .includes(container: { project: :projects_storages })
.references(:projects)
.merge(Project.allowed_to(user, :view_file_links))
+ .where('projects_storages.storage_id = file_links.storage_id')
}
delegate :project, to: :container
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index 119b6dbca13..17a0ab2617f 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -50,9 +50,14 @@ describe 'API v3 file links resource', type: :request do
let(:file_link) do
create(:file_link, creator: current_user, container: work_package, storage: storage)
end
- let(:another_file_link) do
+ let(:file_link_of_other_work_package) do
create(:file_link, creator: current_user, container: another_work_package, storage: storage)
end
+ # If a storage mapping between a project and a storage is removed, the file link still persist. This can occur on
+ # moving a work package to another project, too, if target project does not yet have the storage mapping.
+ let(:file_link_of_unlinked_storage) do
+ create(:file_link, creator: current_user, container: work_package, storage: another_storage)
+ end
subject(:response) { last_response }
@@ -65,7 +70,8 @@ describe 'API v3 file links resource', type: :request do
before do
file_link
- another_file_link
+ file_link_of_other_work_package
+ file_link_of_unlinked_storage
get path
end
@@ -312,11 +318,17 @@ describe 'API v3 file links resource', type: :request do
it_behaves_like 'not found'
end
- context 'if no storage with that id exists' do
+ context 'if no file link with that id exists' do
let(:path) { api_v3_paths.file_link(1337) }
it_behaves_like 'not found'
end
+
+ context 'if file link is in a work package, while its project is not mapped to the file link\'s storage.' do
+ let(:path) { api_v3_paths.file_link(file_link_of_unlinked_storage.id) }
+
+ it_behaves_like 'not found'
+ end
end
describe 'DELETE /api/v3/file_links/:file_link_id' do
diff --git a/modules/storages/spec/requests/api/v3/work_packages/work_packages_linked_filter_spec.rb b/modules/storages/spec/requests/api/v3/work_packages/work_packages_linked_filter_spec.rb
index df8992c633b..b3577eb61ca 100644
--- a/modules/storages/spec/requests/api/v3/work_packages/work_packages_linked_filter_spec.rb
+++ b/modules/storages/spec/requests/api/v3/work_packages/work_packages_linked_filter_spec.rb
@@ -63,6 +63,9 @@ describe 'API v3 work packages resource with filters for linked storage file',
storage: storage2,
origin_id: file_link1.origin_id)
end
+ # This link is considered invisible, as it is linking a work package to a file, where the work package's project
+ # and the file's storage are not linked together.
+ let(:file_link4) { create(:file_link, creator: current_user, container: work_package3, storage: storage1) }
subject(:response) { last_response }
@@ -73,6 +76,7 @@ describe 'API v3 work packages resource with filters for linked storage file',
file_link1
file_link2
file_link3
+ file_link4
login_as current_user
end
@@ -116,6 +120,14 @@ describe 'API v3 work packages resource with filters for linked storage file',
let(:elements) { [] }
end
end
+
+ context 'if the filter is set to a file linked to a work package in an unlinked project' do
+ let(:origin_id_value) { file_link4.origin_id.to_s }
+
+ it_behaves_like 'API V3 collection response', 0, 0, 'WorkPackage', 'WorkPackageCollection' do
+ let(:elements) { [] }
+ end
+ end
end
context 'with single filter for storage id' do
@@ -169,6 +181,14 @@ describe 'API v3 work packages resource with filters for linked storage file',
let(:elements) { [work_package3] }
end
+ context 'if any of the matching work packages is in a project without a mapping to that storage' do
+ let(:storage_url_value) { CGI.escape(storage1.host) }
+
+ it_behaves_like 'API V3 collection response', 2, 2, 'WorkPackage', 'WorkPackageCollection' do
+ let(:elements) { [work_package1, work_package2] }
+ end
+ end
+
context 'if one project has not sufficient permissions' do
let(:role2) { create :role, permissions: %i(view_work_packages) }