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
fix archiving projects via api
ulferts8656c2664b8a
diff --git a/app/contracts/projects/archive_contract.rb b/app/contracts/projects/archive_contract.rbindex 5ab6e2931f0..db28eaad028 100644--- a/app/contracts/projects/archive_contract.rb+++ b/app/contracts/projects/archive_contract.rb@@ -28,8 +28,10 @@module Projectsclass ArchiveContract < ModelContract+ include Projects::Archiver+def validate- user_allowed+ validate_admin_onlyvalidate_no_foreign_wp_referencessuper@@ -37,31 +39,8 @@ module Projectsprotected- def user_allowed- unless authorized?- errors.add :base, :error_unauthorized- end- end-- # Check that there is no wp of a non descendant project that is assigned- # to one of the project or descendant versions- def validate_no_foreign_wp_references- version_ids = model.rolled_up_versions.select(:id)-- exists = WorkPackage- .where.not(project_id: model.self_and_descendants.select(:id))- .where(version_id: version_ids)- .exists?-- errors.add :base, :foreign_wps_reference_version if exists- end-def validate_model?falseend-- def authorized?- user.admin?- endendenddiff --git a/app/contracts/projects/archiver.rb b/app/contracts/projects/archiver.rbnew file mode 100644index 00000000000..252bc5783a9--- /dev/null+++ b/app/contracts/projects/archiver.rb@@ -0,0 +1,56 @@+#-- 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 Projects+ module Archiver+ def validate_admin_only+ unless user.admin?+ errors.add :base, :error_unauthorized+ end+ end++ # Check that there is no wp of a non descendant project that is assigned+ # to one of the project or descendant versions+ def validate_no_foreign_wp_references+ version_ids = model.rolled_up_versions.select(:id)++ exists = WorkPackage+ .where.not(project_id: model.self_and_descendants.select(:id))+ .where(version_id: version_ids)+ .exists?++ errors.add :base, :foreign_wps_reference_version if exists+ end++ def validate_all_ancestors_active+ if model.ancestors.any?(&:archived?)+ errors.add :base, :archived_ancestor+ end+ end+ end+enddiff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex fabc3a28866..8c360707bab 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -32,6 +32,7 @@ module Projectsclass BaseContract < ::ModelContractinclude AssignableValuesContractinclude AssignableCustomFieldValues+ include Projects::Archiverattribute :nameattribute :identifier@@ -39,6 +40,7 @@ module Projectsattribute :publicattribute :active dovalidate_active_present+ validate_changing_activeendattribute :parent dovalidate_parent_assignable@@ -96,7 +98,9 @@ module Projectsdef validate_user_allowed_to_managewith_unchanged_id do- errors.add :base, :error_unauthorized unless user.allowed_to?(manage_permission, model)+ with_active_assumed do+ errors.add :base, :error_unauthorized unless user.allowed_to?(manage_permission, model)+ endendend@@ -122,5 +126,27 @@ module Projectsensuremodel.id = project_idend++ def with_active_assumed+ active = model.active+ model.active = true++ yield+ ensure+ model.active = active+ end++ def validate_changing_active+ return unless model.active_changed?++ validate_admin_only++ if model.active?+ # switched to active -> unarchiving+ validate_all_ancestors_active+ else+ validate_no_foreign_wp_references+ end+ endendenddiff --git a/app/contracts/projects/unarchive_contract.rb b/app/contracts/projects/unarchive_contract.rbindex 10b3b18fbfe..d733b6bb543 100644--- a/app/contracts/projects/unarchive_contract.rb+++ b/app/contracts/projects/unarchive_contract.rb@@ -28,8 +28,10 @@module Projectsclass UnarchiveContract < ModelContract+ include Projects::Archiver+def validate- user_allowed+ validate_admin_onlyvalidate_all_ancestors_activesuper@@ -37,24 +39,8 @@ module Projectsprotected- def user_allowed- unless authorized?- errors.add :base, :error_unauthorized- end- end-- def validate_all_ancestors_active- if model.ancestors.any?(&:archived?)- errors.add :base, :archived_ancestor- end- end-def validate_model?falseend-- def authorized?- user.admin?- endendenddiff --git a/app/services/projects/update_service.rb b/app/services/projects/update_service.rbindex c4f58fd6be0..61421df17d2 100644--- a/app/services/projects/update_service.rb+++ b/app/services/projects/update_service.rb@@ -50,6 +50,7 @@ module Projectssend_update_notificationupdate_wp_versions_on_parent_changepersist_status+ handle_archivingservice_callend@@ -81,5 +82,21 @@ module Projectsdef persist_statusmodel.status.save if model.status.changed?end++ def handle_archiving+ return unless model.saved_change_to_active?++ if model.active?+ # was unarchived+ Projects::UnarchiveService+ .new(user: user, model: model)+ .call+ else+ # as archived+ Projects::ArchiveService+ .new(user: user, model: model)+ .call+ end+ endendenddiff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rbindex 9a3355c6912..2257b1aeaaa 100644--- a/spec/requests/api/v3/project_resource_spec.rb+++ b/spec/requests/api/v3/project_resource_spec.rb@@ -517,12 +517,12 @@ describe 'API v3 Project resource', type: :request, content_type: :json doexpect(last_response.body).to be_json_eql(- {- "format": "markdown",- "html": "<p>Some explanation.</p>",- "raw": "Some explanation."- }.to_json- )+ {+ "format": "markdown",+ "html": "<p>Some explanation.</p>",+ "raw": "Some explanation."+ }.to_json+ ).at_path("statusExplanation")endend@@ -621,6 +621,61 @@ describe 'API v3 Project resource', type: :request, content_type: :json do.at_path('message')endend++ context 'deactivating (archiving) the project' do+ context 'for an admin' do+ let(:current_user) do+ FactoryBot.create(:admin)+ end+ let(:project) do+ FactoryBot.create(:project).tap do |p|+ p.children << child_project+ end+ end+ let(:child_project) do+ FactoryBot.create(:project)+ end++ let(:body) do+ {+ active: false+ }+ end++ it 'responds with 200 OK' do+ expect(last_response.status)+ .to eql(200)+ end++ it 'archives the project' do+ expect(project.reload.active)+ .to be_falsey+ end++ it 'archives the child project' do+ expect(child_project.reload.active)+ .to be_falsey+ end+ end++ context 'for a non admin' do+ let(:body) do+ {+ active: false+ }+ end++ it 'responds with 403' do+ expect(last_response.status)+ .to eql(403)+ end++ it 'does not alter the project' do+ expect(project.reload.active)+ .to be_truthy+ end+ end+ endenddescribe '#delete /api/v3/projects/:id' do