lib/rally_user_helper.rb
def is_project_permission_upgrade?(project, user, new_permission)

      # set default return value
      project_permission_upgrade = false

      # set a default existing_permission conservatively at Project Admin
      existing_permission = PROJECTADMIN_READ
      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_projects = 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, is our proposed change an upgrade
        user_permissions = this_user.UserPermissions
        user_permissions.each do | this_permission |

          if this_permission._type == "ProjectPermission" then
            # user has existing permissions in this project - let's compare new role against existing
            if this_permission.Project.ObjectID.to_s == project["ObjectID"].to_s then
              existing_permission = this_permission.Role
              case existing_permission

                # you can't upgrade a PROJECTADMIN, so return false
                when PROJECTADMIN_READ
                  project_permission_upgrade = false
                when EDITOR
                  if new_permission.eql?(PROJECTADMIN_READ) then
                    project_permission_upgrade = true
                  end
                when VIEWER
                  if new_permission.eql?(EDITOR) || new_permission.eql?(PROJECTADMIN_READ) then
                    project_permission_upgrade = true
                  end
                # NOACCESS is denoted by non-existence of a permission so we don't
                # check that here
              end
              number_matching_projects += 1
            end
          end
        end

        # This is a new project permission - set the changed bit to true
        if number_matching_projects == 0 then
           project_permission_upgrade = true
        end

      else # Cache does not exist or user isn't in it - query info from Rally

        project_permission_query = RallyAPI::RallyQuery.new()
        project_permission_query.type = :projectpermission
        project_permission_query.fetch = "Project,Name,ObjectID,Role,User"
        project_permission_query.page_size = 200 #optional - default is 200
        project_permission_query.order = "Name Asc"
        project_permission_query.query_string = "(User.UserName = \"" + user.UserName + "\")"

        query_results = @rally.find(project_permission_query)

        project_permission_upgrade = false
        number_matching_projects = 0

        # Look to see if any existing ProjectPermissions for this user match the one we're examining
        # If so, check to see if the project permissions are any different
        query_results.each { |pp|

          if ( pp.Project.ObjectID == project["ObjectID"])
            number_matching_projects+=1
            existing_permission = pp.Role
            case existing_permission
                # you can't upgrade a PROJECTADMIN, so return false
                when PROJECTADMIN_READ
                    project_permission_upgrade = false
                when EDITOR
                    if new_permission.eql?(PROJECTADMIN_READ) then
                        project_permission_upgrade = true
                    end
                when VIEWER
                    if new_permission.eql?(EDITOR) || new_permission.eql?(PROJECTADMIN_READ) then
                        project_permission_upgrade = true
                    end
                # NOACCESS is denoted by non-existence of a permission so we don't
                # check that here
            end
          end
        }
        # This is a new project permission - set the upgrade bit to true
        if number_matching_projects == 0 then project_permission_upgrade = true end
      end
      return project_permission_upgrade, existing_permission
    end
# check if the new permissions are an upgrade vs. what the user currently has
check if the new permissions are an upgrade vs. what the user currently has
check if the new permissions are an upgrade vs . what the user currently has
def is_project_permission_upgrade?(project, user, new_permission)
      project_permission_upgrade = false
      existing_permission = PROJECTADMIN_READ
      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_projects = 0
        this_user = @cached_users[user.UserName]
        user_permissions = this_user.UserPermissions
        user_permissions.each do | this_permission |
          if this_permission._type == "ProjectPermission" then
            if this_permission.Project.ObjectID.to_s == project["ObjectID"].to_s then
              existing_permission = this_permission.Role
              case existing_permission
                when PROJECTADMIN_READ
                  project_permission_upgrade = false
                when EDITOR
                  if new_permission.eql?(PROJECTADMIN_READ) then
                    project_permission_upgrade = true
                  end
                when VIEWER
                  if new_permission.eql?(EDITOR) || new_permission.eql?(PROJECTADMIN_READ) then
                    project_permission_upgrade = true
                  end
              end
              number_matching_projects += 1
            end
          end
        end
        if number_matching_projects == 0 then
           project_permission_upgrade = true
        end
      else 
        project_permission_query = RallyAPI::RallyQuery.new()
        project_permission_query.type = :projectpermission
        project_permission_query.fetch = "Project,Name,ObjectID,Role,User"
        project_permission_query.page_size = 200 
        project_permission_query.order = "Name Asc"
        project_permission_query.query_string = "(User.UserName = \"" + user.UserName + "\")"
        query_results = @rally.find(project_permission_query)
        project_permission_upgrade = false
        number_matching_projects = 0
        query_results.each { |pp|
          if ( pp.Project.ObjectID == project["ObjectID"])
            number_matching_projects+=1
            existing_permission = pp.Role
            case existing_permission
                when PROJECTADMIN_READ
                    project_permission_upgrade = false
                when EDITOR
                    if new_permission.eql?(PROJECTADMIN_READ) then
                        project_permission_upgrade = true
                    end
                when VIEWER
                    if new_permission.eql?(EDITOR) || new_permission.eql?(PROJECTADMIN_READ) then
                        project_permission_upgrade = true
                    end
            end
          end
        }
        if number_matching_projects == 0 then project_permission_upgrade = true end
      end
      return project_permission_upgrade, existing_permission
    end
def is_project_permission_upgrade? ( project , user , new_permission ) project_permission_upgrade = false existing_permission = PROJECTADMIN_READ 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_projects = 0 this_user = @cached_users [ user . UserName ] user_permissions = this_user . UserPermissions user_permissions . each do | this_permission | if this_permission . _type == "ProjectPermission" then if this_permission . Project . ObjectID . to_s == project [ "ObjectID" ] . to_s then existing_permission = this_permission . Role case existing_permission when PROJECTADMIN_READ project_permission_upgrade = false when EDITOR if new_permission . eql? ( PROJECTADMIN_READ ) then project_permission_upgrade = true end when VIEWER if new_permission . eql? ( EDITOR ) || new_permission . eql? ( PROJECTADMIN_READ ) then project_permission_upgrade = true end end number_matching_projects += 1 end end end if number_matching_projects == 0 then project_permission_upgrade = true end else project_permission_query = RallyAPI :: RallyQuery . new ( ) project_permission_query . type = :projectpermission project_permission_query . fetch = "Project,Name,ObjectID,Role,User" project_permission_query . page_size = 200 project_permission_query . order = "Name Asc" project_permission_query . query_string = "(User.UserName = \"" + user . UserName + "\")" query_results = @rally . find ( project_permission_query ) project_permission_upgrade = false number_matching_projects = 0 query_results . each { | pp | if ( pp . Project . ObjectID == project [ "ObjectID" ] ) number_matching_projects += 1 existing_permission = pp . Role case existing_permission when PROJECTADMIN_READ project_permission_upgrade = false when EDITOR if new_permission . eql? ( PROJECTADMIN_READ ) then project_permission_upgrade = true end when VIEWER if new_permission . eql? ( EDITOR ) || new_permission . eql? ( PROJECTADMIN_READ ) then project_permission_upgrade = true end end end } if number_matching_projects == 0 then project_permission_upgrade = true end end return project_permission_upgrade , existing_permission end
check if the new permissions are an upgrade vs. what the user currently has
check if the new permissions are an upgrade vs . what the user currently has
# set default return value # set a default existing_permission conservatively at Project Admin # 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, is our proposed change an upgrade # user has existing permissions in this project - let's compare new role against existing # you can't upgrade a PROJECTADMIN, so return false # NOACCESS is denoted by non-existence of a permission so we don't # check that here # This is a new project 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 ProjectPermissions for this user match the one we're examining # If so, check to see if the project permissions are any different # you can't upgrade a PROJECTADMIN, so return false # NOACCESS is denoted by non-existence of a permission so we don't # check that here # This is a new project permission - set the upgrade bit to true
project
user
new_permission