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

have a delete service for attachments

ulfertsa895767619a5
diff --git a/app/contracts/attachments/delete_contract.rb b/app/contracts/attachments/delete_contract.rb
new file mode 100644
index 00000000000..59a27356d3c
--- /dev/null
+++ b/app/contracts/attachments/delete_contract.rb
@@ -0,0 +1,37 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Attachments
+ class DeleteContract < ::DeleteContract
+ delete_permission -> {
+ model.deletable?(user)
+ }
+ end
+end
diff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rb
index e7135a956f6..c4715c027fc 100644
--- a/app/services/attachments/create_service.rb
+++ b/app/services/attachments/create_service.rb
@@ -40,21 +40,24 @@ class Attachments::CreateService
#
# An ActiveRecord::RecordInvalid error is raised if any record can't be saved.
def call(uploaded_file:, description:)
- if JournalManager.journalized?(container)
- save_journalized(uploaded_file, description)
+ if container.nil?
+ create_attachment(uploaded_file, description)
+ elsif JournalManager.journalized?(container)
+ create_journalized(uploaded_file, description)
else
- save_unjournalized(uploaded_file, description)
+ create_unjournalized(uploaded_file, description)
end
end
private
- def save_journalized(uploaded_file, description)
+ def create_journalized(uploaded_file, description)
OpenProject::Mutex.with_advisory_lock_transaction(container) do
+ attachment = create_attachment(uploaded_file, description)
# Get the latest attachments to ensure having them all for journalization.
- # A different worker might have added attachments in the meantime, e.g when bulk uploading
+ # We just created an attachment and a different worker might have added attachments
+ # in the meantime, e.g when bulk uploading.
container.attachments.reload
- attachment = build_attachment(uploaded_file, description)
add_journal
save_container
@@ -63,8 +66,8 @@ class Attachments::CreateService
end
end
- def save_unjournalized(uploaded_file, description)
- build_attachment(uploaded_file, description).tap do
+ def create_unjournalized(uploaded_file, description)
+ create_attachment(uploaded_file, description).tap do
save_container
end
end
@@ -73,6 +76,16 @@ class Attachments::CreateService
container.add_journal author
end
+ def create_attachment(uploaded_file, description)
+ attachment = Attachment.new(file: uploaded_file,
+ container: container,
+ description: description,
+ author: author)
+
+ attachment.save!
+ attachment
+ end
+
def build_attachment(uploaded_file, description)
container.attachments.build(file: uploaded_file, description: description, author: author)
end
diff --git a/app/services/attachments/delete_service.rb b/app/services/attachments/delete_service.rb
new file mode 100644
index 00000000000..38c4fa489ae
--- /dev/null
+++ b/app/services/attachments/delete_service.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+class Attachments::DeleteService < ::BaseServices::Delete
+ def call(params = nil)
+ in_context(model.container || model) do
+ perform(params)
+ end
+ end
+
+ private
+
+ def destroy(attachment)
+ if attachment.container
+ attachment.container.attachments.delete(attachment)
+ else
+ super
+ end
+ end
+end
diff --git a/app/services/base_services/delete.rb b/app/services/base_services/delete.rb
index 184d66bc4c4..ccdd8c06935 100644
--- a/app/services/base_services/delete.rb
+++ b/app/services/base_services/delete.rb
@@ -38,7 +38,7 @@ module BaseServices
def persist(service_result)
service_result = super(service_result)
- unless service_result.result.destroy
+ unless destroy(service_result.result)
service_result.errors = service_result.result.errors
service_result.success = false
end
@@ -46,6 +46,10 @@ module BaseServices
service_result
end
+ def destroy(object)
+ object.destroy
+ end
+
protected
def default_contract_class
diff --git a/app/services/shared/service_context.rb b/app/services/shared/service_context.rb
index 1e8cdb85186..e7d5b367566 100644
--- a/app/services/shared/service_context.rb
+++ b/app/services/shared/service_context.rb
@@ -32,7 +32,7 @@ module Shared
module ServiceContext
private
- def in_context(model, send_notifications, &block)
+ def in_context(model, send_notifications = true, &block)
if model
in_mutex_context(model, send_notifications, &block)
else
@@ -40,13 +40,13 @@ module Shared
end
end
- def in_mutex_context(model, send_notifications, &block)
+ def in_mutex_context(model, send_notifications = true, &block)
OpenProject::Mutex.with_advisory_lock_transaction(model) do
in_user_context(send_notifications, &block)
end
end
- def in_user_context(send_notifications)
+ def in_user_context(send_notifications = true)
result = nil
ActiveRecord::Base.transaction do
diff --git a/lib/api/v3/attachments/attachments_api.rb b/lib/api/v3/attachments/attachments_api.rb
index 44485ebb8e9..3e0fbf7add6 100644
--- a/lib/api/v3/attachments/attachments_api.rb
+++ b/lib/api/v3/attachments/attachments_api.rb
@@ -59,17 +59,7 @@ module API
AttachmentRepresenter.new(@attachment, embed_links: true, current_user: current_user)
end
- delete do
- raise API::Errors::Unauthorized unless @attachment.deletable?(current_user)
-
- if @attachment.container
- @attachment.container.attachments.delete(@attachment)
- else
- @attachment.destroy
- end
-
- status 204
- end
+ delete &::API::V3::Utilities::Endpoints::Delete.new(model: Attachment).mount
namespace :content do
helpers ::API::Helpers::AttachmentRenderer
diff --git a/spec/contracts/time_entries/delete_contract_spec.rb b/spec/contracts/time_entries/delete_contract_spec.rb
index c9e4e9ae2a8..e2efff47bd7 100644
--- a/spec/contracts/time_entries/delete_contract_spec.rb
+++ b/spec/contracts/time_entries/delete_contract_spec.rb
@@ -54,13 +54,13 @@ describe TimeEntries::DeleteContract do
let(:time_entry) do
FactoryBot.build_stubbed(:time_entry,
- project: time_entry_project,
- work_package: time_entry_work_package,
- user: time_entry_user,
- activity: time_entry_activity,
- spent_on: time_entry_spent_on,
- hours: time_entry_hours,
- comments: time_entry_comments)
+ project: time_entry_project,
+ work_package: time_entry_work_package,
+ user: time_entry_user,
+ activity: time_entry_activity,
+ spent_on: time_entry_spent_on,
+ hours: time_entry_hours,
+ comments: time_entry_comments)
end
before do
diff --git a/spec/services/attachments/create_service_spec.rb b/spec/services/attachments/create_service_spec.rb
index 9ccc80f74f0..adb1e188c15 100644
--- a/spec/services/attachments/create_service_spec.rb
+++ b/spec/services/attachments/create_service_spec.rb
@@ -34,7 +34,7 @@ describe Attachments::CreateService do
let(:container) { work_package }
let(:description) { 'a fancy description' }
- subject { described_class.new(work_package, author: user) }
+ subject { described_class.new(container, author: user) }
describe '#call' do
def call_tested_method
@@ -50,12 +50,12 @@ describe Attachments::CreateService do
end
it 'adds the attachment to the WP' do
- work_package.reload
- expect(work_package.attachments).to include Attachment.first
+ container.reload
+ expect(container.attachments).to include Attachment.first
end
it 'adds a journal entry on the WP' do
- expect(work_package.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment
+ expect(container.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment
end
end
@@ -78,6 +78,20 @@ describe Attachments::CreateService do
it_behaves_like 'successful creation'
end
+ context "uncontainered" do
+ let(:container) { nil }
+
+ before do
+ call_tested_method
+ end
+
+ it 'saves the attachment' do
+ attachment = Attachment.first
+ expect(attachment.filename).to eq 'foobar.txt'
+ expect(attachment.description).to eq description
+ end
+ end
+
context 'invalid attachment', with_settings: { attachment_max_size: 0 } do
it 'raises the exception' do
expect { call_tested_method }