1

是否有可用于禁用用 Java 编写的一个 Rally 用户的代码?如果它已经在某个地方,我想得到相同的。任何帮助表示赞赏。谢谢

4

1 回答 1

0

此代码禁用用户:

import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.UpdateResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class DisableUser {

    public static void main(String[] args) throws URISyntaxException, IOException {


        String host = "https://rally1.rallydev.com";
            String username = "user@co.com";
            String password = "1984";
            String applicationName = "RESTExampleStoriesChildren";
            String wsapiVersion = "1.43";

        RallyRestApi restApi = new RallyRestApi(
                new URI(host),
                username,
                password);
        restApi.setApplicationName(applicationName); 
        restApi.setWsapiVersion(wsapiVersion);
        System.out.println(restApi.getWsapiVersion()); 

        try{
        QueryRequest userRequest = new QueryRequest("User");
        userRequest.setFetch(new Fetch("UserName", "Subscription", "DisplayName", "Disabled"));
        userRequest.setQueryFilter(new QueryFilter("UserName", "=", "someuser@co.com"));
        QueryResponse userQueryResponse = restApi.query(userRequest);
        JsonObject userQueryObject = userQueryResponse.getResults().get(0).getAsJsonObject();
        String userRef = userQueryObject.get("_ref").toString();
        System.out.println(userRef);
        System.out.println(userQueryObject.get("UserName") + " is currently disabled? " + userQueryObject.get("Disabled"));
        JsonObject userUpdate = new JsonObject();
        userUpdate.addProperty("Disabled", true);
        UpdateRequest updateUserDisableRequest = new UpdateRequest(userRef,userUpdate);
        UpdateResponse updateResponse = restApi.update(updateUserDisableRequest);

        if (updateResponse.wasSuccessful()) {
            System.out.println("Successfully updated user: " + userQueryObject.get("UserName") +
                    " to: " + userUpdate.get("Disabled"));
            System.out.println(String.format("Updated %s", updateResponse.getObject().get("_ref").getAsString()));
            String[] warningList;
            warningList = updateResponse.getWarnings();
            for (int i=0;i<warningList.length;i++) {
                System.out.println(warningList[i]);
                }
            } else {
                System.out.println("Error occurred attempting to update UserName: " + userQueryObject.get("UserName"));
                String[] errorList;
                errorList = updateResponse.getErrors();
                for (int i=0;i<errorList.length;i++) {
                    System.out.println(errorList[i]);
                }
            }
        }catch(Exception e){
            System.out.println("Exception occurred while attempting to update user.");
            e.printStackTrace();
        }
        finally{
            restApi.close();
        }
    }
}
于 2013-08-21T15:23:27.903 回答