]> source.dussan.org Git - redmine.git/commitdiff
Don't preload projects and roles on Principal#memberships association (#23519).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 7 Jan 2017 11:40:09 +0000 (11:40 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 7 Jan 2017 11:40:09 +0000 (11:40 +0000)
Patch by OndÅ™ej Ezr.

git-svn-id: http://svn.redmine.org/redmine/trunk@16153 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/users_controller.rb
app/models/principal.rb
app/models/user.rb
app/views/groups/show.api.rsb

index 3e4feb8a45e9b95730c658e61992d3fb61ab6f2c..60600a2528b6063a532bef2d9c9b21d360da28f5 100644 (file)
@@ -69,7 +69,7 @@ class UsersController < ApplicationController
     end
 
     # show projects based on current user visibility
-    @memberships = @user.memberships.where(Project.visible_condition(User.current)).to_a
+    @memberships = @user.memberships.preload(:roles, :project).where(Project.visible_condition(User.current)).to_a
 
     respond_to do |format|
       format.html {
index 15cf6996145ffd41682a499bae771453d5e29ae5..a1f5156bfbe91c1620d39ba1d2d67da45b26f7f0 100644 (file)
@@ -28,8 +28,7 @@ class Principal < ActiveRecord::Base
 
   has_many :members, :foreign_key => 'user_id', :dependent => :destroy
   has_many :memberships,
-           lambda {preload(:project, :roles).
-                   joins(:project).
+           lambda {joins(:project).
                    where("#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}")},
            :class_name => 'Member',
            :foreign_key => 'user_id'
index 9192032f03dbc26e7e45f1709dc135c85417f974..53e0ab4d5bfe2a73fb1378c96c95c283141ee0d3 100644 (file)
@@ -546,6 +546,10 @@ class User < Principal
     @membership_by_project_id[project_id]
   end
 
+  def roles
+    @roles ||= Role.joins(members: :project).where(["#{Project.table_name}.status <> ?", Project::STATUS_ARCHIVED]).where(Member.arel_table[:user_id].eq(id)).uniq
+  end
+
   # Returns the user's bult-in role
   def builtin_role
     @builtin_role ||= Role.non_member
@@ -673,9 +677,9 @@ class User < Principal
       return true if admin?
 
       # authorize if user has at least one role that has this permission
-      roles = memberships.collect {|m| m.roles}.flatten.uniq
-      roles << (self.logged? ? Role.non_member : Role.anonymous)
-      roles.any? {|role|
+      rls = self.roles.to_a
+      rls << builtin_role
+      rls.any? {|role|
         role.allowed_to?(action) &&
         (block_given? ? yield(role, self) : true)
       }
index 15211f2cf66e86d7fc03746e9242029d775c7afa..db9dadbd472ac5cdfcad2707f87df133a9cd1269 100644 (file)
@@ -12,7 +12,7 @@ api.group do
   end if include_in_api_response?('users') && !@group.builtin?
 
   api.array :memberships do
-    @group.memberships.each do |membership|
+    @group.memberships.preload(:roles, :project).each do |membership|
       api.membership do
         api.id membership.id
         api.project :id => membership.project.id, :name => membership.project.name
@@ -22,7 +22,7 @@ api.group do
               attrs = {:id => member_role.role.id, :name => member_role.role.name}
               attrs.merge!(:inherited => true) if member_role.inherited_from.present?
               api.role attrs
-            end 
+            end
           end
         end
       end if membership.project