1

我需要将用户添加到项目用户 abcd@web.com 拥有对 workSpace1 的用户权限 Project1 现在通过 Java 代码存在于 workSpace1 中 - 我需要向用户授予 Project1 的编辑权限

我有以下代码 - 没有错误 - 但没有更新

//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");
    }       
4

1 回答 1

2

项目的 Editors 集合是只读的。在 Rally 的 Web 服务 API 中,Rally 用户的用户权限在用户对象的 UserPermissions 集合下进行管理。您可以通过为用户创建新的 ProjectPermission 将用户作为编辑器添加到项目中。以下示例说明了如何为现有 Rally 用户创建编辑器级别的 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 = "subadmin@company.com";
        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 = "tauriel@midearth.com";

        // 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();
    }
}
于 2013-07-14T19:10:23.410 回答