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

[36026] Ensure project filter only searches for active projects

Oliver Günther4d17b197835f
diff --git a/app/models/queries/work_packages/filter/project_filter.rb b/app/models/queries/work_packages/filter/project_filter.rb
index c64cca1c396..c858bc810fd 100644
--- a/app/models/queries/work_packages/filter/project_filter.rb
+++ b/app/models/queries/work_packages/filter/project_filter.rb
@@ -68,6 +68,6 @@ class Queries::WorkPackages::Filter::ProjectFilter < Queries::WorkPackages::Filt
private
def visible_projects
- @visible_projects ||= Project.visible
+ @visible_projects ||= Project.visible.active
end
end
diff --git a/lib/api/v3/queries/schemas/project_filter_dependency_representer.rb b/lib/api/v3/queries/schemas/project_filter_dependency_representer.rb
index b8b768d9257..398ad22e638 100644
--- a/lib/api/v3/queries/schemas/project_filter_dependency_representer.rb
+++ b/lib/api/v3/queries/schemas/project_filter_dependency_representer.rb
@@ -35,7 +35,10 @@ module API
FilterDependencyRepresenter
def href_callback
- api_v3_paths.projects
+ params = [active: { operator: '=', values: ['t'] }]
+ escaped = CGI.escape(::JSON.dump(params))
+
+ "#{api_v3_paths.projects}?filters=#{escaped}"
end
def type
diff --git a/spec/lib/api/v3/queries/schemas/project_filter_dependency_representer_spec.rb b/spec/lib/api/v3/queries/schemas/project_filter_dependency_representer_spec.rb
index 93ce5b6a035..9371d24cd6f 100644
--- a/spec/lib/api/v3/queries/schemas/project_filter_dependency_representer_spec.rb
+++ b/spec/lib/api/v3/queries/schemas/project_filter_dependency_representer_spec.rb
@@ -47,7 +47,8 @@ describe ::API::V3::Queries::Schemas::ProjectFilterDependencyRepresenter, clear_
describe 'values' do
let(:path) { 'values' }
let(:type) { '[]Project' }
- let(:href) { api_v3_paths.projects }
+ let(:filters) { "?filters=%5B%7B%22active%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%22t%22%5D%7D%7D%5D" }
+ let(:href) { api_v3_paths.projects + filters}
context "for operator 'Queries::Operators::Equals'" do
let(:operator) { Queries::Operators::Equals }
diff --git a/spec/models/queries/work_packages/filter/project_filter_instance_spec.rb b/spec/models/queries/work_packages/filter/project_filter_instance_spec.rb
new file mode 100644
index 00000000000..a1427c31ecb
--- /dev/null
+++ b/spec/models/queries/work_packages/filter/project_filter_instance_spec.rb
@@ -0,0 +1,53 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe Queries::WorkPackages::Filter::ProjectFilter, type: :model do
+ let(:query) { FactoryBot.build :query }
+ let(:instance) do
+ described_class.create!(name: 'project', context: query, operator: '=', values: [])
+ end
+
+ describe '#allowed_values' do
+ let!(:project) { FactoryBot.create :project }
+ let!(:archived_project) { FactoryBot.create :project, active: false }
+
+ let(:user) { FactoryBot.create(:user, member_in_projects: [project, archived_project], member_through_role: role) }
+ let(:role) { FactoryBot.create :role, permissions: %i(view_work_packages) }
+
+ before do
+ login_as user
+ end
+
+ it 'does not include the archived project (Regression #36026)' do
+ expect(instance.allowed_values)
+ .to match_array [[project.name, project.id.to_s]]
+ end
+ end
+end
diff --git a/spec/models/queries/work_packages/filter/project_filter_spec.rb b/spec/models/queries/work_packages/filter/project_filter_spec.rb
index 3864298a570..da33b2b003a 100644
--- a/spec/models/queries/work_packages/filter/project_filter_spec.rb
+++ b/spec/models/queries/work_packages/filter/project_filter_spec.rb
@@ -45,7 +45,7 @@ describe Queries::WorkPackages::Filter::ProjectFilter, type: :model do
it 'is true if the user can see project' do
allow(Project)
- .to receive_message_chain(:visible, :exists?)
+ .to receive_message_chain(:visible, :active, :exists?)
.and_return(true)
expect(instance).to be_available
@@ -53,7 +53,7 @@ describe Queries::WorkPackages::Filter::ProjectFilter, type: :model do
it 'is true if the user can not see project' do
allow(Project)
- .to receive_message_chain(:visible, :exists?)
+ .to receive_message_chain(:visible, :active, :exists?)
.and_return(false)
expect(instance).to_not be_available
@@ -71,7 +71,7 @@ describe Queries::WorkPackages::Filter::ProjectFilter, type: :model do
visible_projects = [parent, child]
allow(Project)
- .to receive(:visible)
+ .to receive_message_chain(:visible, :active)
.and_return(visible_projects)
allow(Project)
@@ -99,7 +99,7 @@ describe Queries::WorkPackages::Filter::ProjectFilter, type: :model do
before do
allow(Project)
- .to receive(:visible)
+ .to receive_message_chain(:visible, :active)
.and_return([project, project2])
instance.values = [project.id.to_s]