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

[#50234] added permission check to storage filter

Eric Schubert4ee72dac08c0
diff --git a/modules/storages/app/models/queries/storages/projects/filter/storage_filter_mixin.rb b/modules/storages/app/models/queries/storages/projects/filter/storage_filter_mixin.rb
new file mode 100644
index 00000000000..63aace951ad
--- /dev/null
+++ b/modules/storages/app/models/queries/storages/projects/filter/storage_filter_mixin.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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 Queries::Storages::Projects::Filter::StorageFilterMixin
+ def type
+ :list
+ end
+
+ def allowed_values
+ values.map { |value| [nil, value] }
+ end
+
+ def joins
+ [:storages]
+ end
+
+ def where
+ <<-SQL.squish
+ #{operator_strategy.sql_for_field(where_values, Storages::Storage.table_name, filter_column)}
+ AND projects.id IN (#{Project.allowed_to(User.current, :view_file_links).select(:id).to_sql})
+ SQL
+ end
+
+ private
+
+ def where_values
+ values
+ end
+
+ def filter_column
+ raise NotImplementedError
+ end
+end
diff --git a/modules/storages/app/models/queries/storages/projects/filter/storage_id_filter.rb b/modules/storages/app/models/queries/storages/projects/filter/storage_id_filter.rb
index 5dcf38145f6..43d3b1d29d9 100644
--- a/modules/storages/app/models/queries/storages/projects/filter/storage_id_filter.rb
+++ b/modules/storages/app/models/queries/storages/projects/filter/storage_id_filter.rb
@@ -29,20 +29,12 @@
#++
module Queries::Storages::Projects::Filter
class StorageIdFilter < ::Queries::Projects::Filters::ProjectFilter
- def type
- :list
- end
-
- def allowed_values
- values.map { |value| [nil, value] }
- end
+ include StorageFilterMixin
- def joins
- [:storages]
- end
+ private
- def where
- operator_strategy.sql_for_field(values, Storages::Storage.table_name, 'id')
+ def filter_column
+ 'id'
end
end
end
diff --git a/modules/storages/app/models/queries/storages/projects/filter/storage_url_filter.rb b/modules/storages/app/models/queries/storages/projects/filter/storage_url_filter.rb
index 595a3d19dc1..45a3e1710bc 100644
--- a/modules/storages/app/models/queries/storages/projects/filter/storage_url_filter.rb
+++ b/modules/storages/app/models/queries/storages/projects/filter/storage_url_filter.rb
@@ -29,26 +29,16 @@
#++
module Queries::Storages::Projects::Filter
class StorageUrlFilter < ::Queries::Projects::Filters::ProjectFilter
- def type
- :list
- end
-
- def allowed_values
- values.map { |value| [nil, value] }
- end
+ include StorageFilterMixin
- def joins
- [:storages]
- end
+ private
- def where
- operator_strategy.sql_for_field(unescape_hosts(values), Storages::Storage.table_name, 'host')
+ def filter_column
+ 'host'
end
- private
-
- def unescape_hosts(hosts)
- hosts.map { |host| CGI.unescape(host).gsub(/\/+$/, '') }
+ def where_values
+ values.map { |host| CGI.unescape(host).gsub(/\/+$/, '') }
end
end
end
diff --git a/modules/storages/spec/requests/api/v3/projects/projects_storages_filter_spec.rb b/modules/storages/spec/requests/api/v3/projects/projects_storages_filter_spec.rb
index 4162fcd05e5..1147f2c349c 100644
--- a/modules/storages/spec/requests/api/v3/projects/projects_storages_filter_spec.rb
+++ b/modules/storages/spec/requests/api/v3/projects/projects_storages_filter_spec.rb
@@ -44,7 +44,7 @@ RSpec.describe 'API v3 projects resource with filters for the linked storages',
end
shared_let(:user) { create(:user) }
- shared_let(:role) { create(:role, permissions: %i[view_work_packages]) }
+ shared_let(:role) { create(:role, permissions: %i[view_work_packages view_file_links]) }
shared_let(:project1) { create(:project, members: { user => role }) }
shared_let(:project2) { create(:project, members: { user => role }) }