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
Check archive_project permission when archiving through API
Christophe Bliard9de8b17e26ba
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex a51c8b7bbe2..66d76674d9b 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -89,9 +89,7 @@ module Projectsdef validate_user_allowed_to_managewith_unchanged_id do- with_active_assumed do- errors.add :base, :error_unauthorized unless user.allowed_to?(manage_permission, model)- end+ errors.add :base, :error_unauthorized unless user.allowed_to?(manage_permission, model)endend@@ -118,15 +116,6 @@ module Projectsmodel.id = project_idend- def with_active_assumed- active = model.active- model.active = true-- yield- ensure- model.active = active- end-def validate_changing_activereturn unless model.active_changed?diff --git a/app/contracts/projects/update_contract.rb b/app/contracts/projects/update_contract.rbindex 84860fff644..ca9b662c61f 100644--- a/app/contracts/projects/update_contract.rb+++ b/app/contracts/projects/update_contract.rb@@ -31,7 +31,13 @@ module Projectsprivatedef manage_permission- :edit_project+ if changed_by_user == ["active"]+ :archive_project+ else+ # if "active" is changed, :archive_project permission will also be+ # checked in `Projects::BaseContract#validate_changing_active`+ :edit_project+ endendendenddiff --git a/lib/open_project/access_control/permission.rb b/lib/open_project/access_control/permission.rbindex 2ccf62e644b..3bfc7491d7e 100644--- a/lib/open_project/access_control/permission.rb+++ b/lib/open_project/access_control/permission.rb@@ -35,6 +35,9 @@ module OpenProject:project_module,:dependencies+ # @param public [Boolean] when true, the permission is granted to anybody+ # having at least one role in a project, regardless of the role's+ # permissions.def initialize(name,hash,public: false,diff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rbindex bd1865d6709..5cb23de2bb3 100644--- a/spec/requests/api/v3/projects/update_resource_spec.rb+++ b/spec/requests/api/v3/projects/update_resource_spec.rb@@ -41,10 +41,6 @@ describe 'API v3 Project resource update', type: :request, content_type: :json dlet(:project_status) dobuild(:project_status, project: nil)end- let(:other_project) do- create(:project, public: false)- end- let(:role) { create(:role) }let(:custom_field) docreate(:text_project_custom_field)end@@ -267,7 +263,7 @@ describe 'API v3 Project resource update', type: :request, content_type: :json dendend- context 'when deactivating (archiving) the project' do+ context 'when archiving the project (change active from true to false)' dolet(:body) do{active: false@@ -289,7 +285,7 @@ describe 'API v3 Project resource update', type: :request, content_type: :json dit 'responds with 200 OK' doexpect(last_response.status)- .to be(200)+ .to eq(200)endit 'archives the project' do@@ -303,10 +299,12 @@ describe 'API v3 Project resource update', type: :request, content_type: :json dendend- context 'for a non admin' do+ context 'for a user with only edit_project permission' do+ let(:permissions) { [:edit_project] }+it 'responds with 403' doexpect(last_response.status)- .to be(403)+ .to eq(403)endit 'does not alter the project' do@@ -314,5 +312,163 @@ describe 'API v3 Project resource update', type: :request, content_type: :json d.to be_truthyendend++ context 'for a user with only archive_project permission' do+ let(:permissions) { [:archive_project] }++ it 'responds with 200 OK' do+ expect(last_response.status)+ .to eq(200)+ end++ it 'archives the project' do+ expect(project.reload.active)+ .to be_falsey+ end+ end++ context 'for a user missing archive_project permission on child project' do+ let(:permissions) { [:archive_project] }+ let(:project) do+ create(:project).tap do |p|+ p.children << child_project+ end+ end+ let(:child_project) { create(:project) }++ it 'responds with 422 (and not 403?)' do+ expect(last_response.status)+ .to eq(422)+ end++ it 'does not alter the project' do+ expect(project.reload.active)+ .to be_truthy+ end+ end+ end++ context 'when setting a custom field and archiving the project' do+ let(:body) do+ {+ active: false,+ "customField#{custom_field.id}": {+ raw: "CF text"+ }+ }+ end++ context 'for an admin' do+ let(:current_user) do+ create(:admin)+ end+ let(:project) do+ create(:project).tap do |p|+ p.children << child_project+ end+ end+ let(:child_project) do+ create(:project)+ end++ it 'responds with 200 OK' do+ expect(last_response.status)+ .to eq(200)+ end++ it 'sets the cf value' do+ expect(project.reload.send("custom_field_#{custom_field.id}"))+ .to eql("CF text")+ 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 user with only edit_project permission' do+ let(:permissions) { [:edit_project] }++ it 'responds with 403' do+ expect(last_response.status)+ .to eq(403)+ end+ end++ context 'for a user with only archive_project permission' do+ let(:permissions) { [:archive_project] }++ it 'responds with 403' do+ expect(last_response.status)+ .to eq(403)+ end+ end++ context 'for a user with both archive_project and edit_project permissions' do+ let(:permissions) { %i[archive_project edit_project] }++ it 'responds with 200 OK' do+ expect(last_response.status)+ .to eq(200)+ end+ end+ end++ context 'when unarchiving the project (change active from false to true)' do+ let(:project_active) { false }+ let(:body) do+ {+ active: true+ }+ end++ context 'for an admin' do+ let(:current_user) do+ create(:admin)+ end+ let(:project) do+ create(:project).tap do |p|+ p.children << child_project+ end+ end+ let(:child_project) do+ create(:project)+ end++ it 'responds with 200 OK' do+ expect(last_response.status)+ .to eq(200)+ end++ it 'unarchives the project' do+ expect(project.reload)+ .to be_active+ end++ it 'unarchives the child project' do+ expect(child_project.reload)+ .to be_active+ end+ end++ context 'for a non-admin user, even with both archive_project and edit_project permissions' do+ let(:permissions) { %i[archive_project edit_project] }++ it 'responds with 404' do+ expect(last_response.status)+ .to eq(404)+ end++ it 'does not alter the project' do+ expect(project.reload)+ .not_to be_active+ end+ endendend