diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-01-07 11:40:09 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-01-07 11:40:09 +0000 |
commit | 3deb70d4aab992c00b5048630033d6614aea97c1 (patch) | |
tree | 6afa66b87956fafdc7a5f1f77c32094edcd27d5a /app | |
parent | 79c2e42889913a2f4ec583b4bbc9fbe256d92e7e (diff) | |
download | redmine-3deb70d4aab992c00b5048630033d6614aea97c1.tar.gz redmine-3deb70d4aab992c00b5048630033d6614aea97c1.zip |
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
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/users_controller.rb | 2 | ||||
-rw-r--r-- | app/models/principal.rb | 3 | ||||
-rw-r--r-- | app/models/user.rb | 10 | ||||
-rw-r--r-- | app/views/groups/show.api.rsb | 4 |
4 files changed, 11 insertions, 8 deletions
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 |