Commit 96c31c34 authored by jplang's avatar jplang

Don't preload projects and roles on Principal#memberships association (#23519).

Patch by Ondřej Ezr.

git-svn-id: https://svn.redmine.org/redmine/trunk@16153 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 7bf3a478
......@@ -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 {
......
......@@ -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'
......
......@@ -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)
}
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment