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

Set work_packages--file_links association to be polymorphic.

Pavel Balashov007a9a58f357
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index a8ca34007da..e637d2767e4 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -57,8 +57,8 @@ class WorkPackage < ApplicationRecord
has_many :time_entries, dependent: :delete_all
- has_many :file_links,
- dependent: :delete_all, class_name: 'Storages::FileLink', foreign_key: 'container_id', inverse_of: :container
+ has_many :file_links, dependent: :delete_all, class_name: 'Storages::FileLink', as: :container
+
has_many :storages, through: :project
has_and_belongs_to_many :changesets, -> { # rubocop:disable Rails/HasAndBelongsToMany
@@ -310,8 +310,7 @@ class WorkPackage < ApplicationRecord
def type_id=(tid)
self.type = nil
- result = write_attribute(:type_id, tid)
- result
+ write_attribute(:type_id, tid)
end
# Overrides attributes= so that type_id gets assigned first
@@ -478,7 +477,7 @@ class WorkPackage < ApplicationRecord
key = 'activity_id'
id = attributes[key]
default_id = if id&.present?
- Enumeration.exists? id: id, is_default: true, type: 'TimeEntryActivity'
+ Enumeration.exists? id:, is_default: true, type: 'TimeEntryActivity'
else
true
end
diff --git a/app/services/work_packages/create_service.rb b/app/services/work_packages/create_service.rb
index 629051c95d9..ef5650b73d9 100644
--- a/app/services/work_packages/create_service.rb
+++ b/app/services/work_packages/create_service.rb
@@ -26,7 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class WorkPackages::CreateService < ::BaseServices::BaseCallable
+class WorkPackages::CreateService < BaseServices::BaseCallable
include ::WorkPackages::Shared::UpdateAncestors
include ::Shared::ServiceContext
@@ -51,11 +51,13 @@ class WorkPackages::CreateService < ::BaseServices::BaseCallable
def create(attributes, work_package)
result = set_attributes(attributes, work_package)
- result.success = if result.success
- replace_attachments(work_package)
- else
- false
- end
+ result.success =
+ if result.success
+ work_package.attachments = work_package.attachments_replacements if work_package.attachments_replacements
+ work_package.save
+ else
+ false
+ end
if result.success?
result.merge!(reschedule_related(work_package))
@@ -80,16 +82,10 @@ class WorkPackages::CreateService < ::BaseServices::BaseCallable
.call(attributes)
end
- def replace_attachments(work_package)
- work_package.attachments = work_package.attachments_replacements if work_package.attachments_replacements
- work_package.save
- end
-
def reschedule_related(work_package)
result = WorkPackages::SetScheduleService
- .new(user:,
- work_package:)
- .call
+ .new(user:, work_package:)
+ .call
result.self_and_dependent.each do |r|
unless r.result.save
diff --git a/modules/storages/app/common/storages/peripherals/scopes.rb b/modules/storages/app/common/storages/peripherals/scopes.rb
index 42450b7ca27..c41598c780e 100644
--- a/modules/storages/app/common/storages/peripherals/scopes.rb
+++ b/modules/storages/app/common/storages/peripherals/scopes.rb
@@ -31,9 +31,5 @@ module Storages::Peripherals
def visible_storages
::Storages::Storage.visible(current_user)
end
-
- def visible_file_links
- ::Storages::FileLink.visible(current_user)
- end
end
end
diff --git a/modules/storages/app/models/storages/file_link.rb b/modules/storages/app/models/storages/file_link.rb
index 26154f0bb03..383630d686b 100644
--- a/modules/storages/app/models/storages/file_link.rb
+++ b/modules/storages/app/models/storages/file_link.rb
@@ -47,7 +47,7 @@ class Storages::FileLink < ApplicationRecord
# FileLinks are attached to a container ()currently a WorkPackage)
# Wieland: This needs to become more flexible in the future
- belongs_to :container, class_name: 'WorkPackage'
+ belongs_to :container, polymorphic: true
# Currently only WorkPackages are supported as containers for FileLinks
# For some reason this case is not handled in the belongs_to above.
@@ -64,20 +64,6 @@ class Storages::FileLink < ApplicationRecord
@origin_permission || nil
end
- # A standard Rails custom query:
- # https://www.rubyguides.com/2019/10/scopes-in-ruby-on-rails/
- # Purpose: limit to FileLink visible by given user.
- # Used by: FileLinksAPI#visible_file_links and WorkPackagesFileLinksAPI#visible_file_links
- scope :visible, ->(user = User.current) {
- # join projects through the container, and filter on projects visible from
- # the user
- includes(:container)
- .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
def name
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 dc93572d51d..c242f4409f5 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
@@ -30,10 +30,6 @@
# 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
- # 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
@@ -50,7 +46,20 @@ class API::V3::FileLinks::FileLinksAPI < API::OpenProjectAPI
# The after validation hook executes after the validation of the request format, but before any execution
# inside the endpoint context. Hence, it is a good place to actually fetch the handled resource.
after_validation do
- @file_link = visible_file_links.find(params[:file_link_id])
+ @file_link = begin
+ file_link = Storages::FileLink
+ .where(id: params[:file_link_id])
+ .where("container_id IS NOT NULL AND container_type IS NOT NULL")
+ .first
+
+ if file_link.present? &&
+ current_user.allowed_to?(:view_file_links, file_link.project) &&
+ file_link.project.storage_ids.include?(file_link.storage_id)
+ file_link
+ else
+ raise ::API::Errors::NotFound.new
+ end
+ end
end
# A helper is used to define the behaviour at GET /api/v3/file_links/:file_link_id
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 159e73479d9..9ec106c6ef9 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
@@ -31,10 +31,6 @@
# 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
- # 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/work_packages/:id/file_links/...
resources :file_links do
# Get the list of FileLinks related to a work package, with updated information from Nextcloud.
@@ -51,19 +47,22 @@ class API::V3::FileLinks::WorkPackagesFileLinksAPI < API::OpenProjectAPI
raise ::API::Errors::InvalidQuery.new(message)
end
- # Get a (potentially huge...) list of all FileLinks for the work package.
- file_links = query.results
- .where(id: visible_file_links
- .where(container_id: @work_package.id, container_type: 'WorkPackage'))
-
- # Synchronize with Nextcloud. StorageAPI has handled OAuth2 for us before.
- # We ignore the result, because partial errors (storage network issues) are written to each FileLink
- service_result = ::Storages::FileLinkSyncService
- .new(user: current_user)
- .call(file_links)
-
+ result = if current_user.allowed_to?(:view_file_links, @work_package.project)
+ # Get a (potentially huge...) list of all FileLinks for the work package.
+ file_links = query.results.where(container_id: @work_package.id,
+ container_type: 'WorkPackage',
+ storage: @work_package.project.storages)
+ # Synchronize with Nextcloud. StorageAPI has handled OAuth2 for us before.
+ # We ignore the result, because partial errors (storage network issues) are written to each FileLink
+ ::Storages::FileLinkSyncService
+ .new(user: current_user)
+ .call(file_links)
+ .result
+ else
+ []
+ end
::API::V3::FileLinks::FileLinkCollectionRepresenter.new(
- service_result.result,
+ result,
self_link: api_v3_paths.file_links(@work_package.id),
current_user:
)
diff --git a/spec/requests/api/v3/work_packages/create_resource_spec.rb b/spec/requests/api/v3/work_packages/create_resource_spec.rb
index 3fd9a20baef..094f7dc7b56 100644
--- a/spec/requests/api/v3/work_packages/create_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/create_resource_spec.rb
@@ -307,6 +307,7 @@ describe 'API v3 Work package resource',
expect(WorkPackage.count).to eq(1)
work_package = WorkPackage.first
expect(work_package.file_links).to eq([file_link])
+ expect(work_package.file_links.first.container_type).to eq('WorkPackage')
expect(last_response.body).to be_json_eql(
api_v3_paths.file_links(work_package.id).to_json
).at_path('_links/fileLinks/href')