lib/rally_user_helper.rb
# Mirrors team membership set from a source user to a target user
Mirrors team membership set from a source user to a target user
Mirrors team membership set from a source user to a target user
def sync_team_memberships(source_user_id, target_user_id)
      source_user = find_user(source_user_id)
      target_user = find_user(target_user_id)
      if source_user.nil? then
        @logger.warn "  Source user: #{source_user_id} Not found. Skipping sync of permissions to #{target_user_id}."
        return
      elsif target_user.nil then
        @logger.warn "  Target user: #{target_user_id} Not found. Skipping sync of permissions for #{target_user_id}."
      end
      memberships_existing = target_user["TeamMemberships"]
      source_memberships = source_user["TeamMemberships"]
      source_membership_oids = []
      source_memberships.each do | this_source_membership |
        source_membership_oids.push(get_membership_oid_from_membership(this_source_membership))
      end
      memberships_existing_oids = []
      memberships_existing.each do | this_membership |
        memberships_existing_oids.push(get_membership_oid_from_membership(this_membership))
      end
      permissions_existing = target_user.UserPermissions
      permissions_existing_by_project = {}
      permissions_existing.each do | this_permission |
        if this_permission._type == "ProjectPermission" then
          permissions_existing_by_project[this_permission.Project.ObjectID.to_s] = this_permission
        end
      end
      memberships_to_add = []
      source_membership_oids.each do | this_membership_oid |
        if !memberships_existing_oids.include?(this_membership_oid) then
          memberships_to_add.push(this_membership_oid)
        end
      end
      memberships_to_remove = []
      memberships_existing_oids.each do | this_membership_oid |
        if !source_membership_oids.include?(this_membership_oid) then
          memberships_to_remove.push(this_membership_oid)
        end
      end
      number_memberships_added = 0
      memberships_to_add.each do | this_membership_oid |
        this_permission = permissions_existing_by_project[this_membership_oid]
        if !this_permission.nil? then
          this_role = this_permission.Role
          if !this_role.eql?(EDITOR) && !this_role.eql(PROJECTADMIN) then
            @logger.warn "  Target User: #{target_user_id} must be an Editor or Project Admin to make them a Team Member. Skipping."
            return
          end
          this_project = find_project(this_membership_oid)
          if !this_project.nil? then
            number_memberships_added += 1
            this_project_name = this_project["Name"]
            @logger.info "Updating TeamMembership on #{this_project_name} from #{source_user_id} to: #{target_user_id}."
            update_team_membership(target_user, this_membership_oid, this_project_name, TEAMMEMBER_YES)
          end
        end
      end
      number_memberships_removed = 0
      memberships_to_remove.each do | this_membership_oid |
        this_project = find_project(this_membership_oid)
        if !this_project.nil? then
          number_memberships_removed += 1
          this_project_name = this_project["Name"]
          @logger.info "Removing TeamMembership on #{this_project_name} from #{target_user_id} since source: #{source_user_id} is not a TeamMember."
          update_team_membership(target_user, this_membership_oid, this_project_name, TEAMMEMBER_NO)
        end
      end
      @logger.info "Team Memberships Added: #{number_memberships_added}; Team Memberships Removed: #{number_memberships_removed}"
    end
def sync_team_memberships ( source_user_id , target_user_id ) source_user = find_user ( source_user_id ) target_user = find_user ( target_user_id ) if source_user . nil? then @logger . warn "  Source user: #{source_user_id} Not found. Skipping sync of permissions to #{target_user_id}." return elsif target_user . nil then @logger . warn "  Target user: #{target_user_id} Not found. Skipping sync of permissions for #{target_user_id}." end memberships_existing = target_user [ "TeamMemberships" ] source_memberships = source_user [ "TeamMemberships" ] source_membership_oids = [ ] source_memberships . each do | this_source_membership | source_membership_oids . push ( get_membership_oid_from_membership ( this_source_membership ) ) end memberships_existing_oids = [ ] memberships_existing . each do | this_membership | memberships_existing_oids . push ( get_membership_oid_from_membership ( this_membership ) ) end permissions_existing = target_user . UserPermissions permissions_existing_by_project = { } permissions_existing . each do | this_permission | if this_permission . _type == "ProjectPermission" then permissions_existing_by_project [ this_permission . Project . ObjectID . to_s ] = this_permission end end memberships_to_add = [ ] source_membership_oids . each do | this_membership_oid | if ! memberships_existing_oids . include? ( this_membership_oid ) then memberships_to_add . push ( this_membership_oid ) end end memberships_to_remove = [ ] memberships_existing_oids . each do | this_membership_oid | if ! source_membership_oids . include? ( this_membership_oid ) then memberships_to_remove . push ( this_membership_oid ) end end number_memberships_added = 0 memberships_to_add . each do | this_membership_oid | this_permission = permissions_existing_by_project [ this_membership_oid ] if ! this_permission . nil? then this_role = this_permission . Role if ! this_role . eql? ( EDITOR ) && ! this_role . eql ( PROJECTADMIN ) then @logger . warn "  Target User: #{target_user_id} must be an Editor or Project Admin to make them a Team Member. Skipping." return end this_project = find_project ( this_membership_oid ) if ! this_project . nil? then number_memberships_added += 1 this_project_name = this_project [ "Name" ] @logger . info "Updating TeamMembership on #{this_project_name} from #{source_user_id} to: #{target_user_id}." update_team_membership ( target_user , this_membership_oid , this_project_name , TEAMMEMBER_YES ) end end end number_memberships_removed = 0 memberships_to_remove . each do | this_membership_oid | this_project = find_project ( this_membership_oid ) if ! this_project . nil? then number_memberships_removed += 1 this_project_name = this_project [ "Name" ] @logger . info "Removing TeamMembership on #{this_project_name} from #{target_user_id} since source: #{source_user_id} is not a TeamMember." update_team_membership ( target_user , this_membership_oid , this_project_name , TEAMMEMBER_NO ) end end @logger . info "Team Memberships Added: #{number_memberships_added}; Team Memberships Removed: #{number_memberships_removed}" end
Mirrors team membership set from a source user to a target user
Mirrors team membership set from a source user to a target user
# build membership lists by Project ObjectID # build Target User Permissions list by Project ObjectID # Needed to make sure that user to whom we're trying to set team membership for # is an editor
source_user_id
target_user_id