lib/rally_user_helper.rb
def workspace_permissions_different?(workspace, user, new_permission)

      # set default return value
      workspace_permission_changed = false

      use_cache = false

      # first try to lookup against cached user list -- much faster than re-querying Rally
      if @cached_users != nil then
        if @cached_users.has_key?(user.UserName) then use_cache = true end
      end

      # first try to lookup against cached user list -- much faster than re-querying Rally
      if use_cache then

        number_matching_workspaces = 0
        this_user = @cached_users[user.UserName]

        # loop through permissions and look to see if there's an existing permission for this
        # workspace, and if so, has it changed
        user_permissions = this_user.UserPermissions
        user_permissions.each do | this_permission |
          if this_permission._type == "WorkspacePermission" then
            if this_permission.Workspace.ObjectID.to_s == workspace["ObjectID"].to_s then
              number_matching_workspaces += 1
              if this_permission.Role != new_permission then workspace_permission_changed = true end
            end
          end
        end
        # This is a new workspace permission - set the changed bit to true
        if number_matching_workspaces == 0 then workspace_permission_changed = true end

      else # Cache does not exist or user isn't in it - query info from Rally
        workspace_permission_query = RallyAPI::RallyQuery.new()
        workspace_permission_query.type = :workspacepermission
        workspace_permission_query.fetch = "Workspace,Name,ObjectID,Role,User"
        workspace_permission_query.page_size = 200 #optional - default is 200
        workspace_permission_query.order = "Name Asc"
        workspace_permission_query.query_string = "(User.UserName = \"" + user.UserName + "\")"

        query_results = @rally.find(workspace_permission_query)

        workspace_permission_changed = false
        number_matching_workspaces = 0

        # Look to see if any existing WorkspacePermissions for this user match the one we're examining
        # If so, check to see if the workspace permissions are any different
        query_results.each { |wp|
          if ( wp.Workspace.ObjectID == workspace["ObjectID"])
            number_matching_workspaces+=1
            if wp.Role != new_permission then workspace_permission_changed = true end
          end
        }
        # This is a new workspace permission - set the changed bit to true
        if number_matching_workspaces == 0 then workspace_permission_changed = true end
      end
      return workspace_permission_changed
    end
# check if the new permissions are different than what the user currently has
# if we don't do this, we will delete and recreate permissions each time and that
# will make the revision history on user really, really, really, really ugly
check if the new permissions are different than what the user currently has
if we don't do this, we will delete and recreate permissions each time and that
will make the revision history on user really, really, really, really ugly
check if the new permissions are different than what the user currently has if we don ' t do this we will delete and recreate permissions each time and that will make the revision history on user really really really really ugly
def workspace_permissions_different?(workspace, user, new_permission)
      workspace_permission_changed = false
      use_cache = false
      if @cached_users != nil then
        if @cached_users.has_key?(user.UserName) then use_cache = true end
      end
      if use_cache then
        number_matching_workspaces = 0
        this_user = @cached_users[user.UserName]
        user_permissions = this_user.UserPermissions
        user_permissions.each do | this_permission |
          if this_permission._type == "WorkspacePermission" then
            if this_permission.Workspace.ObjectID.to_s == workspace["ObjectID"].to_s then
              number_matching_workspaces += 1
              if this_permission.Role != new_permission then workspace_permission_changed = true end
            end
          end
        end
        if number_matching_workspaces == 0 then workspace_permission_changed = true end
      else 
        workspace_permission_query = RallyAPI::RallyQuery.new()
        workspace_permission_query.type = :workspacepermission
        workspace_permission_query.fetch = "Workspace,Name,ObjectID,Role,User"
        workspace_permission_query.page_size = 200 
        workspace_permission_query.order = "Name Asc"
        workspace_permission_query.query_string = "(User.UserName = \"" + user.UserName + "\")"
        query_results = @rally.find(workspace_permission_query)
        workspace_permission_changed = false
        number_matching_workspaces = 0
        query_results.each { |wp|
          if ( wp.Workspace.ObjectID == workspace["ObjectID"])
            number_matching_workspaces+=1
            if wp.Role != new_permission then workspace_permission_changed = true end
          end
        }
        if number_matching_workspaces == 0 then workspace_permission_changed = true end
      end
      return workspace_permission_changed
    end
def workspace_permissions_different? ( workspace , user , new_permission ) workspace_permission_changed = false use_cache = false if @cached_users != nil then if @cached_users . has_key? ( user . UserName ) then use_cache = true end end if use_cache then number_matching_workspaces = 0 this_user = @cached_users [ user . UserName ] user_permissions = this_user . UserPermissions user_permissions . each do | this_permission | if this_permission . _type == "WorkspacePermission" then if this_permission . Workspace . ObjectID . to_s == workspace [ "ObjectID" ] . to_s then number_matching_workspaces += 1 if this_permission . Role != new_permission then workspace_permission_changed = true end end end end if number_matching_workspaces == 0 then workspace_permission_changed = true end else workspace_permission_query = RallyAPI :: RallyQuery . new ( ) workspace_permission_query . type = :workspacepermission workspace_permission_query . fetch = "Workspace,Name,ObjectID,Role,User" workspace_permission_query . page_size = 200 workspace_permission_query . order = "Name Asc" workspace_permission_query . query_string = "(User.UserName = \"" + user . UserName + "\")" query_results = @rally . find ( workspace_permission_query ) workspace_permission_changed = false number_matching_workspaces = 0 query_results . each { | wp | if ( wp . Workspace . ObjectID == workspace [ "ObjectID" ] ) number_matching_workspaces += 1 if wp . Role != new_permission then workspace_permission_changed = true end end } if number_matching_workspaces == 0 then workspace_permission_changed = true end end return workspace_permission_changed end
check if the new permissions are different than what the user currently has
if we don't do this, we will delete and recreate permissions each time and that
will make the revision history on user really, really, really, really ugly
check if the new permissions are different than what the user currently has if we don ' t do this we will delete and recreate permissions each time and that will make the revision history on user really really really really ugly
# set default return value # first try to lookup against cached user list -- much faster than re-querying Rally # first try to lookup against cached user list -- much faster than re-querying Rally # loop through permissions and look to see if there's an existing permission for this # workspace, and if so, has it changed # This is a new workspace permission - set the changed bit to true # Cache does not exist or user isn't in it - query info from Rally #optional - default is 200 # Look to see if any existing WorkspacePermissions for this user match the one we're examining # If so, check to see if the workspace permissions are any different # This is a new workspace permission - set the changed bit to true
workspace
user
new_permission