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

[#36559] always pass `current_user` to a query

Tobias Dillmann19e9f1bba8c1
diff --git a/app/models/query.rb b/app/models/query.rb
index 3d5ab4097e0..0d7ac379e64 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -51,6 +51,8 @@ class Query < ApplicationRecord
serialize :column_names, type: Array
serialize :sort_criteria, type: Array
+ attr_accessor :current_user
+
validates :include_subprojects,
inclusion: [true, false]
@@ -67,8 +69,9 @@ class Query < ApplicationRecord
scope(:global, -> { where(project_id: nil) })
- def self.new_default(attributes = nil)
+ def self.new_default(attributes = nil, current_user = nil)
new(attributes).tap do |query|
+ query.current_user = current_user
query.add_default_filter
query.set_default_sort
query.show_hierarchies = true
@@ -218,7 +221,7 @@ class Query < ApplicationRecord
end
@available_columns_project = project&.cache_key || 0
- @available_columns = ::Query.available_columns(project, user)
+ @available_columns = ::Query.available_columns(project, current_user)
end
def self.available_columns(project = nil, current_user = nil)
@@ -331,10 +334,11 @@ class Query < ApplicationRecord
def sort_criteria_columns
sort_criteria
- .map do |attribute, direction|
+ .filter_map do |attribute, direction|
attribute = attribute.to_sym
+ col = sort_criteria_column(attribute)
- [sort_criteria_column(attribute), direction]
+ [col, direction] if col
end
end
diff --git a/lib/api/v3/queries/create_form_api.rb b/lib/api/v3/queries/create_form_api.rb
index 35a737940fd..050632724ff 100644
--- a/lib/api/v3/queries/create_form_api.rb
+++ b/lib/api/v3/queries/create_form_api.rb
@@ -38,8 +38,7 @@ module API
helpers ::API::V3::Queries::QueryHelper
post do
- # TODO: is it wrong to pass the user here? A query belongs to a user, but that is the user that created the query?
- create_or_update_query_form Query.new_default(user: current_user), ::Queries::CreateContract, CreateFormRepresenter
+ create_or_update_query_form Query.new_default({}, current_user), ::Queries::CreateContract, CreateFormRepresenter
end
end
end
diff --git a/lib/api/v3/queries/update_form_api.rb b/lib/api/v3/queries/update_form_api.rb
index af04dd5af45..ac3c5d0c3a6 100644
--- a/lib/api/v3/queries/update_form_api.rb
+++ b/lib/api/v3/queries/update_form_api.rb
@@ -36,6 +36,8 @@ module API
helpers ::API::V3::Queries::QueryHelper
post do
+ @query.current_user = current_user
+
# We try to ignore invalid aspects of the query as the user
# might not even be able to fix them (public query)
# and because they might only be invalid in his context