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
valid_subset! for all queries
ulferts4194a35b5752
diff --git a/app/components/filters_component.html.erb b/app/components/filters_component.html.erbindex f17e1568549..7c83b1b41d3 100644--- a/app/components/filters_component.html.erb+++ b/app/components/filters_component.html.erb@@ -9,7 +9,11 @@'action': 'filters#toggleDisplayFilters','test-selector': 'filter-component-toggle' })) do %><%= t(:label_filter) %>- <%= render(Primer::Beta::Counter.new(count: filters_count, round: true, hide_if_zero: false, scheme: :default)) %>+ <%= render(Primer::Beta::Counter.new(count: filters_count,+ round: true,+ hide_if_zero: false,+ scheme: :default,+ data: { "test-selector": "filters-button-counter" })) %><% end %><div class="op-filters-header-actions"><% buttons.each do |button| %>diff --git a/app/models/queries/base_query.rb b/app/models/queries/base_query.rbindex e7c5b6bbc50..bb1ef1f7b36 100644--- a/app/models/queries/base_query.rb+++ b/app/models/queries/base_query.rb@@ -34,6 +34,7 @@ module Queries::BaseQueryinclude Queries::Selects::AvailableSelectsinclude Queries::Orders::AvailableOrdersinclude Queries::GroupBys::AvailableGroupBys+ include Queries::ValidSubsetinclude ActiveModel::Validationsvalidate :filters_valid,diff --git a/app/models/queries/projects/factory.rb b/app/models/queries/projects/factory.rbindex 45a76461820..a96e4ce64a9 100644--- a/app/models/queries/projects/factory.rb+++ b/app/models/queries/projects/factory.rb@@ -36,7 +36,10 @@ class Queries::Projects::Factoryclass << selfdef find(id, params:, user:)- find_and_update_static_query(id, params, user) || find_and_update_persisted_query(id, params, user)+ query = find_and_update_static_query(id, params, user) || find_and_update_persisted_query(id, params, user)+ query&.valid_subset!++ queryenddef static_query(id)diff --git a/app/models/queries/projects/filters/created_at_filter.rb b/app/models/queries/projects/filters/created_at_filter.rbindex 7355b4595b4..b956e63d1b5 100644--- a/app/models/queries/projects/filters/created_at_filter.rb+++ b/app/models/queries/projects/filters/created_at_filter.rb@@ -30,4 +30,10 @@ class Queries::Projects::Filters::CreatedAtFilter < Queries::Projects::Filters::def type:datetime_pastend++ def available?+ # The column is only available for admins.+ # When the filter is used, one could get the same information as with the column.+ User.current.admin?+ endenddiff --git a/app/models/queries/projects/filters/type_filter.rb b/app/models/queries/projects/filters/type_filter.rbindex f8874cbf26a..e642d108d66 100644--- a/app/models/queries/projects/filters/type_filter.rb+++ b/app/models/queries/projects/filters/type_filter.rb@@ -54,7 +54,7 @@ module Queriesdef type_strategy# Instead of getting the IDs of all the projects a user is allowed- # to see we only check that the value is an integer. Non valid ids+ # to see we only check that the value is an integer. Non valid ids# will then simply create an empty result but will not cause any# harm.@type_strategy ||= ::Queries::Filters::Strategies::IntegerList.new(self)diff --git a/app/models/queries/selects/not_existing_select.rb b/app/models/queries/selects/not_existing_select.rbindex 2f1e5c3978a..f5c6bd3c1a0 100644--- a/app/models/queries/selects/not_existing_select.rb+++ b/app/models/queries/selects/not_existing_select.rb@@ -39,6 +39,10 @@ module QueriesI18n.t("activerecord.errors.messages.does_not_exist")end+ def available?+ false+ end+privatedef always_falsediff --git a/app/models/queries/valid_subset.rb b/app/models/queries/valid_subset.rbnew file mode 100644index 00000000000..4f434ef2ffc--- /dev/null+++ b/app/models/queries/valid_subset.rb@@ -0,0 +1,50 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2024 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 Queries+ module ValidSubset+ extend ActiveSupport::Concern++ def valid_subset!+ valid_filters_subset!+ valid_selects_subset!+ end++ private++ def valid_filters_subset!+ filters.each(&:valid_values!).select! do |filter|+ filter.available? && filter.valid?+ end+ end++ def valid_selects_subset!+ selects.select!(&:available?)+ end+ end+enddiff --git a/spec/features/projects/persisted_lists_spec.rb b/spec/features/projects/persisted_lists_spec.rbindex ad99676c78d..fe08ff52208 100644--- a/spec/features/projects/persisted_lists_spec.rb+++ b/spec/features/projects/persisted_lists_spec.rb@@ -271,7 +271,7 @@ RSpec.describe "Persisted lists on projects index page",endend- describe "persisted filters" do+ describe "persisted query access" docurrent_user { user }let(:another_project) do@@ -401,4 +401,36 @@ RSpec.describe "Persisted lists on projects index page",.to have_text("You are not authorized to access this page.")endend++ describe "persisted query access on invalid query" do+ current_user { user }++ let!(:project_member) { create(:member, principal: user, project:, roles: [developer]) }++ let!(:invalid_list) do+ # Faking a query that has references stored to a custom field that no longer exists (e.g. has been deleted)+ create(:project_query, name: "My projects list", user:, select: %w[name created_at cf_1]) do |query|+ query.where("member_of", "=", OpenProject::Database::DB_VALUE_TRUE)+ query.where("cf_1", "=", 1)+ query.where("created_at", "=", "2020-01-01")++ query.save(validate: false)+ end+ end++ it "still shows the query falling back to a valid subset" do+ visit projects_path(query_id: invalid_list.id)++ # Keeps only the 'Name' column as the cf does not exist and Created on is admin only.+ projects_page.expect_columns "Name"+ projects_page.expect_no_columns "Created on"++ # Keeps only the 'I am member' filter as the cf does not exist and created_at is admin only.+ projects_page.expect_filter_count 1+ projects_page.expect_filter_set("member_of")++ # The query is still valid, therefore it is executed, and returns the project the user is member in.+ projects_page.expect_projects_listed(project)+ end+ endenddiff --git a/spec/models/queries/projects/factory_spec.rb b/spec/models/queries/projects/factory_spec.rbindex e43930ea664..4f0e2ad7c93 100644--- a/spec/models/queries/projects/factory_spec.rb+++ b/spec/models/queries/projects/factory_spec.rb@@ -338,7 +338,7 @@ RSpec.describe Queries::Projects::Factory, with_settings: { enabled_projects_coldirection: "desc"}],- selects: %w[created_at name]+ selects: %w[description name]}end@@ -364,7 +364,7 @@ RSpec.describe Queries::Projects::Factory, with_settings: { enabled_projects_colit "has the selects" doexpect(find.selects.map(&:attribute))- .to eq(%i[created_at name])+ .to eq(%i[description name])endend@@ -460,7 +460,7 @@ RSpec.describe Queries::Projects::Factory, with_settings: { enabled_projects_collet(:id) { nil }let(:params) do{- selects: %w[created_at project_status]+ selects: %w[description project_status]}end@@ -486,7 +486,7 @@ RSpec.describe Queries::Projects::Factory, with_settings: { enabled_projects_colit "has the selects overwritten" doexpect(find.selects.map(&:attribute))- .to eq(%i[created_at project_status])+ .to eq(%i[description project_status])endend@@ -582,7 +582,7 @@ RSpec.describe Queries::Projects::Factory, with_settings: { enabled_projects_collet(:id) { 42 }let(:params) do{- selects: %w[created_at project_status]+ selects: %w[description project_status]}end@@ -608,7 +608,7 @@ RSpec.describe Queries::Projects::Factory, with_settings: { enabled_projects_colit "has the selects specified by the params" doexpect(find.selects.map(&:attribute))- .to eq(%i[created_at project_status])+ .to eq(%i[description project_status])endenddiff --git a/spec/models/queries/projects/filters/created_at_filter_spec.rb b/spec/models/queries/projects/filters/created_at_filter_spec.rbindex 0e3a6068b24..9fc86c234b4 100644--- a/spec/models/queries/projects/filters/created_at_filter_spec.rb+++ b/spec/models/queries/projects/filters/created_at_filter_spec.rb@@ -55,9 +55,9 @@ RSpec.describe Queries::Projects::Filters::CreatedAtFilter dologin_as userend- it "is true" do+ it "is false" doexpect(instance)- .to be_available+ .not_to be_availableendendenddiff --git a/spec/models/queries/projects/project_query_spec.rb b/spec/models/queries/projects/project_query_spec.rbindex 513a00c52dd..ed541b95f78 100644--- a/spec/models/queries/projects/project_query_spec.rb+++ b/spec/models/queries/projects/project_query_spec.rb@@ -149,4 +149,179 @@ RSpec.describe Queries::Projects::ProjectQuery do# rubocop:enable Naming/VariableNumberendend++ describe "#valid_subset!" do+ context "with filters" do+ let(:valid_project_ids) do+ project_scope = instance_double(ActiveRecord::Relation)++ allow(Project)+ .to receive(:visible)+ .and_return(project_scope)++ allow(project_scope)+ .to receive(:pluck)+ .with(:id)+ .and_return([1, 2, 3])++ [1, 2, 3]+ end++ context "with them being valid" do+ current_user { admin }++ before do+ instance.where("active", "=", OpenProject::Database::DB_VALUE_TRUE)+ instance.where("member_of", "=", OpenProject::Database::DB_VALUE_TRUE)+ instance.where("created_at", ">t-", ["6"])++ instance.valid_subset!+ end++ it "leaves the values untouched" do+ expect(instance.filters.map { |filter| [filter.field, filter.operator, filter.values] })+ .to eq([[:active, "=", ["t"]], [:member_of, "=", ["t"]], [:created_at, ">t-", ["6"]]])+ end+ end++ context "with one of them being invalid as it is a made up filter name" do+ current_user { user }++ before do+ instance.where("active", "=", OpenProject::Database::DB_VALUE_TRUE)+ instance.where("bogus", ">t-", ["6"])++ instance.valid_subset!+ end++ it "leaves only the valid filters" do+ expect(instance.filters.map { |filter| [filter.field, filter.operator, filter.values] })+ .to eq([[:active, "=", ["t"]]])+ end+ end++ context "with one of them being invalid as it is admin only" do+ current_user { user }++ before do+ instance.where("active", "=", OpenProject::Database::DB_VALUE_TRUE)+ instance.where("created_at", ">t-", ["6"])++ instance.valid_subset!+ end++ it "leaves only the valid filters" do+ expect(instance.filters.map { |filter| [filter.field, filter.operator, filter.values] })+ .to eq([[:active, "=", ["t"]]])+ end+ end++ context "with one of them being invalid as it has one invalid value" do+ current_user { user }++ before do+ instance.where("active", "=", OpenProject::Database::DB_VALUE_TRUE)+ instance.where("parent_id", "=", valid_project_ids.map(&:to_s) + %w[42])++ instance.valid_subset!+ end++ it "keeps the invalid filter but removes the invalid value" do+ expect(instance.filters.map { |filter| [filter.field, filter.operator, filter.values] })+ .to eq([[:active, "=", ["t"]], [:parent_id, "=", valid_project_ids.map(&:to_s)]])+ end+ end++ context "with one of them being invalid as it has only invalid values" do+ current_user { user }++ before do+ instance.where("active", "=", OpenProject::Database::DB_VALUE_TRUE)+ valid_project_ids+ instance.where("parent_id", "=", %w[23 42])++ instance.valid_subset!+ end++ it "keeps only the valid filters" do+ expect(instance.filters.map { |filter| [filter.field, filter.operator, filter.values] })+ .to eq([[:active, "=", ["t"]]])+ end+ end++ context "with one of them being invalid as it has an invalid operator" do+ current_user { user }++ before do+ instance.where("active", "=", OpenProject::Database::DB_VALUE_TRUE)+ instance.where("parent_id", "d=", valid_project_ids.map(&:to_s))++ instance.valid_subset!+ end++ it "keeps only the valid filters" do+ expect(instance.filters.map { |filter| [filter.field, filter.operator, filter.values] })+ .to eq([[:active, "=", ["t"]]])+ end+ end++ context "with all of them being invalid" do+ current_user { user }++ before do+ instance.where("created_at", "=", OpenProject::Database::DB_VALUE_TRUE)+ instance.where("parent_id", "d=", valid_project_ids.map(&:to_s))++ instance.valid_subset!+ end++ it "keeps only the valid filters" do+ expect(instance.filters.map { |filter| [filter.field, filter.operator, filter.values] })+ .to be_empty+ end+ end+ end++ context "with selects" do+ let(:current_user) { user }++ current_user { user }++ before do+ instance.select(*columns)++ instance.valid_subset!+ end++ context "with them being valid" do+ let(:current_user) { admin }+ let(:columns) { %i(name description created_at) }++ it "leaves the values untouched" do+ expect(instance.selects.map(&:attribute))+ .to eq columns+ end+ end++ context "with them being invalid" do+ # No admin, hence no created_at column. CF column does not exist.+ let(:columns) { %i(bogus created_at cf_1) } # rubocop:disable Naming/VariableNumber++ it "removes the values" do+ expect(instance.selects.map(&:attribute))+ .to be_empty+ end+ end++ context "with them being partially invalid" do+ let(:current_user) { admin }+ let(:columns) { %i(bogus name created_at cf_1 description) } # rubocop:disable Naming/VariableNumber++ it "removes only the offending values" do+ expect(instance.selects.map(&:attribute))+ .to eq %i(name created_at description)+ end+ end+ end+ endenddiff --git a/spec/support/pages/projects/index.rb b/spec/support/pages/projects/index.rbindex 90c541f239c..a7359e4f6bf 100644--- a/spec/support/pages/projects/index.rb+++ b/spec/support/pages/projects/index.rb@@ -120,6 +120,11 @@ module Pagesvisible: :hidden)end+ def expect_filter_count(count)+ expect(page)+ .to have_css('[data-test-selector="filters-button-counter"]', text: count)+ end+def expect_no_project_create_buttonexpect(page).to have_no_css('[data-test-selector="project-new-button"]')end