From ce8113dea2a73eff5286bfbc31a2042693943471 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 2 Jan 2011 11:38:35 +0000 Subject: [PATCH] Additional roles of a user who belongs to a group are not always copied when copying the project (#7213). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4615 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/project.rb | 7 ++++++- test/unit/project_test.rb | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index 4af77d1d..891db8b8 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -740,7 +740,12 @@ class Project < ActiveRecord::Base # Copies members from +project+ def copy_members(project) - project.memberships.each do |member| + # Copy users first, then groups to handle members with inherited and given roles + members_to_copy = [] + members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)} + members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)} + + members_to_copy.each do |member| new_member = Member.new new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on") # only copy non inherited roles diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index 61ae3be3..8eb79fda 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -770,6 +770,22 @@ class ProjectTest < ActiveSupport::TestCase assert_equal @project, membership.project end end + + should "copy memberships with groups and additional roles" do + group = Group.create!(:lastname => "Copy group") + user = User.find(7) + group.users << user + # group role + Member.create!(:project_id => @source_project.id, :principal => group, :role_ids => [2]) + member = Member.find_by_user_id_and_project_id(user.id, @source_project.id) + # additional role + member.role_ids = [1] + + assert @project.copy(@source_project) + member = Member.find_by_user_id_and_project_id(user.id, @project.id) + assert_not_nil member + assert_equal [1, 2], member.role_ids.sort + end should "copy project specific queries" do assert @project.valid?