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

Cache principal query directly in the capabilities scope

Dombi Attilac354528110ee
diff --git a/app/models/capabilities/scopes/default.rb b/app/models/capabilities/scopes/default.rb
index 68f84bba946..e39224dc3f3 100644
--- a/app/models/capabilities/scopes/default.rb
+++ b/app/models/capabilities/scopes/default.rb
@@ -70,7 +70,7 @@ module Capabilities::Scopes
ON members.id = member_roles.member_id
AND members.entity_type IS NULL
AND members.entity_id IS NULL
- JOIN (#{Principal.visible.not_builtin.not_locked.to_sql}) users
+ JOIN (#{principal_sql}) users
ON "users".id = members.user_id
LEFT OUTER JOIN "projects"
ON "projects".id = members.project_id
@@ -89,7 +89,7 @@ module Capabilities::Scopes
users.id principal_id,
projects.id context_id
FROM (#{Action.default.to_sql}) actions
- JOIN (#{Principal.visible.not_builtin.not_locked.to_sql}) users
+ JOIN (#{principal_sql}) users
ON "users".admin = true AND actions.grant_to_admin = true
LEFT OUTER JOIN "projects"
ON "projects".active = true
@@ -113,7 +113,7 @@ module Capabilities::Scopes
JOIN "roles"
ON ("roles".id = "role_permissions".role_id OR "actions"."public")
AND roles.builtin = #{Role::BUILTIN_NON_MEMBER}
- JOIN (#{Principal.visible.not_builtin.not_locked.to_sql}) users
+ JOIN (#{principal_sql}) users
ON 1 = 1
JOIN "projects"
ON "projects".active = true
@@ -155,6 +155,11 @@ module Capabilities::Scopes
WHERE enabled_modules.project_id IS NOT NULL OR "actions".module IS NULL
SQL_PART
end
+
+ def principal_sql
+ RequestStore[:capabilities_principal_sql] ||=
+ Principal.visible.not_builtin.not_locked.to_sql
+ end
end
end
end
diff --git a/app/models/principals/scopes/visible.rb b/app/models/principals/scopes/visible.rb
index 123d92262e1..077f425373c 100644
--- a/app/models/principals/scopes/visible.rb
+++ b/app/models/principals/scopes/visible.rb
@@ -36,14 +36,13 @@ module Principals::Scopes
class_methods do
def visible(user = ::User.current)
- RequestStore[:"principal_default_scope_#{user.id}"] ||=
- if user.allowed_in_any_project?(:manage_members) ||
- user.allowed_globally?(:manage_user) ||
- user.allowed_in_any_project?(:share_work_packages)
- all
- else
- in_visible_project_or_me(user)
- end
+ if user.allowed_in_any_project?(:manage_members) ||
+ user.allowed_globally?(:manage_user) ||
+ user.allowed_in_any_project?(:share_work_packages)
+ all
+ else
+ in_visible_project_or_me(user)
+ end
end
end
end