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
Add checks for update and delete actions
Mir Bhatiaef5150649a38
diff --git a/app/contracts/relations/base_contract.rb b/app/contracts/relations/base_contract.rbindex 59754616162..11998a861a6 100644--- a/app/contracts/relations/base_contract.rb+++ b/app/contracts/relations/base_contract.rb@@ -36,9 +36,9 @@ module Relationsattribute :fromattribute :to- validate :validate_user_allowedvalidate :validate_from_existsvalidate :validate_to_exists+ validate :validate_user_allowedvalidate :validate_nodes_relatablevalidate :validate_accepted_type@@ -76,7 +76,8 @@ module Relationsenddef validate_user_allowed- return if model.to_id.nil? || model.from_id.nil?+ # Only check if the work packages exist and are visible+ return if skip_validation?unless from_manageable?errors.add :from_id, :error_not_manageable@@ -110,5 +111,9 @@ module Relationsdef to_manageable?user.allowed_in_work_package?(:manage_work_package_relations, model.to)end++ def skip_validation?+ model.to_id.nil? || model.from_id.nil? || errors[:from_id].any? || errors[:to_id].any?+ endendenddiff --git a/app/contracts/relations/delete_contract.rb b/app/contracts/relations/delete_contract.rbindex b00e08dedf7..fd6f9d2052c 100644--- a/app/contracts/relations/delete_contract.rb+++ b/app/contracts/relations/delete_contract.rb@@ -31,5 +31,15 @@module Relationsclass DeleteContract < ::DeleteContractdelete_permission -> { user.allowed_in_work_package?(:manage_work_package_relations, model.from) }++ validate :validate_to_deletable++ def validate_to_deletable+ return unless user.allowed_in_work_package?(:manage_work_package_relations, model.from)++ unless user.allowed_in_work_package?(:manage_work_package_relations, model.to)+ errors.add :base, :error_not_deletable+ end+ endendenddiff --git a/app/controllers/work_package_relations_controller.rb b/app/controllers/work_package_relations_controller.rbindex 98699eee7b1..181bd932cce 100644--- a/app/controllers/work_package_relations_controller.rb+++ b/app/controllers/work_package_relations_controller.rb@@ -30,6 +30,7 @@class WorkPackageRelationsController < ApplicationControllerinclude OpTurbo::ComponentStream+ include OpTurbo::FlashStreamHelperbefore_action :set_work_packagebefore_action :set_relation, only: %i[edit update]@@ -48,10 +49,16 @@ class WorkPackageRelationsController < ApplicationControllerenddef edit- respond_with_dialog(- WorkPackageRelationsTab::WorkPackageRelationDialogComponent- .new(work_package: @work_package, relation: @relation)- )+ contract = Relations::UpdateContract.new(@relation, current_user)++ if contract.valid?+ respond_with_dialog(+ WorkPackageRelationsTab::WorkPackageRelationDialogComponent+ .new(work_package: @work_package, relation: @relation)+ )+ else+ respond_with_flash_error(message: I18n.t(:"activerecord.errors.models.relation.attributes.base.error_not_editable"))+ endenddef create@@ -97,7 +104,11 @@ class WorkPackageRelationsController < ApplicationControllerServiceResult.successend- respond_with_relations_tab_update(service_result)+ if service_result.failure?+ respond_with_flash_error(message: service_result.message)+ else+ respond_with_relations_tab_update(service_result)+ endendprivatediff --git a/config/locales/en.yml b/config/locales/en.ymlindex f503b485344..4dd22b770b0 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1568,12 +1568,17 @@ en:typed_dag:circular_dependency: "The relationship creates a circle of relationships."attributes:+ base:+ error_not_deletable: "This relation cannot be deleted because you do not have edit permissions for the selected work package."+ error_not_editable: "This relation cannot be edited because you do not have edit permissions for the selected work package."to_id:format: "The selected work package %{message}"+ error_not_found: "could not be found."error_readonly: "cannot be changed for existing relations."error_not_manageable: "cannot be added because you do not have edit permissions for the selected work package."from_id:format: "The selected work package %{message}"+ error_not_found: "could not be found."error_readonly: "cannot be changed for existing relations."error_not_manageable: "cannot be added because you do not have edit permissions for the selected work package."repository:diff --git a/spec/contracts/relations/delete_contract_spec.rb b/spec/contracts/relations/delete_contract_spec.rbnew file mode 100644index 00000000000..dce64973fd6--- /dev/null+++ b/spec/contracts/relations/delete_contract_spec.rb@@ -0,0 +1,94 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Relations::DeleteContract do+ include_context "ModelContract shared context"++ shared_let(:from_project) { create(:project) }+ shared_let(:to_project) { create(:project) }+ shared_let(:from_work_package) { create(:work_package, project: from_project) }+ shared_let(:to_work_package) { create(:work_package, project: to_project) }+ shared_let(:relation) { create(:relation, from: from_work_package, to: to_work_package) }++ let(:contract) { described_class.new(relation, current_user) }++ context "when user has permission to manage relations for both work packages" do+ let(:current_user) do+ create(:user,+ member_with_permissions: {+ from_project => %i[manage_work_package_relations],+ to_project => %i[manage_work_package_relations]+ })+ end++ it_behaves_like "contract is valid"+ end++ context "when user has permission for 'from' work package but not 'to' work package" do+ let(:current_user) do+ create(:user,+ member_with_permissions: {+ from_project => %i[manage_work_package_relations]+ })+ end++ it_behaves_like "contract is invalid", base: :error_not_deletable+ end++ context "when user has permission for 'to' work package but not 'from' work package" do+ let(:current_user) do+ create(:user,+ member_with_permissions: {+ to_project => %i[manage_work_package_relations]+ })+ end++ it_behaves_like "contract is invalid", base: :error_unauthorized+ end++ context "when user has no permissions for either work package" do+ let(:current_user) { create(:user) }++ it_behaves_like "contract is invalid", base: :error_unauthorized+ end++ context "when user is an admin" do+ let(:current_user) { create(:admin) }++ it_behaves_like "contract is valid"+ end++ include_examples "contract reuses the model errors" do+ let(:current_user) { create(:admin) }+ end+enddiff --git a/spec/features/work_packages/details/relations/primerized_relations_spec.rb b/spec/features/work_packages/details/relations/primerized_relations_spec.rbindex a9f2400c0a6..a7436f32ce1 100644--- a/spec/features/work_packages/details/relations/primerized_relations_spec.rb+++ b/spec/features/work_packages/details/relations/primerized_relations_spec.rb@@ -365,7 +365,7 @@ RSpec.describe "Primerized work package relations tab",wait_for_network_idle- expect(page).to have_text "Related work package not found."+ expect(page).to have_text "The selected work package could not be found."relations_tab.search_in_autocompleter(wp_blocks)diff --git a/spec/requests/api/v3/relations/relations_api_spec.rb b/spec/requests/api/v3/relations/relations_api_spec.rbindex c74f85af5ef..34c48258e4c 100644--- a/spec/requests/api/v3/relations/relations_api_spec.rb+++ b/spec/requests/api/v3/relations/relations_api_spec.rb@@ -338,8 +338,8 @@ RSpec.describe "API v3 Relation resource", content_type: :json docontext "without manage_work_package_relations" dolet(:permissions) { [:view_work_packages] }- it "is forbidden" do- expect(last_response).to have_http_status :forbidden+ it "returns 422" do+ expect(last_response).to have_http_status :unprocessable_entityendend@@ -347,7 +347,7 @@ RSpec.describe "API v3 Relation resource", content_type: :json do# This one is expected to fail (422) because the `to` work package# is in another project for which the user does not have permission to# view work packages.- context "without manage_work_package_relations" do+ context "without manage_work_package_relations for 'to' work package" dolet!(:to) { create(:work_package) }it "returns 422" do