mirror of
https://github.com/meineerde/redmine.git
synced 2026-02-01 03:57:15 +00:00
Fixed User#project_ids_by_role when first called within chained scopes (#26376).
git-svn-id: http://svn.redmine.org/redmine/trunk@16767 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
d2cc2f9dca
commit
92ca57e938
@ -603,36 +603,40 @@ class User < Principal
|
||||
# Includes the projects that the user is a member of and the projects
|
||||
# that grant custom permissions to the builtin groups.
|
||||
def project_ids_by_role
|
||||
return @project_ids_by_role if @project_ids_by_role
|
||||
|
||||
group_class = anonymous? ? GroupAnonymous : GroupNonMember
|
||||
group_id = group_class.pluck(:id).first
|
||||
|
||||
members = Member.joins(:project, :member_roles).
|
||||
where("#{Project.table_name}.status <> 9").
|
||||
where("#{Member.table_name}.user_id = ? OR (#{Project.table_name}.is_public = ? AND #{Member.table_name}.user_id = ?)", self.id, true, group_id).
|
||||
pluck(:user_id, :role_id, :project_id)
|
||||
|
||||
hash = {}
|
||||
members.each do |user_id, role_id, project_id|
|
||||
# Ignore the roles of the builtin group if the user is a member of the project
|
||||
next if user_id != id && project_ids.include?(project_id)
|
||||
|
||||
hash[role_id] ||= []
|
||||
hash[role_id] << project_id
|
||||
end
|
||||
|
||||
result = Hash.new([])
|
||||
if hash.present?
|
||||
roles = Role.where(:id => hash.keys).to_a
|
||||
hash.each do |role_id, proj_ids|
|
||||
role = roles.detect {|r| r.id == role_id}
|
||||
if role
|
||||
result[role] = proj_ids.uniq
|
||||
# Clear project condition for when called from chained scopes
|
||||
# eg. project.children.visible(user)
|
||||
Project.unscoped do
|
||||
return @project_ids_by_role if @project_ids_by_role
|
||||
|
||||
group_class = anonymous? ? GroupAnonymous : GroupNonMember
|
||||
group_id = group_class.pluck(:id).first
|
||||
|
||||
members = Member.joins(:project, :member_roles).
|
||||
where("#{Project.table_name}.status <> 9").
|
||||
where("#{Member.table_name}.user_id = ? OR (#{Project.table_name}.is_public = ? AND #{Member.table_name}.user_id = ?)", self.id, true, group_id).
|
||||
pluck(:user_id, :role_id, :project_id)
|
||||
|
||||
hash = {}
|
||||
members.each do |user_id, role_id, project_id|
|
||||
# Ignore the roles of the builtin group if the user is a member of the project
|
||||
next if user_id != id && project_ids.include?(project_id)
|
||||
|
||||
hash[role_id] ||= []
|
||||
hash[role_id] << project_id
|
||||
end
|
||||
|
||||
result = Hash.new([])
|
||||
if hash.present?
|
||||
roles = Role.where(:id => hash.keys).to_a
|
||||
hash.each do |role_id, proj_ids|
|
||||
role = roles.detect {|r| r.id == role_id}
|
||||
if role
|
||||
result[role] = proj_ids.uniq
|
||||
end
|
||||
end
|
||||
end
|
||||
@project_ids_by_role = result
|
||||
end
|
||||
@project_ids_by_role = result
|
||||
end
|
||||
|
||||
# Returns the ids of visible projects
|
||||
|
||||
@ -952,6 +952,14 @@ class UserTest < ActiveSupport::TestCase
|
||||
assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
|
||||
end
|
||||
|
||||
def test_project_ids_by_role_should_not_poison_cache_when_first_called_from_chained_scopes
|
||||
user = User.find(2)
|
||||
project = Project.find(1)
|
||||
|
||||
project.children.visible(user)
|
||||
assert_equal [1, 2, 5], user.project_ids_by_role.values.flatten.sort
|
||||
end
|
||||
|
||||
def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
|
||||
user = User.find(2)
|
||||
assert_equal [], user.projects_by_role[Role.find(3)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user