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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Quarantined admin view
Oliver Günther9c856db8520e
diff --git a/app/components/admin/quarantined_attachments/row_component.rb b/app/components/admin/quarantined_attachments/row_component.rbnew file mode 100644index 00000000000..2ebb8999149--- /dev/null+++ b/app/components/admin/quarantined_attachments/row_component.rb@@ -0,0 +1,72 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 Admin+ module QuarantinedAttachments+ class RowComponent < ::RowComponent+ def attachment+ model+ end++ def row_css_id+ "quarantined_attachment_#{attachment.id}"+ end++ def filename+ attachment.filename+ end++ def container+ "#{attachment.container_type} ##{attachment.container_id}"+ end++ def author+ render Users::AvatarComponent.new(user: attachment.author, size: :mini, link: true, show_name: true)+ end++ def created_at+ helpers.format_time attachment.created_at+ end++ def button_links+ [delete_link]+ end++ def delete_link+ helpers.link_to(+ helpers.op_icon('icon icon-delete'),+ { controller: '/admin/attachments/quarantined_attachments', action: :destroy, id: model },+ method: :delete,+ data: { confirm: I18n.t(:text_are_you_sure), disable_with: I18n.t(:label_loading) },+ )+ end+ end+ end+enddiff --git a/app/components/admin/quarantined_attachments/table_component.rb b/app/components/admin/quarantined_attachments/table_component.rbnew file mode 100644index 00000000000..5a84873ddea--- /dev/null+++ b/app/components/admin/quarantined_attachments/table_component.rb@@ -0,0 +1,55 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 Admin+ module QuarantinedAttachments+ class TableComponent < ::TableComponent+ columns :filename, :container, :author, :created_at+ options :current_user++ def initial_sort+ %i[id desc]+ end++ def sortable?+ false+ end++ def headers+ [+ ['filename', { caption: Attachment.human_attribute_name(:filename) }],+ ['container', { caption: I18n.t('antivirus_scan.quarantined_attachments.container') }],+ ['author', { caption: Attachment.human_attribute_name(:author) }],+ ['created_at', { caption: Attachment.human_attribute_name(:created_at) }]+ ]+ end+ end+ end+enddiff --git a/app/controllers/admin/attachments/quarantined_attachments_controller.rb b/app/controllers/admin/attachments/quarantined_attachments_controller.rbnew file mode 100644index 00000000000..74ba9f5c177--- /dev/null+++ b/app/controllers/admin/attachments/quarantined_attachments_controller.rb@@ -0,0 +1,79 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 Admin+ module Attachments+ class QuarantinedAttachmentsController < ApplicationController+ layout 'admin'+ before_action :require_admin+ before_action :find_quarantined_attachments++ menu_item :attachment_quarantine++ def index; end++ def destroy+ attachment = @attachments.find(params[:id])+ container = attachment.container++ attachment.destroy!+ ::Journals::CreateService+ .new(container, User.system)+ .call(notes: I18n.t('antivirus_scan.deleted_by_admin', filename: attachment.filename))++ flash[:notice] = t(:notice_successful_delete)+ redirect_to action: :index+ end++ def default_breadcrumb+ t('antivirus_scan.quarantined_attachments.title')+ end++ private++ def find_quarantined_attachments+ @attachments = Attachment+ .status_quarantined+ .includes(:author)+ end++ def find_attachment+ @attachment = @attachments.find(params[:id])+ rescue ActiveRecord::RecordNotFound+ render_404+ end++ def prevent_same_user+ if current_user == @attachment.author+ flash[:error] = t('antivirus_scan.quarantined_attachments.error_cannot_act_self')+ redirect_to action: :index+ end+ end+ end+ end+enddiff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rbindex e8c169e20b3..fca3e2517d7 100644--- a/app/helpers/settings_helper.rb+++ b/app/helpers/settings_helper.rb@@ -49,11 +49,6 @@ module SettingsHelpercontroller: '/admin/settings/projects_settings',label: :label_project_plural},- {- name: 'attachments',- controller: '/admin/settings/attachments_settings',- label: :'attributes.attachments'- },{name: 'repositories',controller: '/admin/settings/repositories_settings',diff --git a/app/models/attachment.rb b/app/models/attachment.rbindex 6eb641f39c4..9f5db90bbc8 100644--- a/app/models/attachment.rb+++ b/app/models/attachment.rb@@ -69,7 +69,7 @@ class Attachment < ApplicationRecordmount_uploader :file, OpenProject::Configuration.file_uploader- after_commit :enqueue_jobs, on: :create+ after_commit :enqueue_jobs, on: :create, if: -> { !internal_container? }scope :pending_direct_upload, -> { where(digest: "", downloads: -1) }scope :not_pending_direct_upload, -> { where.not(digest: "", downloads: -1) }diff --git a/app/views/admin/attachments/quarantined_attachments/index.html.erb b/app/views/admin/attachments/quarantined_attachments/index.html.erbnew file mode 100644index 00000000000..65c0cdd7579--- /dev/null+++ b/app/views/admin/attachments/quarantined_attachments/index.html.erb@@ -0,0 +1,9 @@+<% html_title t('antivirus_scan.quarantined_attachments.title') %>++<%=+ render(Primer::OpenProject::PageHeader.new) do |header|+ header.with_title(variant: :large) { t('antivirus_scan.quarantined_attachments.title') }+ end+%>++<%= render ::Admin::QuarantinedAttachments::TableComponent.new(rows: @attachments) %>diff --git a/app/views/admin/settings/attachments_settings/show.html.erb b/app/views/admin/settings/attachments_settings/show.html.erbindex 2f8c1b3060f..7c65fcfa56e 100644--- a/app/views/admin/settings/attachments_settings/show.html.erb+++ b/app/views/admin/settings/attachments_settings/show.html.erb@@ -29,7 +29,7 @@ See COPYRIGHT and LICENSE files for more details.<%= toolbar title: t(:'attributes.attachments') %><%= styled_form_tag(- admin_settings_update_attachments_path,+ admin_settings_attachments_path,method: :patch,data: {'controller': 'refresh-on-form-changes',diff --git a/config/initializers/menus.rb b/config/initializers/menus.rbindex fdd25575784..f16f599d7cd 100644--- a/config/initializers/menus.rb+++ b/config/initializers/menus.rb@@ -350,6 +350,18 @@ Redmine::MenuManager.map :admin_menu do |menu|icon: 'help2',if: Proc.new { User.current.allowed_globally?(:edit_attribute_help_texts) }+ menu.push :settings_attachments,+ { controller: '/admin/settings/attachments_settings', action: :show },+ caption: :'attributes.attachments',+ icon: 'attachment',+ if: Proc.new { User.current.admin? }++ menu.push :attachment_quarantine,+ { controller: '/admin/attachments/quarantined_attachments', action: :index },+ caption: :'antivirus_scan.quarantined_attachments.title',+ parent: :settings_attachments,+ if: Proc.new { User.current.admin? && Setting.antivirus_scan_action == :quarantine }+menu.push :enumerations,{ controller: '/enumerations' },if: Proc.new { User.current.admin? },diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 8af65f96651..b5735ae8e2d 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -103,8 +103,12 @@ en:antivirus_scan:not_processed_yet_message: "Downloading is blocked, as file was not scanned for viruses yet. Please try again later."- quarantine_message: "A virus was detected in file %{filename}. It has been quarantined and is not available for download."- delete_message: "A virus was detected in file %{filename}. The file has been deleted."+ quarantined_message: "A virus was detected in file %{filename}. It has been quarantined and is not available for download."+ deleted_message: "A virus was detected in file %{filename}. The file has been deleted."+ deleted_by_admin: "The quarantined file %{filename} has been deleted by an administrator."+ quarantined_attachments:+ container: "Container"+ title: "Quarantined attachments"attribute_help_texts:note_public: "Any text and images you add to this field is publicly visible to all logged in users!"diff --git a/config/routes.rb b/config/routes.rbindex 0d041130771..7e04fe0ae02 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -422,9 +422,9 @@ Rails.application.routes.draw dopatch tab[:name], controller: tab[:controller], action: :update, as: "update_#{tab[:name]}"end- get 'attachments/av_form', controller: '/admin/settings/attachments_settings', action: 'av_form'-resource :authentication, controller: '/admin/settings/authentication_settings', only: %i[show update]+ resource :attachments, controller: '/admin/settings/attachments_settings', only: %i[show update]+ get 'attachments/av_form', controller: '/admin/settings/attachments_settings', action: 'av_form'resource :incoming_mails, controller: '/admin/settings/incoming_mails_settings', only: %i[show update]resource :aggregation, controller: '/admin/settings/aggregation_settings', only: %i[show update]resource :mail_notifications, controller: '/admin/settings/mail_notifications_settings', only: %i[show update]@@ -441,8 +441,11 @@ Rails.application.routes.draw do# Plugin settingsget 'plugin/:id', action: :show_pluginpost 'plugin/:id', action: :update_plugin+end+ resources :quarantined_attachments, controller: '/admin/attachments/quarantined_attachments', only: %i[index destroy]+resource :backups, controller: '/admin/backups', only: %i[show] docollection doget :reset_tokendiff --git a/spec/features/admin/attachments/quarantined_attachments_spec.rb b/spec/features/admin/attachments/quarantined_attachments_spec.rbnew file mode 100644index 00000000000..616f6c44949--- /dev/null+++ b/spec/features/admin/attachments/quarantined_attachments_spec.rb@@ -0,0 +1,86 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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'++RSpec.describe 'Quarantined attachments', :js, :with_cuprite do+ shared_let(:other_author) { create(:user) }+ shared_let(:admin) { create(:admin) }++ shared_let(:container) { create(:work_package) }+ shared_let(:own_quarantined_attachment) do+ create(:attachment, container:, status: :quarantined, author: admin, filename: 'my-own.txt')+ end+ shared_let(:quarantined_attachment) do+ create(:attachment, container:, status: :quarantined, author: other_author, filename: 'other-1.txt')+ end+ shared_let(:other_quarantined_attachment) do+ create(:attachment, container:, status: :quarantined, author: other_author, filename: 'other-2.txt')+ end++ before do+ login_as admin+ end++ it 'allows management other attachments' do+ visit admin_quarantined_attachments_path++ expect(page).to have_text 'my-own.txt'+ expect(page).to have_text 'other-1.txt'+ expect(page).to have_text 'other-2.txt'++ page.within("#quarantined_attachment_#{own_quarantined_attachment.id}") do+ expect(page).to have_no_link I18n.t('antivirus_scan.quarantined_attachments.override')+ expect(page).to have_no_link I18n.t('antivirus_scan.quarantined_attachments.delete')+ end++ page.within("#quarantined_attachment_#{quarantined_attachment.id}") do+ expect(page).to have_link I18n.t('antivirus_scan.quarantined_attachments.override')+ expect(page).to have_link I18n.t('antivirus_scan.quarantined_attachments.delete')++ accept_confirm do+ click_link I18n.t('antivirus_scan.quarantined_attachments.delete')+ end++ expect { quarantined_attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)+ end++ page.within("#quarantined_attachment_#{other_quarantined_attachment.id}") do+ expect(page).to have_link I18n.t('antivirus_scan.quarantined_attachments.override')+ expect(page).to have_link I18n.t('antivirus_scan.quarantined_attachments.delete')++ accept_confirm do+ click_link I18n.t('antivirus_scan.quarantined_attachments.override')+ end+ end++ expect(page).to have_text 'my-own.txt'+ expect(page).not_to have_text 'other-1.txt'+ expect(page).not_to have_text 'other-2.txt'+ end+enddiff --git a/spec/requests/admin/attachments/quarantined_attachments_spec.rb b/spec/requests/admin/attachments/quarantined_attachments_spec.rbnew file mode 100644index 00000000000..e4754349acc--- /dev/null+++ b/spec/requests/admin/attachments/quarantined_attachments_spec.rb@@ -0,0 +1,106 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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'++RSpec.describe 'Quarantined attachments',+ type: :rails_request do++ shared_let(:other_author) { create(:user) }+ shared_let(:admin) { create(:admin) }++ shared_let(:container) { create(:work_package) }+ shared_let(:own_quarantined_attachment) do+ create(:attachment, container:, status: :quarantined, author: admin, filename: 'my-own.txt')+ end+ shared_let(:quarantined_attachment) do+ create(:attachment, container:, status: :quarantined, author: other_author, filename: 'other-1.txt')+ end+ shared_let(:scanned_attachment) do+ create(:attachment, container:, status: :scanned, author: other_author, filename: 'scanned-1.txt')+ end++ before do+ login_as admin+ ActionController::Base.allow_forgery_protection = false+ end++ around do |example|+ state = ActionController::Base.allow_forgery_protection+ begin+ ActionController::Base.allow_forgery_protection = false+ example.run+ ensure+ ActionController::Base.allow_forgery_protection = state+ end+ end++ describe 'DELETE /admin/quarantined_attachments/:id' do+ it 'allows management other attachments' do+ delete admin_quarantined_attachment_path(quarantined_attachment)+ expect(response).to be_redirect++ expect { quarantined_attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)+ end++ it 'disallows own attachments' do+ delete admin_quarantined_attachment_path(own_quarantined_attachment)+ expect(response).to be_redirect++ expect { own_quarantined_attachment.reload }.not_to raise_error+ end++ it 'fails to find if not quarantined' do+ delete admin_quarantined_attachment_path(scanned_attachment)+ expect(response).to have_http_status 404+ end+ end++ describe 'PATCH /admin/quarantined_attachments/:id/override' do+ it 'allows management other attachments' do+ patch override_admin_quarantined_attachment_path(quarantined_attachment)+ expect(response).to be_redirect++ quarantined_attachment.reload+ expect(quarantined_attachment).to be_status_scanned+ end++ it 'disallows own attachments' do+ patch override_admin_quarantined_attachment_path(own_quarantined_attachment)+ expect(response).to be_redirect++ own_quarantined_attachment.reload+ expect(own_quarantined_attachment).to be_status_quarantined+ end++ it 'fails to find if not quarantined' do+ patch override_admin_quarantined_attachment_path(scanned_attachment)+ expect(response).to have_http_status 404+ end+ end+end