プロジェクトの 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();
}
}