lib/rally_user_helper.rb
# Mirrors ProjectPermission set from a source user to a target user
# Note that creation of a ProjectPermission will automatically create
# the minimum needed WorkspacePermission for the "owning" Workspace,
# if a WorkspacePermission does not already exist. Thus there's
# no need for a sync_workspace_permissions method
Mirrors ProjectPermission set from a source user to a target user
Note that creation of a ProjectPermission will automatically create
the minimum needed WorkspacePermission for the "owning" Workspace,
if a WorkspacePermission does not already exist. Thus there's
no need for a sync_workspace_permissions method
Mirrors ProjectPermission set from a source user to a target user Note that creation of a ProjectPermission will automatically create the minimum needed WorkspacePermission for the " owning " Workspace if a WorkspacePermission does not already exist . Thus there ' s no need for a sync_workspace_permissions method
def sync_project_permissions(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}."
        return
      end
      permissions_existing = target_user.UserPermissions
      source_permissions = source_user.UserPermissions
      if source_permissions.nil? then
        @logger.warn "  Source user: #{source_user_id} does not have any permissions assigned. Skipping sync of permissions to #{target_user_id}."
        return
      end
      source_permissions_by_project = {}
      source_permissions.each do | this_source_permission |
        if this_source_permission._type == "ProjectPermission" then
          source_permissions_by_project[this_source_permission.Project.ObjectID.to_s] = this_source_permission
        end
      end
      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
      permissions_to_update = []
      permissions_to_create = []
      permissions_to_delete = []
      source_permissions_by_project.each_pair do | this_source_project_oid, this_source_permission |
        if !permissions_existing_by_project.has_key?(this_source_project_oid) then
          permissions_to_create.push(this_source_permission)
        else
          this_source_role = this_source_permission.Role
          this_source_project = find_project(this_source_project_oid)
          this_source_project_name = this_source_project["Name"]
          if project_permissions_different?(this_source_project, target_user, this_source_role) then
            existing_permission = permissions_existing_by_project[this_source_project_oid]
            this_existing_project = existing_permission.Project
            this_existing_project_name = this_existing_project["Name"]
            this_existing_role = existing_permission.Role
            @logger.info "Existing Permission: #{this_existing_project_name}: #{this_existing_role}"
            @logger.info "Updated Permission: #{this_source_project_name}: #{this_source_role}"
            permissions_to_update.push(this_source_permission)
          end
        end
      end
      permissions_existing_by_project.each_pair do | this_existing_project_oid, this_existing_permission |
        if !source_permissions_by_project.has_key?(this_existing_project_oid) then
          permissions_to_delete.push(this_existing_permission)
        end
      end
      number_new_permissions = 0
      permissions_to_create.each do | this_new_permission |
        this_project = find_project(this_new_permission.Project.ObjectID.to_s)
        if !this_project.nil? then
          this_project = this_new_permission.Project
          this_workspace = this_project.Workspace
          this_workspace.read
          this_project_name = this_new_permission.Project.Name
          this_workspace_name = this_workspace.Name
          this_role = this_new_permission.Role
          @logger.info "Workspace: #{this_workspace_name}"
          @logger.info "Creating #{this_role} permission on #{this_project_name} from #{source_user_id} to: #{target_user_id}."
          create_project_permission(target_user, this_project, this_role)
          number_new_permissions += 1
        end
      end
      number_updated_permissions = 0
      permissions_to_update.each do | this_new_permission |
        this_project = find_project(this_new_permission.Project.ObjectID.to_s)
        if !this_project.nil? then
          this_project = this_new_permission.Project
          this_workspace = this_project.Workspace
          this_workspace.read
          this_project_name = this_new_permission.Project.Name
          this_workspace_name = this_workspace.Name
          this_role = this_new_permission.Role
          @logger.info "Workspace: #{this_workspace_name}"
          @logger.info "Updating #{this_role} permission on #{this_project_name} from #{source_user_id} to: #{target_user_id}."
          create_project_permission(target_user, this_project, this_role)
          number_updated_permissions += 1
        end
      end
      number_removed_permissions = 0
      permissions_to_delete.each do | this_deleted_permission |
        this_project = find_project(this_deleted_permission.Project.ObjectID.to_s)
        if !this_project.nil? then
          this_project = this_deleted_permission.Project
          this_workspace = this_project.Workspace
          this_workspace.read
          this_project_name = this_deleted_permission.Project.Name
          this_workspace_name = this_workspace.Name
          this_role = this_deleted_permission.Role
          @logger.info "Workspace: #{this_workspace_name}"
          @logger.info "Removing #{this_role} permission to #{this_project_name} from #{target_user_id} since it is not present on source: #{source_user_id}."
          if !@upgrade_only_mode then
            delete_project_permission(target_user, this_project)
            number_removed_permissions += 1
          else
            @logger.info "  #{target_user_id} - upgrade_only_mode == true."
            @logger.info "  Proposed Permission removal would downgrade permissions. No permission removal applied."
          end
        end
      end
      @logger.info "#{number_new_permissions} Permissions Created; #{number_updated_permissions} Permissions Updated; #{number_removed_permissions} Permissions Removed."
    end
def sync_project_permissions ( 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}." return end permissions_existing = target_user . UserPermissions source_permissions = source_user . UserPermissions if source_permissions . nil? then @logger . warn "  Source user: #{source_user_id} does not have any permissions assigned. Skipping sync of permissions to #{target_user_id}." return end source_permissions_by_project = { } source_permissions . each do | this_source_permission | if this_source_permission . _type == "ProjectPermission" then source_permissions_by_project [ this_source_permission . Project . ObjectID . to_s ] = this_source_permission end end 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 permissions_to_update = [ ] permissions_to_create = [ ] permissions_to_delete = [ ] source_permissions_by_project . each_pair do | this_source_project_oid , this_source_permission | if ! permissions_existing_by_project . has_key? ( this_source_project_oid ) then permissions_to_create . push ( this_source_permission ) else this_source_role = this_source_permission . Role this_source_project = find_project ( this_source_project_oid ) this_source_project_name = this_source_project [ "Name" ] if project_permissions_different? ( this_source_project , target_user , this_source_role ) then existing_permission = permissions_existing_by_project [ this_source_project_oid ] this_existing_project = existing_permission . Project this_existing_project_name = this_existing_project [ "Name" ] this_existing_role = existing_permission . Role @logger . info "Existing Permission: #{this_existing_project_name}: #{this_existing_role}" @logger . info "Updated Permission: #{this_source_project_name}: #{this_source_role}" permissions_to_update . push ( this_source_permission ) end end end permissions_existing_by_project . each_pair do | this_existing_project_oid , this_existing_permission | if ! source_permissions_by_project . has_key? ( this_existing_project_oid ) then permissions_to_delete . push ( this_existing_permission ) end end number_new_permissions = 0 permissions_to_create . each do | this_new_permission | this_project = find_project ( this_new_permission . Project . ObjectID . to_s ) if ! this_project . nil? then this_project = this_new_permission . Project this_workspace = this_project . Workspace this_workspace . read this_project_name = this_new_permission . Project . Name this_workspace_name = this_workspace . Name this_role = this_new_permission . Role @logger . info "Workspace: #{this_workspace_name}" @logger . info "Creating #{this_role} permission on #{this_project_name} from #{source_user_id} to: #{target_user_id}." create_project_permission ( target_user , this_project , this_role ) number_new_permissions += 1 end end number_updated_permissions = 0 permissions_to_update . each do | this_new_permission | this_project = find_project ( this_new_permission . Project . ObjectID . to_s ) if ! this_project . nil? then this_project = this_new_permission . Project this_workspace = this_project . Workspace this_workspace . read this_project_name = this_new_permission . Project . Name this_workspace_name = this_workspace . Name this_role = this_new_permission . Role @logger . info "Workspace: #{this_workspace_name}" @logger . info "Updating #{this_role} permission on #{this_project_name} from #{source_user_id} to: #{target_user_id}." create_project_permission ( target_user , this_project , this_role ) number_updated_permissions += 1 end end number_removed_permissions = 0 permissions_to_delete . each do | this_deleted_permission | this_project = find_project ( this_deleted_permission . Project . ObjectID . to_s ) if ! this_project . nil? then this_project = this_deleted_permission . Project this_workspace = this_project . Workspace this_workspace . read this_project_name = this_deleted_permission . Project . Name this_workspace_name = this_workspace . Name this_role = this_deleted_permission . Role @logger . info "Workspace: #{this_workspace_name}" @logger . info "Removing #{this_role} permission to #{this_project_name} from #{target_user_id} since it is not present on source: #{source_user_id}." if ! @upgrade_only_mode then delete_project_permission ( target_user , this_project ) number_removed_permissions += 1 else @logger . info "  #{target_user_id} - upgrade_only_mode == true." @logger . info "  Proposed Permission removal would downgrade permissions. No permission removal applied." end end end @logger . info "#{number_new_permissions} Permissions Created; #{number_updated_permissions} Permissions Updated; #{number_removed_permissions} Permissions Removed." end
Mirrors ProjectPermission set from a source user to a target user
Note that creation of a ProjectPermission will automatically create
the minimum needed WorkspacePermission for the "owning" Workspace,
if a WorkspacePermission does not already exist.
Mirrors ProjectPermission set from a source user to a target user Note that creation of a ProjectPermission will automatically create the minimum needed WorkspacePermission for the " owning " Workspace if a WorkspacePermission does not already exist .
# Check to make sure source and target users exist # Check to make sure source user has permissions # build permission hashes by Project ObjectID # Prepare arrays of permissions to update, create, or delete # Check target permissions list for permissions to create and/or update # If target hash doesn't contain the OID referenced in the source permission set, it's a new # permission we need to create # We found the OID key, so there is an existing permission for this Project. Is it different # from the target permission? # Loop through target permissions list and check for Project Permissions that don't exist # in source permissions template, indicating they need to be removed # Process creates # Process updates # Process deletes
source_user_id
target_user_id