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

[#50910] enable 'all' operator for the user_action filter

Eric Schuberta81c14a6d971
diff --git a/app/models/queries/projects/filters/user_action_filter.rb b/app/models/queries/projects/filters/user_action_filter.rb
index e03206abfda..ebd0d7ef8f9 100644
--- a/app/models/queries/projects/filters/user_action_filter.rb
+++ b/app/models/queries/projects/filters/user_action_filter.rb
@@ -35,24 +35,39 @@ module Queries
end
def type
- :list
+ :list_all
end
def where
- capability_select = Capability
- .where(action: values)
- .where(principal: User.current)
- .reselect(:context_id)
+ operator = if operator_class <= ::Queries::Operators::Equals || operator_class <= ::Queries::Operators::All
+ 'IN'
+ elsif operator_class <= ::Queries::Operators::NotEquals
+ 'NOT IN'
+ else
+ raise ArgumentError
+ end
- sql_operator = if operator_class <= ::Queries::Operators::Equals
- "IN"
- elsif operator_class <= ::Queries::Operators::NotEquals
- "NOT IN"
- else
- raise ArgumentError
- end
+ capability_select_queries
+ .map { |query| "#{Project.table_name}.id #{operator} (#{query.to_sql})" }
+ .join(' AND ')
+ end
+
+ private
- "#{Project.table_name}.id #{sql_operator} (#{capability_select.to_sql})"
+ def capability_select_queries
+ if operator_class <= ::Queries::Operators::All
+ values.map do |val|
+ Capability
+ .where(action: val)
+ .where(principal: User.current)
+ .reselect(:context_id)
+ end
+ else
+ [Capability
+ .where(action: values)
+ .where(principal: User.current)
+ .reselect(:context_id)]
+ end
end
end
end
diff --git a/spec/models/queries/projects/filters/user_action_filter_spec.rb b/spec/models/queries/projects/filters/user_action_filter_spec.rb
index 75d54a19a1c..26697941c5c 100644
--- a/spec/models/queries/projects/filters/user_action_filter_spec.rb
+++ b/spec/models/queries/projects/filters/user_action_filter_spec.rb
@@ -31,7 +31,7 @@ require 'spec_helper'
RSpec.describe Queries::Projects::Filters::UserActionFilter do
it_behaves_like 'basic query filter' do
let(:class_key) { :user_action }
- let(:type) { :list }
+ let(:type) { :list_all }
let(:model) { Project }
let(:attribute) { :user_action }
let(:values) { ['projects/view'] }
diff --git a/spec/requests/api/v3/projects/index_resource_spec.rb b/spec/requests/api/v3/projects/index_resource_spec.rb
index 6da2d8732c4..b2ecd3cfdae 100644
--- a/spec/requests/api/v3/projects/index_resource_spec.rb
+++ b/spec/requests/api/v3/projects/index_resource_spec.rb
@@ -106,22 +106,38 @@ RSpec.describe 'API v3 Project resource index', content_type: :json do
end
context 'with filtering by capability action' do
- let(:other_project) do
- create(:project, members: [current_user])
+ let(:other_project) { create(:project) }
+ let(:another_project) { create(:project) }
+ let(:projects) { [project, other_project, another_project] }
+ let(:role) { create(:project_role, permissions: %i[copy_projects view_work_packages]) }
+ let(:other_role) { create(:project_role, permissions: %i[view_work_packages]) }
+ let(:another_role) { create(:project_role, permissions: []) }
+ let(:current_user) do
+ create(:user, member_with_roles: { project => role,
+ other_project => other_role,
+ another_project => another_role })
end
- let(:projects) { [project, other_project] }
- let(:role) { create(:project_role, permissions: [:copy_projects]) }
let(:get_path) do
- api_v3_paths.path_for :projects, filters: [{ user_action: { operator: "=", values: ["projects/copy"] } }]
+ api_v3_paths.path_for :projects, filters: [{ user_action: { operator:, values: %w[projects/copy work_packages/read] } }]
end
- it_behaves_like 'API V3 collection response', 1, 1, 'Project'
+ context 'if using the equals operator' do
+ let(:operator) { '=' }
- it 'returns the project the current user has the capability in' do
- expect(response.body)
- .to be_json_eql(api_v3_paths.project(project.id).to_json)
- .at_path('_embedded/elements/0/_links/self/href')
+ it_behaves_like 'API V3 collection response', 2, 2, 'Project'
+ end
+
+ context 'if using the all operator' do
+ let(:operator) { '*' }
+
+ it_behaves_like 'API V3 collection response', 1, 1, 'Project'
+
+ it 'returns the project the current user has the capabilities in' do
+ expect(response.body)
+ .to be_json_eql(api_v3_paths.project(project.id).to_json)
+ .at_path('_embedded/elements/0/_links/self/href')
+ end
end
end