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

fix archiving projects via api

ulferts8656c2664b8a
diff --git a/app/contracts/projects/archive_contract.rb b/app/contracts/projects/archive_contract.rb
index 5ab6e2931f0..db28eaad028 100644
--- a/app/contracts/projects/archive_contract.rb
+++ b/app/contracts/projects/archive_contract.rb
@@ -28,8 +28,10 @@
module Projects
class ArchiveContract < ModelContract
+ include Projects::Archiver
+
def validate
- user_allowed
+ validate_admin_only
validate_no_foreign_wp_references
super
@@ -37,31 +39,8 @@ module Projects
protected
- 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?
false
end
-
- def authorized?
- user.admin?
- end
end
end
diff --git a/app/contracts/projects/archiver.rb b/app/contracts/projects/archiver.rb
new file mode 100644
index 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
+end
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index fabc3a28866..8c360707bab 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -32,6 +32,7 @@ module Projects
class BaseContract < ::ModelContract
include AssignableValuesContract
include AssignableCustomFieldValues
+ include Projects::Archiver
attribute :name
attribute :identifier
@@ -39,6 +40,7 @@ module Projects
attribute :public
attribute :active do
validate_active_present
+ validate_changing_active
end
attribute :parent do
validate_parent_assignable
@@ -96,7 +98,9 @@ module Projects
def validate_user_allowed_to_manage
with_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)
+ end
end
end
@@ -122,5 +126,27 @@ module Projects
ensure
model.id = project_id
end
+
+ 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
+ end
end
end
diff --git a/app/contracts/projects/unarchive_contract.rb b/app/contracts/projects/unarchive_contract.rb
index 10b3b18fbfe..d733b6bb543 100644
--- a/app/contracts/projects/unarchive_contract.rb
+++ b/app/contracts/projects/unarchive_contract.rb
@@ -28,8 +28,10 @@
module Projects
class UnarchiveContract < ModelContract
+ include Projects::Archiver
+
def validate
- user_allowed
+ validate_admin_only
validate_all_ancestors_active
super
@@ -37,24 +39,8 @@ module Projects
protected
- 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?
false
end
-
- def authorized?
- user.admin?
- end
end
end
diff --git a/app/services/projects/update_service.rb b/app/services/projects/update_service.rb
index c4f58fd6be0..61421df17d2 100644
--- a/app/services/projects/update_service.rb
+++ b/app/services/projects/update_service.rb
@@ -50,6 +50,7 @@ module Projects
send_update_notification
update_wp_versions_on_parent_change
persist_status
+ handle_archiving
service_call
end
@@ -81,5 +82,21 @@ module Projects
def persist_status
model.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
+ end
end
end
diff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rb
index 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 do
expect(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")
end
end
@@ -621,6 +621,61 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
.at_path('message')
end
end
+
+ 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
+ end
end
describe '#delete /api/v3/projects/:id' do