2013-07-14 131 views
0

我有要求將用戶添加到項目 用戶[email protected]有用戶的權限workSpace1 Project1存在於workSpace1 現在通過Java代碼 - 我需要給用戶編輯權限到ProjectJava Rally Rest Api:如何向項目添加新用戶

我有下面的代碼 - 沒有錯誤 - 但沒有更新

//fetch project as JSonObject 
JsonObject prjObj = new Example1().fetchPrjDetails("../project/12399939580.js", restApi); 

//get Editors as JSonArray   
JsonArray editorsList = prjObj.get("Editors").getAsJsonArray(); 

//Create new JSonObject with new user information    
JsonObject newUser = new JsonObject(); 
newUser.addProperty("_rallyAPIMajor", "1"); 
newUser.addProperty("_rallyAPIMinor", "37"); 
newUser.addProperty("_ref", "../user/12418060172.js"); 
newUser.addProperty("_refObjectName", "venu"); 
newUser.addProperty("_type", "User"); 

//add this new JSonObject to Editors Array   
editorsList.add(newUser); 

//remove the existing editors 
prjObj.remove("Editors"); 
//i dont know whether i need the above line 

//add editors to 
prjObj.add("Editors", editorsList); 


UpdateRequest uRequest = new UpdateRequest("..project/12399939580.js", prjObj); 
UpdateResponse uResponse = restApi.update(uRequest); 
    if (uResponse.wasSuccessful()) { 
    System.out.println("update success"); 
    }  

回答

0

編者收集一個項目是隻讀的。在拉力賽的web服務API中,拉力賽用戶的用戶權限由用戶對象上的UserPermissions集合管理。通過爲用戶創建新的ProjectPermission,將用戶添加到作爲編輯器的項目中。以下示例說明如何爲現有的拉力賽用戶創建編輯器級別的ProjectPermission,以及如何更新用戶的TeamMemberships集合,以使他或她也是該項目中的團隊成員。

public class RestExample_AddExistingUserToProject { 

    public static void main(String[] args) throws URISyntaxException, IOException { 
     // Create and configure a new instance of RallyRestApi 
     // Connection parameters 
     String rallyURL = "https://rally1.rallydev.com"; 
     String wsapiVersion = "1.43"; 
     String applicationName = "RestExample_AddExistingUserToProject"; 

     // Integration Credentials 
     // Must be a Rally Subscription Administrator, or a Workspace Administrator 
     // in a Rally Workspace whose Workspace Admins have been granted user administration 
     // privileges 

     String myUserName = "[email protected]"; 
     String myUserPassword = "topsecret"; 

     RallyRestApi restApi = new RallyRestApi(
       new URI(rallyURL), 
       myUserName, 
       myUserPassword); 
     restApi.setWsapiVersion(wsapiVersion); 
     restApi.setApplicationName(applicationName);  

     // Workspace and Project Settings 
     String myWorkspace = "Middle Earth"; 
     String myProject = "Mirkwood"; 

     // Rally UserID and meta-data for whom we wish to add a new Project Permission 
     String rallyUserId = "[email protected]"; 

     // Workspace Permission Strings 
     final String WORKSPACE_ADMIN = "Admin"; 
     final String WORKSPACE_USER = "User"; 
     final String PROJECT_EDITOR = "Editor"; 
     final String PROJECT_VIEWER = "Viewer"; 

     //Read User 
     QueryRequest userRequest = new QueryRequest("User"); 
     userRequest.setFetch(new Fetch(
       "UserName", 
       "FirstName", 
       "LastName", 
       "DisplayName", 
       "UserPermissions", 
       "Name", 
       "Role", 
       "Workspace", 
       "ObjectID", 
       "Project", 
       "ObjectID", 
       "TeamMemberships") 
     ); 
     userRequest.setQueryFilter(new QueryFilter("UserName", "=", rallyUserId)); 
     QueryResponse userQueryResponse = restApi.query(userRequest); 
     JsonObject userObject = userQueryResponse.getResults().get(0).getAsJsonObject();  
     String userRef = userObject.get("_ref").toString(); 
     System.out.println("Found User with Ref: " + userRef); 

     // Now add an additional Project Permission to the found User 

     // Get reference to Workspace in which Project for Permission Grant Resides 
     QueryRequest workspaceRequest = new QueryRequest("Workspace"); 
     workspaceRequest.setFetch(new Fetch("Name", "Owner", "Projects")); 
     workspaceRequest.setQueryFilter(new QueryFilter("Name", "=", myWorkspace)); 
     QueryResponse workspaceQueryResponse = restApi.query(workspaceRequest); 
     String workspaceRef = workspaceQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString(); 

     // Get reference to Project for this Permission Grant 
     QueryRequest projectRequest = new QueryRequest("Project"); 
     projectRequest.setFetch(new Fetch("Name", "Owner", "Projects")); 
     projectRequest.setQueryFilter(new QueryFilter("Name", "=", myProject)); 
     QueryResponse projectQueryResponse = restApi.query(projectRequest); 
     JsonObject projectObj = projectQueryResponse.getResults().get(0).getAsJsonObject(); 
     String projectRef = projectObj.get("_ref").toString(); 

     // Create the new ProjectPermission for the User 
     JsonObject newProjectPermission = new JsonObject(); 
     newProjectPermission.addProperty("Workspace", workspaceRef);  
     newProjectPermission.addProperty("Project", projectRef); 
     newProjectPermission.addProperty("User", userRef); 
     newProjectPermission.addProperty("Role", PROJECT_EDITOR); 

     CreateRequest createProjectPermissionRequest = new CreateRequest("projectpermission", newProjectPermission); 
     System.out.println(createProjectPermissionRequest.getBody()); 
     CreateResponse createProjectPermissionResponse = restApi.create(createProjectPermissionRequest); 

     if (createProjectPermissionResponse.wasSuccessful()) {   
      System.out.println(String.format("Create Project Permission Successful? %s", createProjectPermissionResponse.wasSuccessful())); 
      System.out.println(String.format("Added %s Access to Project %s", PROJECT_EDITOR, myProject)); 
      System.out.println(String.format("For user %s", rallyUserId));      
     } else { 
      String[] createErrors; 
      createErrors = createProjectPermissionResponse.getErrors(); 
      System.out.println("Error occurred creating User Project Permissions: "); 
      for (int i=0; i<createErrors.length;i++) { 
       System.out.println(createErrors[i]); 
      } 
     } 

     // Also Make this User a Team Member for Project of interest. 
     // Get this User's Existing Team Memberships 
     JsonArray existTeamMemberships = (JsonArray) userQueryResponse.getResults().get(0).getAsJsonObject().get("TeamMemberships"); 

     // Add this Project to User's Team Membership collection 
     existTeamMemberships.add(projectObj); 

     // Setup update fields/values for Team Membership 
     JsonObject updateUserTeamMembershipObj = new JsonObject(); 
     updateUserTeamMembershipObj.add("TeamMemberships", existTeamMemberships); 

     // Issue UpdateRequest for Team Membership 
     System.out.println("\nUpdating User's Team Membership..."); 
     UpdateRequest updateTeamMembershipsRequest = new UpdateRequest(userRef, updateUserTeamMembershipObj); 
     UpdateResponse updateTeamMembershipResponse = restApi.update(updateTeamMembershipsRequest); 

     if (updateTeamMembershipResponse.wasSuccessful()) { 
      System.out.println("Updated User to be Team Member in Project: " + myProject); 
     } else { 
      String[] updateTeamMembershipErrors; 
      updateTeamMembershipErrors = updateTeamMembershipResponse.getErrors(); 
       System.out.println("Error occurred updating Team Membership: "); 
      for (int i=0; i<updateTeamMembershipErrors.length;i++) { 
       System.out.println(updateTeamMembershipErrors[i]); 
      }     
     }     
     restApi.close(); 
    } 
} 
相關問題