Project

General

Profile

Actions

Patch #39857

closed

Optimize users visibility check

Added by Pavel Rosický 11 months ago. Updated 11 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

diff --git a/app/models/principal.rb b/app/models/principal.rb
index 4cce97e26..25a79d768 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -48,8 +48,8 @@ class Principal < ActiveRecord::Base
       all
     else
       view_all_active = false
-      if user.memberships.to_a.any?
-        view_all_active = user.memberships.any? {|m| m.roles.any? {|r| r.users_visibility == 'all'}}
+      if user.memberships.any?
+        view_all_active = User.where(id: user.id).joins(memberships: :roles).where("#{Role.table_name}.users_visibility = ?", 'all').any?
       else
         view_all_active = user.builtin_role.users_visibility == 'all'
       end

in a bad scenario user.memberships.any? {|m| m.roles... is (n * memberships without permissions + 1 queries) which is very slow. Let the database do the job.
unfortunately in an ideal scenario (where the very first membership has the users_visibility == 'all') performance could be slightly worse, but I think it's an acceptable tradeoff

Actions

Also available in: Atom PDF