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

use service for query deletion

ulferts749b90cfeb4e
diff --git a/app/contracts/projects/delete_contract.rb b/app/contracts/projects/delete_contract.rb
index 4c0967a21a3..1f80d3ee0fd 100644
--- a/app/contracts/projects/delete_contract.rb
+++ b/app/contracts/projects/delete_contract.rb
@@ -26,8 +26,6 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'model_contract'
-
module Projects
class DeleteContract < ::DeleteContract
delete_permission :admin
diff --git a/app/contracts/queries/projects/project_queries/delete_contract.rb b/app/contracts/queries/projects/project_queries/delete_contract.rb
new file mode 100644
index 00000000000..30deba66b35
--- /dev/null
+++ b/app/contracts/queries/projects/project_queries/delete_contract.rb
@@ -0,0 +1,33 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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 Queries::Projects::ProjectQueries
+ class DeleteContract < ::DeleteContract
+ delete_permission -> { user == model.user }
+ end
+end
diff --git a/app/controllers/projects/queries_controller.rb b/app/controllers/projects/queries_controller.rb
index c99e433d5e6..ba6cee37ea4 100644
--- a/app/controllers/projects/queries_controller.rb
+++ b/app/controllers/projects/queries_controller.rb
@@ -47,7 +47,8 @@ class Projects::QueriesController < ApplicationController
end
def destroy
- @query.destroy
+ Queries::Projects::ProjectQueries::DeleteService.new(user: current_user, model: @query)
+ .call
redirect_to projects_path
end
diff --git a/app/services/queries/projects/project_queries/delete_service.rb b/app/services/queries/projects/project_queries/delete_service.rb
new file mode 100644
index 00000000000..20a66695399
--- /dev/null
+++ b/app/services/queries/projects/project_queries/delete_service.rb
@@ -0,0 +1,29 @@
+#-- 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.
+#++
+
+class Queries::Projects::ProjectQueries::DeleteService < BaseServices::Delete; end
diff --git a/spec/contracts/queries/projects/project_queries/delete_contract_spec.rb b/spec/contracts/queries/projects/project_queries/delete_contract_spec.rb
new file mode 100644
index 00000000000..7195d530aa9
--- /dev/null
+++ b/spec/contracts/queries/projects/project_queries/delete_contract_spec.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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'
+require 'contracts/shared/model_contract_shared_context'
+
+RSpec.describe Queries::Projects::ProjectQueries::DeleteContract do
+ include_context 'ModelContract shared context'
+
+ let(:current_user) { build_stubbed(:user) }
+ let(:query_user) { current_user }
+ let(:query) { build_stubbed(:project_query, user: query_user) }
+ let(:contract) { described_class.new(query, current_user) }
+
+ it_behaves_like 'contract is valid'
+
+ context 'if the current user is not the query user' do
+ let(:query_user) { build_stubbed(:user) }
+
+ it_behaves_like 'contract is invalid', base: :error_unauthorized
+ end
+end