From: Jean-Philippe Lang Date: Sat, 7 Jan 2017 11:40:09 +0000 (+0000) Subject: Don't preload projects and roles on Principal#memberships association (#23519). X-Git-Tag: 3.4.0~453 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3deb70d4aab992c00b5048630033d6614aea97c1;p=redmine.git Don't preload projects and roles on Principal#memberships association (#23519). Patch by Ondřej Ezr. git-svn-id: http://svn.redmine.org/redmine/trunk@16153 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3e4feb8a4..60600a252 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 { diff --git a/app/models/principal.rb b/app/models/principal.rb index 15cf69961..a1f5156bf 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -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' diff --git a/app/models/user.rb b/app/models/user.rb index 9192032f0..53e0ab4d5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) } diff --git a/app/views/groups/show.api.rsb b/app/views/groups/show.api.rsb index 15211f2cf..db9dadbd4 100644 --- a/app/views/groups/show.api.rsb +++ b/app/views/groups/show.api.rsb @@ -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