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

Improve performance of /api/v3/capabilities endpoint

Christophe Bliardfc5b44a80330
diff --git a/app/models/actions/scopes/default.rb b/app/models/actions/scopes/default.rb
index 070f4ded3c2..13c5ac4728c 100644
--- a/app/models/actions/scopes/default.rb
+++ b/app/models/actions/scopes/default.rb
@@ -32,13 +32,16 @@ module Actions::Scopes
class_methods do
def default
- actions_sql = <<~SQL.squish
- (SELECT id, permission, global, module, grant_to_admin
- FROM (VALUES #{action_map}) AS t(id, permission, global, module, grant_to_admin)) actions
- SQL
+ RequestStore[:action_default_scope] ||= begin
+ actions_sql = <<~SQL.squish
+ (SELECT id, permission, global, module, grant_to_admin
+ FROM (VALUES #{action_map}) AS t(id, permission, global, module, grant_to_admin)) actions
+ SQL
- select('actions.*')
- .from(actions_sql)
+ unscoped # prevent triggering the default scope again
+ .select('actions.*')
+ .from(actions_sql)
+ end
end
private
diff --git a/app/models/capabilities/scopes/default.rb b/app/models/capabilities/scopes/default.rb
index 7d0a07a5735..83701b52888 100644
--- a/app/models/capabilities/scopes/default.rb
+++ b/app/models/capabilities/scopes/default.rb
@@ -46,14 +46,15 @@ module Capabilities::Scopes
) capabilities
SQL
- select('capabilities.*')
+ unscoped # prevent triggering the default scope again
+ .select('capabilities.*')
.from(capabilities_sql)
end
private
def default_sql_by_member
- <<~SQL.squish
+ <<~SQL_PART
SELECT DISTINCT
actions.id "action",
users.id principal_id,
@@ -72,11 +73,11 @@ module Capabilities::Scopes
AND actions.module = enabled_modules.name
WHERE (projects.active AND (enabled_modules.project_id IS NOT NULL OR "actions".module IS NULL))
OR (projects.id IS NULL AND "actions".global)
- SQL
+ SQL_PART
end
def default_sql_by_admin
- <<~SQL.squish
+ <<~SQL_PART
SELECT DISTINCT
actions.id "action",
users.id principal_id,
@@ -91,11 +92,11 @@ module Capabilities::Scopes
ON enabled_modules.project_id = projects.id
AND actions.module = enabled_modules.name
WHERE (projects.id IS NOT NULL AND (enabled_modules.project_id IS NOT NULL OR "actions".module IS NULL)) OR "actions".global
- SQL
+ SQL_PART
end
def default_sql_by_non_member
- <<~SQL.squish
+ <<~SQL_PART
SELECT DISTINCT
actions.id "action",
users.id principal_id,
@@ -117,11 +118,11 @@ module Capabilities::Scopes
AND actions.module = enabled_modules.name
WHERE enabled_modules.project_id IS NOT NULL OR "actions".module IS NULL
- SQL
+ SQL_PART
end
def default_sql_by_non_member_with_anonymous
- <<~SQL.squish
+ <<~SQL_PART
SELECT DISTINCT
actions.id "action",
users.id principal_id,
@@ -138,7 +139,7 @@ module Capabilities::Scopes
AND actions.module = enabled_modules.name
WHERE enabled_modules.project_id IS NOT NULL OR "actions".module IS NULL
- SQL
+ SQL_PART
end
end
end