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

[#43692] Modify API to be able to handle file_links without a container. (#11977)

Pavel Balashouedc0ccb81451
diff --git a/modules/storages/app/contracts/storages/file_links/create_contract.rb b/modules/storages/app/contracts/storages/file_links/create_contract.rb
index 18466ecb648..ae8529dd6f7 100644
--- a/modules/storages/app/contracts/storages/file_links/create_contract.rb
+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb
@@ -61,9 +61,9 @@ class Storages::FileLinks::CreateContract < ModelContract
# Check that the current has the permission on the project.
# model variable is available because the concern is executed inside a contract.
def validate_user_allowed_to_manage
- unless user.allowed_to?(:manage_file_links, model.container&.project)
- errors.add :base, :error_unauthorized
- end
+ return if model.container.nil? || user.allowed_to?(:manage_file_links, model.project)
+
+ errors.add(:base, :error_unauthorized)
end
def validate_storage_presence
@@ -77,7 +77,7 @@ class Storages::FileLinks::CreateContract < ModelContract
def validate_project_storage_link
return if errors.include?(:storage)
- return if model.project.storages.include?(model.storage)
+ return if model.container.nil? || model.project.storages.include?(model.storage)
errors.add(:storage, :not_linked_to_project)
end
diff --git a/modules/storages/app/models/storages/file_link.rb b/modules/storages/app/models/storages/file_link.rb
index 03019893243..26154f0bb03 100644
--- a/modules/storages/app/models/storages/file_link.rb
+++ b/modules/storages/app/models/storages/file_link.rb
@@ -51,7 +51,7 @@ class Storages::FileLink < ApplicationRecord
# Currently only WorkPackages are supported as containers for FileLinks
# For some reason this case is not handled in the belongs_to above.
- validates :container_type, inclusion: { in: ["WorkPackage"] }
+ validates :container_type, inclusion: { in: ["WorkPackage", nil] }
# origin_id is the Nextcloud ID of the file and should be valid.
validates :origin_id, presence: true
diff --git a/modules/storages/app/workers/cleanup_uncontainered_file_links_job.rb b/modules/storages/app/workers/cleanup_uncontainered_file_links_job.rb
new file mode 100644
index 00000000000..96f27af0f2c
--- /dev/null
+++ b/modules/storages/app/workers/cleanup_uncontainered_file_links_job.rb
@@ -0,0 +1,40 @@
+#-- 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 CleanupUncontaineredFileLinksJob < Cron::CronJob
+ queue_with_priority :low
+
+ self.cron_expression = '06 22 * * *'
+
+ def perform
+ Storages::FileLink
+ .where(container: nil)
+ .where('created_at <= ?', Time.current - OpenProject::Configuration.attachments_grace_period.minutes)
+ .delete_all
+ end
+end
diff --git a/modules/storages/db/migrate/20230123092649_make_containter_id_and_containter_type_optional_for_file_links.rb b/modules/storages/db/migrate/20230123092649_make_containter_id_and_containter_type_optional_for_file_links.rb
new file mode 100644
index 00000000000..2d1fa08e91a
--- /dev/null
+++ b/modules/storages/db/migrate/20230123092649_make_containter_id_and_containter_type_optional_for_file_links.rb
@@ -0,0 +1,34 @@
+#-- 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 MakeContainterIdAndContainterTypeOptionalForFileLinks < ActiveRecord::Migration[7.0]
+ def change
+ change_column_null :file_links, :container_id, true
+ change_column_null :file_links, :container_type, true
+ end
+end
diff --git a/modules/storages/lib/api/v3/file_links/create_endpoint.rb b/modules/storages/lib/api/v3/file_links/create_endpoint.rb
index de979ab6f01..dbd8dd82b44 100644
--- a/modules/storages/lib/api/v3/file_links/create_endpoint.rb
+++ b/modules/storages/lib/api/v3/file_links/create_endpoint.rb
@@ -65,18 +65,14 @@ class API::V3::FileLinks::CreateEndpoint < API::Utilities::Endpoints::Create
def present_success(request, service_call)
render_representer.create(
service_call.all_results,
- self_link: request.api_v3_paths.file_links(request.work_package.id),
+ self_link: self_link(request),
current_user: request.current_user
)
end
private
- def params_modifier
- ->(params) do
- params[:container_id] = work_package.id
- params[:container_type] = work_package.class.name
- params
- end
+ def self_link(_request)
+ "#{::API::V3::URN_PREFIX}file_links:no_link_provided"
end
end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index bef27f2f2b5..f39fee65c9f 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -135,7 +135,8 @@ module API::V3::FileLinks
associated_resource :container,
v3_path: :work_package,
- representer: ::API::V3::WorkPackages::WorkPackageRepresenter
+ representer: ::API::V3::WorkPackages::WorkPackageRepresenter,
+ skip_render: ->(*) { represented.container_id.nil? }
def _type
'FileLink'
@@ -144,7 +145,7 @@ module API::V3::FileLinks
private
def user_allowed_to_manage?(model)
- current_user.allowed_to?(:manage_file_links, model.container.project)
+ model.container.present? && current_user.allowed_to?(:manage_file_links, model.project)
end
def make_origin_data(model)
diff --git a/modules/storages/lib/api/v3/file_links/file_links_api.rb b/modules/storages/lib/api/v3/file_links/file_links_api.rb
index 493300447fc..dc93572d51d 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_api.rb
@@ -29,13 +29,21 @@
# This class provides definitions for API routes and endpoints for the file_links namespace. It inherits the
# functionality from the Grape REST API framework. It is mounted in lib/api/v3/root.rb.
# -> /api/v3/file_links/...
-class API::V3::FileLinks::FileLinksAPI < ::API::OpenProjectAPI
+class API::V3::FileLinks::FileLinksAPI < API::OpenProjectAPI
# helpers is defined by the grape framework. They make methods from the
# module available from within the endpoint context.
helpers Storages::Peripherals::Scopes
# The `:resources` keyword defines the API namespace -> /api/v3/file_links/...
resources :file_links do
+ post &::API::V3::FileLinks::CreateEndpoint
+ .new(
+ model: ::Storages::FileLink,
+ parse_service: Storages::Peripherals::ParseCreateParamsService,
+ render_representer: ::API::V3::FileLinks::FileLinkCollectionRepresenter
+ )
+ .mount
+
# `route_param` extends the route by a route parameter of the endpoint.
# The input parameter value is parsed into the `:file_link_id` symbol.
route_param :file_link_id, type: Integer, desc: 'File link id' do
diff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
index 33f63ad9686..159e73479d9 100644
--- a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
@@ -30,7 +30,7 @@
# functionality from the Grape REST API framework. It is mounted in lib/api/v3/work_packages/work_packages_api.rb,
# which puts the file_links namespace behind the provided namespace of the work packages api
# -> /api/v3/work_packages/:id/file_links/...
-class API::V3::FileLinks::WorkPackagesFileLinksAPI < ::API::OpenProjectAPI
+class API::V3::FileLinks::WorkPackagesFileLinksAPI < API::OpenProjectAPI
# helpers is defined by the grape framework. They make methods from the
# module available from within the endpoint context.
helpers Storages::Peripherals::Scopes
@@ -69,11 +69,16 @@ class API::V3::FileLinks::WorkPackagesFileLinksAPI < ::API::OpenProjectAPI
)
end
- post &::API::V3::FileLinks::CreateEndpoint
+ post &::API::V3::FileLinks::WorkPackagesFileLinksCreateEndpoint
.new(
model: ::Storages::FileLink,
parse_service: Storages::Peripherals::ParseCreateParamsService,
- render_representer: ::API::V3::FileLinks::FileLinkCollectionRepresenter
+ render_representer: ::API::V3::FileLinks::FileLinkCollectionRepresenter,
+ params_modifier: ->(params) do
+ params[:container_id] = work_package.id
+ params[:container_type] = work_package.class.name
+ params
+ end
)
.mount
end
diff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_create_endpoint.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_create_endpoint.rb
new file mode 100644
index 00000000000..9343fcea533
--- /dev/null
+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_create_endpoint.rb
@@ -0,0 +1,35 @@
+#-- 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 API::V3::FileLinks::WorkPackagesFileLinksCreateEndpoint < API::V3::FileLinks::CreateEndpoint
+ private
+
+ def self_link(request)
+ request.api_v3_paths.file_links(request.work_package.id)
+ end
+end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index d5518166089..20c40110dd0 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -162,5 +162,7 @@ module OpenProject::Storages
add_api_endpoint 'API::V3::WorkPackages::WorkPackagesAPI', :id do
mount ::API::V3::FileLinks::WorkPackagesFileLinksAPI
end
+
+ add_cron_jobs { CleanupUncontaineredFileLinksJob }
end
end
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 4fd70fbe001..8bbbaf2aa6e 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
@@ -91,6 +91,76 @@ describe 'API v3 file links resource' do
login_as current_user
end
+ describe 'POST /api/v3/file_links' do
+ let(:path) { '/api/v3/file_links' }
+ let(:permissions) { %i(manage_file_links) }
+ let(:storage_url) { storage.host }
+ let(:params) do
+ {
+ _type: "Collection",
+ _embedded: {
+ elements: embedded_elements
+ }
+ }
+ end
+ let(:embedded_elements) do
+ [
+ {
+ originData: {
+ id: 5503,
+ name: "logo.png",
+ mimeType: "image/png",
+ createdAt: "2021-12-19T09:42:10.170Z",
+ lastModifiedAt: "2021-12-20T14:00:13.987Z",
+ createdByName: "Luke Skywalker",
+ lastModifiedByName: "Anakin Skywalker"
+ },
+ _links: {
+ storageUrl: {
+ href: storage_url
+ }
+ }
+ },
+ build(:file_link_element, storage_url:)
+ ]
+ end
+
+ before do
+ header 'Content-Type', 'application/json'
+ post path, params.to_json
+ end
+
+ context 'when all embedded file link elements are valid' do
+ it_behaves_like 'API V3 collection response', 2, 2, 'FileLink' do
+ let(:elements) { Storages::FileLink.all.order(id: :asc) }
+ let(:expected_status_code) { 201 }
+ end
+
+ it 'creates corresponding FileLink records', :aggregate_failures do
+ expect(Storages::FileLink.count).to eq 2
+ Storages::FileLink.find_each.with_index do |file_link, i|
+ unset_keys = %w[container_id container_type]
+ set_keys = (file_link.attributes.keys - unset_keys)
+ set_keys.each do |key|
+ expect(file_link.attributes[key]).not_to(
+ be_nil,
+ "expected attribute #{key.inspect} of FileLink ##{i + 1} to be set.\ngot nil."
+ )
+ end
+ unset_keys.each do |key|
+ expect(file_link.attributes[key]).to be_nil
+ end
+ end
+ end
+
+ it 'does not provide a link to the collection of created file links' do
+ expect(response.body).to be_json_eql(
+ 'urn:openproject-org:api:v3:file_links:no_link_provided'.to_json
+ ).at_path('_links/self/href')
+ end
+ end
+ end
+
describe 'GET /api/v3/work_packages/:work_package_id/file_links' do
let(:path) { api_v3_paths.file_links(work_package.id) }
@@ -214,6 +284,10 @@ describe 'API v3 file links resource' do
end
end
end
+
+ it 'provides a link to the collection of created file links' do
+ expect(response.body).to be_json_eql(path.to_json).at_path('_links/self/href')
+ end
end
context 'when some embedded file link elements are NOT valid' do
diff --git a/modules/storages/spec/workers/cleanup_uncontainered_file_links_job_spec.rb b/modules/storages/spec/workers/cleanup_uncontainered_file_links_job_spec.rb
new file mode 100644
index 00000000000..5ae569ec547
--- /dev/null
+++ b/modules/storages/spec/workers/cleanup_uncontainered_file_links_job_spec.rb
@@ -0,0 +1,67 @@
+#-- 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.
+#++
+
+require 'spec_helper'
+
+describe CleanupUncontaineredFileLinksJob, type: :job do
+ it 'has a schedule set' do
+ expect(described_class.cron_expression).to eq('06 22 * * *')
+ end
+
+ it 'removes uncontainered file_links which are old enough' do
+ grace_period = 10
+ allow(OpenProject::Configuration)
+ .to receive(:attachments_grace_period)
+ .and_return(grace_period)
+
+ expect(Storages::FileLink.count).to eq(0)
+
+ uncontainered_old = create(:file_link,
+ container_id: nil,
+ container_type: nil,
+ created_at: Time.current - grace_period.minutes - 1.second)
+ uncontainered_young = create(:file_link,
+ container_id: nil,
+ container_type: nil)
+ containered_old = create(:file_link,
+ container_id: 1,
+ created_at: Time.current - grace_period.minutes - 1.second)
+ containered_young = create(:file_link,
+ container_id: 1)
+
+ expect(Storages::FileLink.count).to eq(4)
+
+ described_class.new.perform
+
+ expect(Storages::FileLink.count).to eq(3)
+ file_link_ids = Storages::FileLink.pluck(:id).sort
+ expected_file_link_ids = [uncontainered_young.id, containered_old.id, containered_young.id].sort
+ expect(file_link_ids).to eq(expected_file_link_ids)
+ expect(file_link_ids).not_to include(uncontainered_old.id)
+ end
+end