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

[#47049] Extend storage representer to have a list of project folders.

Andreas Pfohlb945db4e43be
diff --git a/modules/storages/app/models/queries/storages/project_storages/filter/project_id_filter.rb b/modules/storages/app/models/queries/storages/project_storages/filter/project_id_filter.rb
new file mode 100644
index 00000000000..dc68fe2e7e4
--- /dev/null
+++ b/modules/storages/app/models/queries/storages/project_storages/filter/project_id_filter.rb
@@ -0,0 +1,41 @@
+#-- 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::ProjectStorages::Filter
+ class ProjectIdFilter < ::Queries::Filters::Base
+ self.model = Storages::ProjectStorage
+
+ def type
+ :list
+ end
+
+ def allowed_values
+ Storages::ProjectStorage.pluck(:project_id).map { |id| [id, id.to_s] }
+ end
+ end
+end
diff --git a/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb b/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb
new file mode 100644
index 00000000000..0f0955631c8
--- /dev/null
+++ b/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb
@@ -0,0 +1,41 @@
+#-- 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::ProjectStorages::Filter
+ class StorageIdFilter < ::Queries::Filters::Base
+ self.model = Storages::ProjectStorage
+
+ def type
+ :list
+ end
+
+ def allowed_values
+ Storages::ProjectStorage.pluck(:storage_id).map { |id| [id, id.to_s] }
+ end
+ end
+end
diff --git a/modules/storages/app/models/queries/storages/project_storages/project_storages_query.rb b/modules/storages/app/models/queries/storages/project_storages/project_storages_query.rb
new file mode 100644
index 00000000000..c4aa3f99b40
--- /dev/null
+++ b/modules/storages/app/models/queries/storages/project_storages/project_storages_query.rb
@@ -0,0 +1,33 @@
+#-- 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.
+#++
+
+class Queries::Storages::ProjectStorages::ProjectStoragesQuery < Queries::BaseQuery
+ def self.model
+ @model ||= ::Storages::ProjectStorage
+ end
+end
diff --git a/modules/storages/lib/api/v3/project_storages/project_storage_collection_representer.rb b/modules/storages/lib/api/v3/project_storages/project_storage_collection_representer.rb
new file mode 100644
index 00000000000..5b0bcf0fee6
--- /dev/null
+++ b/modules/storages/lib/api/v3/project_storages/project_storage_collection_representer.rb
@@ -0,0 +1,36 @@
+#-- 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 API
+ module V3
+ module ProjectStorages
+ class ProjectStorageCollectionRepresenter < ::API::Decorators::UnpaginatedCollection
+ end
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/project_storages/project_storage_representer.rb b/modules/storages/lib/api/v3/project_storages/project_storage_representer.rb
new file mode 100644
index 00000000000..eebb4459405
--- /dev/null
+++ b/modules/storages/lib/api/v3/project_storages/project_storage_representer.rb
@@ -0,0 +1,51 @@
+#-- 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 API::V3::ProjectStorages
+ class ProjectStorageRepresenter < ::API::Decorators::Single
+ include API::Decorators::DateProperty
+ include API::Decorators::LinkedResource
+
+ self_link(title: false)
+
+ property :id
+ date_time_property :created_at
+ date_time_property :updated_at
+ property :project_folder_id
+ property :project_folder_mode
+
+ associated_resource :storage, skip_render: ->(*) { true }, skip_link: ->(*) { false }
+ associated_resource :project, skip_render: ->(*) { true }, skip_link: ->(*) { false }
+ associated_resource :creator, v3_path: :user, representer: ::API::V3::Users::UserRepresenter,
+ skip_render: ->(*) { true }, skip_link: ->(*) { false }
+
+ def _type
+ 'ProjectStorage'
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/project_storages/project_storages_api.rb b/modules/storages/lib/api/v3/project_storages/project_storages_api.rb
new file mode 100644
index 00000000000..1c48de45e87
--- /dev/null
+++ b/modules/storages/lib/api/v3/project_storages/project_storages_api.rb
@@ -0,0 +1,64 @@
+#-- 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 API::V3::ProjectStorages
+ class ProjectStoragesAPI < API::OpenProjectAPI
+ helpers Storages::Peripherals::Scopes
+
+ resources :project_storages do
+ get do
+ query = ParamsToQueryService
+ .new(Storages::ProjectStorage,
+ current_user,
+ query_class: Queries::Storages::ProjectStorages::ProjectStoragesQuery)
+ .call(params)
+
+ unless query.valid?
+ message = I18n.t('api_v3.errors.missing_or_malformed_parameter')
+ raise ::API::Errors::InvalidQuery.new(message)
+ end
+
+ results = query.results.filter { |result| current_user.allowed_to?(:view_file_links, result.project) }
+
+ ::API::V3::ProjectStorages::ProjectStorageCollectionRepresenter.new(
+ results,
+ self_link: api_v3_paths.project_storages,
+ current_user:
+ )
+ end
+
+ route_param :id, type: Integer, desc: 'ProjectStorage id' do
+ after_validation do
+ @project_storage = Storages::ProjectStorage.find(params[:id])
+ end
+
+ get &API::V3::Utilities::Endpoints::Show.new(model: Storages::ProjectStorage).mount
+ end
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rb
index 820a44c4a89..585a414647f 100644
--- a/modules/storages/lib/api/v3/storages/storage_representer.rb
+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb
@@ -180,10 +180,22 @@ module API::V3::Storages
private
+ def storage_projects(storage)
+ storage.projects.merge(Project.allowed_to(current_user, :manage_file_links))
+ end
+
+ def project_folders(storage)
+ storage_projects(storage).map(&:projects_storages).flatten.map do |ps|
+ {
+ project_id: ps.project_id,
+ project_folder_id: ps.project_folder_id,
+ project_folder_mode: ps.project_folder_mode
+ }
+ end
+ end
+
def storage_projects_ids(storage)
- storage.projects
- .merge(Project.allowed_to(current_user, :manage_file_links))
- .pluck(:id)
+ storage_projects(storage).pluck(:id)
end
def authorization_state
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 779d1eceae8..0ded92628e7 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -129,6 +129,11 @@ module OpenProject::Storages
::Queries::Register.register(::Queries::Storages::FileLinks::FileLinkQuery) do
filter ::Queries::Storages::FileLinks::Filter::StorageFilter
end
+
+ ::Queries::Register.register(::Queries::Storages::ProjectStorages::ProjectStoragesQuery) do
+ filter ::Queries::Storages::ProjectStorages::Filter::StorageIdFilter
+ filter ::Queries::Storages::ProjectStorages::Filter::ProjectIdFilter
+ end
end
end
@@ -138,6 +143,14 @@ module OpenProject::Storages
"#{root}/storages"
end
+ add_api_path :project_storages do
+ "#{root}/project_storages"
+ end
+
+ add_api_path :project_storage do |id|
+ "#{root}/project_storages/#{id}"
+ end
+
add_api_path :storage do |storage_id|
"#{storages}/#{storage_id}"
end
@@ -173,6 +186,7 @@ module OpenProject::Storages
# Add api endpoints specific to this module
add_api_endpoint 'API::V3::Root' do
mount ::API::V3::Storages::StoragesAPI
+ mount ::API::V3::ProjectStorages::ProjectStoragesAPI
mount ::API::V3::FileLinks::FileLinksAPI
end