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 archive project through API
Christophe Bliard164daa48f838
diff --git a/app/contracts/concerns/requires_admin_guard.rb b/app/contracts/concerns/requires_admin_guard.rbindex b528807c354..568193a0bca 100644--- a/app/contracts/concerns/requires_admin_guard.rb+++ b/app/contracts/concerns/requires_admin_guard.rb@@ -30,15 +30,11 @@ module RequiresAdminGuardextend ActiveSupport::Concernincluded do- validate { validate_admin_only(user, errors) }+ validate :validate_admin_onlyend- module_function-# Adds an error if user is archived or not an admin.- #- # Can be used from outside like +RequiresAdminGuard.validate_admin_only(user, errors)+- def validate_admin_only(user, errors)+ def validate_admin_onlyunless user.admin? && user.active?errors.add :base, :error_unauthorizedenddiff --git a/app/contracts/projects/archive_contract.rb b/app/contracts/projects/archive_contract.rbindex a0a3300267d..4efb22ba8ee 100644--- a/app/contracts/projects/archive_contract.rb+++ b/app/contracts/projects/archive_contract.rb@@ -27,15 +27,24 @@#++module Projects- class ArchiveContract < ModelContract- include Projects::Archiver-+ class ArchiveContract < ::BaseContractvalidate :validate_no_foreign_wp_referencesvalidate :validate_has_archive_project_permission- validate :validate_has_archive_project_permissionprotected+ # 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))+ .exists?(version_id: version_ids)++ errors.add :base, :foreign_wps_reference_version if exists+ end+def validate_has_archive_project_permissionvalidate_can_archive_projectvalidate_can_archive_subprojects@@ -57,9 +66,5 @@ module Projectserrors.add :base, :archive_permission_missing_on_subprojectsendend-- def validate_model?- false- endendenddiff --git a/app/contracts/projects/archiver.rb b/app/contracts/projects/archiver.rbdeleted file mode 100644index 1b3b8e954d6..00000000000--- a/app/contracts/projects/archiver.rb+++ /dev/null@@ -1,50 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2023 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 Projects- module Archiver- # 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 c5fe9015e6c..a51c8b7bbe2 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -30,7 +30,6 @@ module Projectsclass BaseContract < ::ModelContractinclude AssignableValuesContractinclude AssignableCustomFieldValues- include Projects::Archiverattribute :nameattribute :identifier@@ -131,14 +130,11 @@ module Projectsdef validate_changing_activereturn unless model.active_changed?- RequiresAdminGuard.validate_admin_only(user, errors)+ contract_klass = model.being_archived? ? ArchiveContract : UnarchiveContract+ contract = contract_klass.new(model, user)+ contract.validate- if model.active?- # switched to active -> unarchiving- validate_all_ancestors_active- else- validate_no_foreign_wp_references- end+ errors.merge!(contract.errors)endendenddiff --git a/app/contracts/projects/unarchive_contract.rb b/app/contracts/projects/unarchive_contract.rbindex d0f6b578481..e2144abea9c 100644--- a/app/contracts/projects/unarchive_contract.rb+++ b/app/contracts/projects/unarchive_contract.rb@@ -27,16 +27,17 @@#++module Projects- class UnarchiveContract < ModelContract+ class UnarchiveContract < ::BaseContractinclude RequiresAdminGuard- include Projects::Archivervalidate :validate_all_ancestors_activeprotected- def validate_model?- false+ def validate_all_ancestors_active+ if model.ancestors.any?(&:archived?)+ errors.add :base, :archived_ancestor+ endendendenddiff --git a/app/models/project.rb b/app/models/project.rbindex 8b9f027c974..b09223f0341 100644--- a/app/models/project.rb+++ b/app/models/project.rb@@ -162,6 +162,10 @@ class Project < ApplicationRecord!active?end+ def being_archived?+ (active == false) && (active_was == true)+ end+def copy_allowed?User.current.allowed_to?(:copy_projects, self)enddiff --git a/app/models/users/project_role_cache.rb b/app/models/users/project_role_cache.rbindex 2ed6f6a4213..8301a61c9f8 100644--- a/app/models/users/project_role_cache.rb+++ b/app/models/users/project_role_cache.rb@@ -40,8 +40,9 @@ class Users::ProjectRoleCacheprivatedef roles(project)- # No role on archived projects- return [] unless !project || project&.active?+ # Project is nil if checking global role+ # No roles on archived projects, unless the active state is being changed+ return [] if project && archived?(project)# Return all roles if user is adminreturn all_givable_roles if user.admin?@@ -56,4 +57,11 @@ class Users::ProjectRoleCachedef all_givable_roles@all_givable_roles ||= Role.givable.to_aend++ def archived?(project)+ # project for which activity is being changed is still considered active+ return false if project.being_archived?++ project.archived?+ endenddiff --git a/app/services/authorization/user_allowed_service.rb b/app/services/authorization/user_allowed_service.rbindex 6a174c6b057..fa140bdb09f 100644--- a/app/services/authorization/user_allowed_service.rb+++ b/app/services/authorization/user_allowed_service.rb@@ -79,7 +79,7 @@ class Authorization::UserAllowedServiceend# No action allowed on archived projects- return false unless project.active?+ return false unless project.active? || project.being_archived?# No action allowed on disabled modulesreturn false unless project.allows_to?(action)# Inactive users are never authorizeddiff --git a/app/services/projects/update_service.rb b/app/services/projects/update_service.rbindex 4a756227fff..098f195b460 100644--- a/app/services/projects/update_service.rb+++ b/app/services/projects/update_service.rb@@ -90,17 +90,19 @@ module Projectsdef handle_archivingreturn unless model.saved_change_to_active?- if model.active?- # was unarchived- Projects::UnarchiveService- .new(user:, model:)- .call- else- # as archived- Projects::ArchiveService- .new(user:, model:)- .call- end+ service_class =+ if model.active?+ # was unarchived+ Projects::UnarchiveService+ else+ # was archived+ Projects::ArchiveService+ end++ # EmptyContract is used because archive/unarchive conditions have+ # already been checked in Projects::UpdateContract+ service = service_class.new(user:, model:, contract_class: EmptyContract)+ service.callendendenddiff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rbindex 91affacda11..a36e293fb71 100644--- a/spec/models/project_spec.rb+++ b/spec/models/project_spec.rb@@ -55,18 +55,54 @@ describe Project doenddescribe '#archived?' do- context 'if archived' do- it 'is true' do- expect(project).not_to be_archived+ subject { project.archived? }++ context 'if active is true' do+ let(:active) { true }++ it { is_expected.to be false }+ end++ context 'if active is false' do+ let(:active) { false }++ it { is_expected.to be true }+ end+ end++ describe '#being_archived?' do+ subject { project.being_archived? }++ context 'if active is true' do+ let(:active) { true }++ it { is_expected.to be false }+ end++ context 'if active was true and changes to false (marking as archived)' do+ let(:active) { true }++ before do+ project.active = falseend++ it { is_expected.to be true }end- context 'if not archived' do+ context 'if active is false' dolet(:active) { false }- it 'is false' do- expect(project).to be_archived+ it { is_expected.to be false }+ end++ context 'if active was false and changes to true (marking as active)' do+ let(:active) { false }++ before do+ project.active = trueend++ it { is_expected.to be false }endenddiff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rbindex a4781e23a04..bd1865d6709 100644--- a/spec/requests/api/v3/projects/update_resource_spec.rb+++ b/spec/requests/api/v3/projects/update_resource_spec.rb@@ -268,6 +268,12 @@ describe 'API v3 Project resource update', type: :request, content_type: :json dendcontext 'when deactivating (archiving) the project' do+ let(:body) do+ {+ active: false+ }+ end+context 'for an admin' dolet(:current_user) docreate(:admin)@@ -281,12 +287,6 @@ describe 'API v3 Project resource update', type: :request, content_type: :json dcreate(:project)end- let(:body) do- {- active: false- }- end-it 'responds with 200 OK' doexpect(last_response.status).to be(200)@@ -304,12 +304,6 @@ describe 'API v3 Project resource update', type: :request, content_type: :json dendcontext 'for a non admin' do- let(:body) do- {- active: false- }- end-it 'responds with 403' doexpect(last_response.status).to be(403)diff --git a/spec/services/authorization/user_allowed_service_spec.rb b/spec/services/authorization/user_allowed_service_spec.rbindex 9408206eac6..a832c7f9558 100644--- a/spec/services/authorization/user_allowed_service_spec.rb+++ b/spec/services/authorization/user_allowed_service_spec.rb@@ -137,6 +137,7 @@ describe Authorization::UserAllowedService dobefore doArray(context).each do |project|project.active = false+ project.clear_changes_informationendend@@ -151,6 +152,18 @@ describe Authorization::UserAllowedService doendend+ context 'with the project being archived' do+ before do+ Array(context).each do |project|+ project.active = false+ end+ end++ it 'is true' do+ expect(subject).to be_truthy+ end+ end+context 'with the project not having the action enabled' dolet(:project_allows_to) { false }