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

[#40228] implement get file link endpoint

Eric Schubert3495ae9c85b5
diff --git a/docs/api/apiv3/components/schemas/storage_model.yml b/docs/api/apiv3/components/schemas/storage_model.yml
index bc8278908f2..cb861594b78 100644
--- a/docs/api/apiv3/components/schemas/storage_model.yml
+++ b/docs/api/apiv3/components/schemas/storage_model.yml
@@ -29,6 +29,7 @@ properties:
type: object
required:
- self
+ - type
- href
properties:
self:
diff --git a/modules/storages/app/models/storages/file_link.rb b/modules/storages/app/models/storages/file_link.rb
index 3ee8b4cf195..92c4b1bf022 100644
--- a/modules/storages/app/models/storages/file_link.rb
+++ b/modules/storages/app/models/storages/file_link.rb
@@ -1,8 +1,6 @@
-#-- encoding: UTF-8
-
#-- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
+# Copyright (C) 2012-2022 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.
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index f43a0943d9c..b40b749146a 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -1,5 +1,3 @@
-#-- encoding: UTF-8
-
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
@@ -41,4 +39,8 @@ class Storages::Storage < ApplicationRecord
validates_length_of :host, minimum: 1, maximum: 255
validates_uniqueness_of :name
validates_length_of :name, minimum: 1, maximum: 255
+
+ def visible_to?(user)
+ user.allowed_to_globally?(:view_file_links)
+ 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
new file mode 100644
index 00000000000..cdfc651842b
--- /dev/null
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -0,0 +1,109 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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 FileLinks
+ class FileLinkRepresenter < ::API::Decorators::Single
+ include API::Decorators::LinkedResource
+ include API::Decorators::DateProperty
+
+ property :id
+
+ date_time_property :created_at
+
+ date_time_property :updated_at
+
+ property :originData,
+ exec_context: :decorator,
+ getter: ->(*) { make_origin_data(represented) }
+
+ link :self do
+ {
+ href: api_v3_paths.file_link(represented.container_id, represented.id),
+ method: :get
+ }
+ end
+
+ link :delete do
+ {
+ href: api_v3_paths.file_link(represented.container_id, represented.id),
+ method: :delete
+ }
+ end
+
+ link :creator do
+ {
+ href: api_v3_paths.user(represented.creator_id),
+ method: :get
+ }
+ end
+
+ link :staticDownloadLocation do
+ {
+ href: api_v3_paths.file_link_download(represented.container_id, represented.id),
+ method: :get
+ }
+ end
+
+ link :staticOpenLocation do
+ {
+ href: api_v3_paths.file_link_open(represented.container_id, represented.id),
+ method: :get
+ }
+ end
+
+ # TODO: add download and open location
+
+ associated_resource :storage
+
+ associated_resource :container,
+ v3_path: :work_package,
+ representer: ::API::V3::WorkPackages::WorkPackageRepresenter
+
+ def _type
+ 'FileLink'
+ end
+
+ private
+
+ def make_origin_data(model)
+ {
+ id: model.origin_id,
+ name: model.origin_name,
+ mimeType: model.origin_mime_type,
+ createdAt: datetime_formatter.format_datetime(model.origin_created_at, allow_nil: true),
+ lastModifiedAt: datetime_formatter.format_datetime(model.origin_updated_at, allow_nil: true),
+ createdByName: model.origin_created_by_name,
+ lastModifiedByName: model.origin_last_modified_by_name
+ }
+ end
+ end
+ end
+ end
+end
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 610e92cca9b..21f7fa591b9 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
@@ -40,10 +40,12 @@ module API
end
route_param :file_link_id, type: Integer, desc: 'File link id' do
- get do
- raise ::API::Errors::NotImplemented
+ after_validation do
+ @file_link = ::Storages::FileLink.find(params[:file_link_id])
end
+ get &::API::V3::Utilities::Endpoints::Show.new(model: ::Storages::FileLink).mount
+
delete do
raise ::API::Errors::NotImplemented
end
diff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rb
new file mode 100644
index 00000000000..b90229394c7
--- /dev/null
+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb
@@ -0,0 +1,70 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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 Storages
+ class StorageRepresenter < ::API::Decorators::Single
+ include API::Decorators::LinkedResource
+ include API::Decorators::DateProperty
+
+ property :id
+
+ property :name
+
+ date_time_property :created_at
+
+ date_time_property :updated_at
+
+ link :self do
+ {
+ href: api_v3_paths.storage(represented.id),
+ method: :get
+ }
+ end
+
+ link :type do
+ {
+ href: "#{::API::V3::URN_PREFIX}storages:nextcloud",
+ title: 'Nextcloud'
+ }
+ end
+
+ link :origin do
+ {
+ href: represented.host
+ }
+ end
+
+ def _type
+ 'Storage'
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/storages/storages_api.rb b/modules/storages/lib/api/v3/storages/storages_api.rb
index d2235b8f339..34da8cbdac8 100644
--- a/modules/storages/lib/api/v3/storages/storages_api.rb
+++ b/modules/storages/lib/api/v3/storages/storages_api.rb
@@ -32,9 +32,13 @@ module API
class StoragesAPI < ::API::OpenProjectAPI
resources :storages do
route_param :storage_id, type: Integer, desc: 'Storage id' do
- get do
- raise ::API::Errors::NotImplemented
+ after_validation do
+ @storage = ::Storages::Storage.find(params[:storage_id])
+
+ raise ::API::Errors::NotFound unless @storage.visible_to?(current_user)
end
+
+ get &::API::V3::Utilities::Endpoints::Show.new(model: ::Storages::Storage).mount
end
end
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index d3b34aaa62c..fab28eb2909 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -64,7 +64,7 @@ module OpenProject::Storages
parent: :settings
end
- add_api_path :storages do |storage_id|
+ add_api_path :storage do |storage_id|
"#{root}/storages/#{storage_id}"
end
diff --git a/modules/storages/spec/factories/file_link_factory.rb b/modules/storages/spec/factories/file_link_factory.rb
new file mode 100644
index 00000000000..743367d8ee4
--- /dev/null
+++ b/modules/storages/spec/factories/file_link_factory.rb
@@ -0,0 +1,33 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+FactoryBot.define do
+ factory :file_link, class: '::Storages::FileLink' do
+ container_type { 'WorkPackage' }
+ end
+end
diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rb
new file mode 100644
index 00000000000..5dea328ccb9
--- /dev/null
+++ b/modules/storages/spec/factories/storage_factory.rb
@@ -0,0 +1,35 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+FactoryBot.define do
+ factory :storage, class: '::Storages::Storage' do
+ name { 'TestStorage' }
+ provider_type { 'nextcloud' }
+ host { 'https://nextcloud.test-storage.org/' }
+ 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 f82507de67d..9f564a69649 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
@@ -32,15 +32,22 @@ describe 'API v3 file links resource', type: :request do
include API::V3::Utilities::PathHelper
let(:permissions) { %i(view_work_packages) }
- let(:role) { FactoryBot.create(:role, permissions: permissions) }
- let(:project) { FactoryBot.create(:project) }
+ let(:project) { create(:project) }
let(:current_user) do
- FactoryBot.create(:user, member_in_project: project, member_through_role: role)
+ create(:user, member_in_project: project, member_with_permissions: permissions)
end
let(:work_package) do
- FactoryBot.create :work_package, author: current_user, project: project
+ create(:work_package, author: current_user, project: project)
+ end
+
+ let(:storage) do
+ create(:storage, creator: current_user)
+ end
+
+ let(:file_link) do
+ create(:file_link, creator: current_user, container: work_package, storage: storage)
end
subject(:response) { last_response }
@@ -57,7 +64,7 @@ describe 'API v3 file links resource', type: :request do
end
it 'returns not implemented' do
- expect(subject.status).to eql 501
+ expect(subject.status).to be 501
end
end
@@ -70,24 +77,24 @@ describe 'API v3 file links resource', type: :request do
end
it 'returns not implemented' do
- expect(subject.status).to eql 501
+ expect(subject.status).to be 501
end
end
describe 'GET /api/v3/work_packages/:work_package_id/file_links/:file_link_id' do
- let(:path) { api_v3_paths.file_link(work_package.id, 1337) }
+ let(:path) { api_v3_paths.file_link(work_package.id, file_link.id) }
before do
get path
end
- it 'returns not implemented' do
- expect(subject.status).to eql 501
+ it 'is successful' do
+ expect(subject.status).to be 200
end
end
describe 'DELETE /api/v3/work_packages/:work_package_id/file_links/:file_link_id' do
- let(:path) { api_v3_paths.file_link(work_package.id, 1337) }
+ let(:path) { api_v3_paths.file_link(work_package.id, file_link.id) }
before do
header 'Content-Type', 'application/json'
@@ -95,31 +102,31 @@ describe 'API v3 file links resource', type: :request do
end
it 'returns not implemented' do
- expect(subject.status).to eql 501
+ expect(subject.status).to be 501
end
end
describe 'GET /api/v3/work_packages/:work_package_id/file_links/:file_link_id/download' do
- let(:path) { api_v3_paths.file_link_download(work_package.id, 1337) }
+ let(:path) { api_v3_paths.file_link_download(work_package.id, file_link.id) }
before do
get path
end
it 'returns not implemented' do
- expect(subject.status).to eql 501
+ expect(subject.status).to be 501
end
end
describe 'GET /api/v3/work_packages/:work_package_id/file_links/:file_link_id/open' do
- let(:path) { api_v3_paths.file_link_open(work_package.id, 1337) }
+ let(:path) { api_v3_paths.file_link_open(work_package.id, file_link.id) }
before do
get path
end
it 'returns not implemented' do
- expect(subject.status).to eql 501
+ expect(subject.status).to be 501
end
end
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
index 379dc750791..ed995efa821 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -28,15 +28,18 @@
require 'spec_helper'
-describe 'API v3 storages resource', type: :request do
+describe 'API v3 storages resource', type: :request, content_type: :json do
include API::V3::Utilities::PathHelper
let(:permissions) { %i(view_file_links) }
- let(:role) { FactoryBot.create(:role, permissions: permissions) }
- let(:project) { FactoryBot.create(:project) }
+ let(:project) { create(:project) }
let(:current_user) do
- FactoryBot.create(:user, member_in_project: project, member_through_role: role)
+ create(:user, member_in_project: project, member_with_permissions: permissions)
+ end
+
+ let(:storage) do
+ create(:storage, creator: current_user)
end
subject(:response) { last_response }
@@ -46,14 +49,26 @@ describe 'API v3 storages resource', type: :request do
end
describe 'GET /api/v3/storages/:storage_id' do
- let(:path) { api_v3_paths.storages(1337) }
+ let(:path) { api_v3_paths.storage(storage.id) }
before do
get path
end
- it 'returns not implemented' do
- expect(subject.status).to eql 501
+ it 'is successful' do
+ expect(subject.status).to be 200
+ end
+
+ context 'if user has no permissions' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'not found'
+ end
+
+ context 'if no storage with that id exists' do
+ let(:path) { api_v3_paths.storage(1337) }
+
+ it_behaves_like 'not found'
end
end
end