Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Added the activeOnly flag to the GetProjectUser and added the Disable…
Browse files Browse the repository at this point in the history
…UsersInProject command.
  • Loading branch information
Zdenek Svoboda committed Aug 11, 2011
1 parent b1fa9e0 commit d5fb04c
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 39 deletions.
2 changes: 1 addition & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-cl-backend</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1485,19 +1485,19 @@ public GdcUser(JSONObject user) {
}
this.setLogin(v);
v = c.getString("firstname");
if(v != null && v.trim().length()<=0) {
if(v != null && v.trim().length()>0) {
this.setFirstName(v);
}
v = c.getString("lastname");
if(v != null && v.trim().length()<=0) {
if(v != null && v.trim().length()>0) {
this.setLastName(v);
}
v = c.getString("phonenumber");
if(v != null && v.trim().length()<=0) {
if(v != null && v.trim().length()>0) {
this.setPhoneNumber(v);
}
v = c.getString("status");
if(v != null && v.trim().length()<=0) {
if(v != null && v.trim().length()>0) {
this.setStatus(v);
}
JSONObject l = u.getJSONObject("links");
Expand Down Expand Up @@ -1781,12 +1781,73 @@ private JSONObject getAddUsersToProjectStructure(List<String> uris, String roleU
return param;
}

/**
* Disables a user in project
*
* @param projectId project ID
* @param uris user URIs
* @throws GdcRestApiException
*/
public void disableUsersInProject(String projectId, List<String> uris)
throws GdcRestApiException {

l.debug("Disabling users "+uris+" in project "+projectId);
String projectsUrl = getProjectUrl(projectId);

PostMethod req = createPostMethod(projectsUrl+PROJECT_USERS_SUFFIX);
JSONObject param = getDisableUsersInProjectStructure(uris);
InputStreamRequestEntity request = new InputStreamRequestEntity(new ByteArrayInputStream(
param.toString().getBytes()));
req.setRequestEntity(request);
String result = null;
try {
String response = executeMethodOk(req);
JSONObject responseObject = JSONObject.fromObject(response);
JSONObject projectUsersUpdateResult = responseObject.getJSONObject("projectUsersUpdateResult");
JSONArray failed = projectUsersUpdateResult.getJSONArray("failed");
if(!failed.isEmpty()) {
String errMsg = "Following users can't be disabled in the project:";
for(Object uri : failed.toArray()) {
errMsg += " "+uris.toString();
}
l.debug(errMsg);
throw new GdcRestApiException(errMsg);
}
//JSONArray successful = projectUsersUpdateResult.getJSONArray("successful");
} catch (HttpMethodException ex) {
l.debug("Error disabling users "+uris+" in project",ex);
throw new GdcRestApiException("Error disabling users "+uris+" in project ",ex);
} finally {
req.releaseConnection();
}
}

private JSONObject getDisableUsersInProjectStructure(List<String> uris) {
JSONObject param = new JSONObject();
JSONArray users = new JSONArray();
for(String uri : uris) {
JSONObject user = new JSONObject();
JSONObject content = new JSONObject();
content.put("status","DISABLED");
user.put("content", content);
JSONObject links = new JSONObject();
links.put("self", uri);
user.put("links", links);
JSONObject item = new JSONObject();
item.put("user",user);
users.add(item);
}
param.put("users", users);
return param;
}

/**
* Returns the selected project's users
* @param pid project ID
* @param activeUsersOnly lists only active users
* @return array of the project's users
*/
public ArrayList<GdcUser> getProjectUsers(String pid) {
public ArrayList<GdcUser> getProjectUsers(String pid, boolean activeUsersOnly) {
ArrayList<GdcUser> ret = new ArrayList<GdcUser>();
l.debug("Executing getProjectUsers for project id="+pid);
HttpMethod req = createGetMethod(getProjectUrl(pid) + PROJECT_USERS_SUFFIX);
Expand All @@ -1804,7 +1865,10 @@ public ArrayList<GdcUser> getProjectUsers(String pid) {
}
for(Object o : users) {
JSONObject user = (JSONObject)o;
ret.add(new GdcUser(user));
GdcUser g = new GdcUser(user);
if((activeUsersOnly && "ENABLED".equalsIgnoreCase(g.getStatus())) || (!activeUsersOnly)) {
ret.add(g);
}
}
return ret;
}
Expand Down Expand Up @@ -3269,7 +3333,7 @@ private static <T extends HttpMethod> T configureHttpMethod(T request) {
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Accept-Charset", "utf-u");
request.setRequestHeader("User-Agent", "GoodData CL/1.2.38-BETA");
request.setRequestHeader("User-Agent", "GoodData CL/1.2.39-BETA");
return request;
}

Expand Down
8 changes: 6 additions & 2 deletions cli-distro/doc/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ Project Management Commands:
- usersFile - (optional) writes the user's URI to the specified file
- append - (optional) should the users file be appended (default is false)

`GetProjectUsers(usersFile="...", field = "...");` - get list of users from the current project
`GetProjectUsers(usersFile="...", field = "...", activeOnly=<true|false>);` - get list of users from the current project
- usersFile - writes the user's URI to the specified file
- field - uri | email - writes either user uri or e-mail to the usersFile
- activeOnly - (optional) lists only active users (not disabled) the false is default

`AddUsersToProject(usersFile="...", role="...")` - adds users in the usersFile to the open project in a specific role
- usersFile - (optional) writes the user's URI to the specified file
- usersFile - the list of user URIs in a file
- role - (optional) initial user's role: admin|editor|dashboard only

`DisableUsersInProject(usersFile="...")` - disables users in the usersFile in the open project
- usersFile - the list of user URIs in a file

`ExportProject(tokenFile="...", exportUsers="...", exportData="...", authorizedUsers="...");` - exports an existing project to temporary storage and returns the import token
- tokenFile - a file where the import token will be stored
- exportUsers - export existing project users `true` | `false`
Expand Down
6 changes: 5 additions & 1 deletion cli-distro/examples/create_user/cmd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ CreateUser(domain="GoodData", username="jane.doe.3@gooddata.com", password="secr

CreateProject(name="Users Test");

AddUsersToProject(usersFile="examples/create_user/users.txt", role="DASHBOARD ONLY");
AddUsersToProject(usersFile="examples/create_user/users.txt", role="DASHBOARD ONLY");
GetProjectUsers(usersFile="examples/create_user/users.1.txt", field = "email", activeOnly="false");
DisableUsersInProject(usersFile="examples/create_user/users.txt");
GetProjectUsers(usersFile="examples/create_user/users.2.txt", field = "email", activeOnly="false");
GetProjectUsers(usersFile="examples/create_user/users.3.txt", field = "email", activeOnly="true");
2 changes: 1 addition & 1 deletion cli-distro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-cl-cli</artifactId>
Expand Down
37 changes: 34 additions & 3 deletions cli/src/main/java/com/gooddata/processor/GdcDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ protected CliParams parse(CommandLine ln, Properties defaults) throws InvalidArg

if(cp.containsKey(CLI_PARAM_VERSION[0])) {

l.info("GoodData CL version 1.2.38-BETA" +
l.info("GoodData CL version 1.2.39-BETA" +
((BUILD_NUMBER.length()>0) ? ", build "+BUILD_NUMBER : "."));
System.exit(0);

Expand Down Expand Up @@ -578,6 +578,9 @@ else if(c.match("CreateUser")) {
else if(c.match("AddUsersToProject")) {
addUsersToProject(c, cli, ctx);
}
else if(c.match("DisableUsersInProject")) {
disableUsersInProject(c, cli, ctx);
}
else if(c.match("GetProjectUsers")) {
getProjectUsers(c, cli, ctx);
}
Expand Down Expand Up @@ -777,6 +780,31 @@ private void addUsersToProject(Command c, CliParams p, ProcessingContext ctx) th
l.info("Users "+uris+"' successfully added to project "+pid);
}


/**
* Adds a new user to project
* @param c command
* @param p cli parameters
* @param ctx current context
* @throws IOException IO issues
*/
private void disableUsersInProject(Command c, CliParams p, ProcessingContext ctx) throws IOException {
l.info("Disabling users in project.");

String pid = ctx.getProjectIdMandatory();
String usersFile = c.getParamMandatory("usersFile");
List<String> uris = new ArrayList<String>();
BufferedReader r = FileUtil.createBufferedUtf8Reader(usersFile);
String uri = r.readLine();
while (uri != null && uri.trim().length()>0) {
uris.add(uri.trim());
uri = r.readLine();
}
ctx.getRestApi(p).disableUsersInProject(pid, uris);
l.info("Users "+uris+"' successfully disabled in project "+pid);
}


/**
* Adds a new user to project
* @param c command
Expand All @@ -786,11 +814,14 @@ private void addUsersToProject(Command c, CliParams p, ProcessingContext ctx) th
*/
private void getProjectUsers(Command c, CliParams p, ProcessingContext ctx) throws IOException {
String pid = ctx.getProjectIdMandatory();
l.info("Getting users from project"+pid);
l.info("Getting users from project "+pid);
String usersFile = c.getParamMandatory("usersFile");
String field = c.getParamMandatory("field");
String activeOnlys = c.getParam("activeOnly");
final boolean activeOnly = (activeOnlys != null && "true".equalsIgnoreCase(activeOnlys));

if("email".equalsIgnoreCase(field) || "uri".equalsIgnoreCase(field)) {
List<GdcRESTApiWrapper.GdcUser> users = ctx.getRestApi(p).getProjectUsers(pid);
List<GdcRESTApiWrapper.GdcUser> users = ctx.getRestApi(p).getProjectUsers(pid,activeOnly);
for(GdcRESTApiWrapper.GdcUser user : users) {
if("email".equalsIgnoreCase(field)) {
FileUtil.writeStringToFile(user.getLogin()+"\n", usersFile, true);
Expand Down
8 changes: 6 additions & 2 deletions cli/src/main/resources/com/gooddata/processor/COMMANDS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ CreateUser(domain="...", username="...", password="...", firstName="...", lastNa
- usersFile - (optional) writes the user's URI to the specified file
- append - (optional) should the users file be appended (default is false)

GetProjectUsers(usersFile="...", field = "..."); - get list of users from the current project
GetProjectUsers(usersFile="...", field = "...", activeOnly=<true|false>); - get list of users from the current project
- usersFile - writes the user's URI to the specified file
- field - uri | email - writes either user uri or e-mail to the usersFile
- activeOnly - (optional) lists only active users (not disabled) the false is default

AddUsersToProject(usersFile="...", role="...") - adds users in the usersFile to the open project in a specific role
- usersFile - (optional) writes the user's URI to the specified file
- usersFile - the list of user URIs in a file
- role - (optional) initial user's role: admin|editor|dashboard only

DisableUsersInProject(usersFile="...") - disables users in the usersFile in the open project
- usersFile - the list of user URIs in a file

ExportProject(tokenFile="...", exportUsers="...", exportData="...", authorizedUsers="..."); - exports an existing project to temporary storage and returns the import token
- tokenFile - a file where the import token will be stored
- exportUsers - export existing project users true | false
Expand Down
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-cl-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-cl-connector</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private static DeleteMethod createDeleteMethod(String path) {
private static <T extends HttpMethod> T configureHttpMethod(T request) {
request.setRequestHeader("Content-Type", "text/xml");
request.setRequestHeader("Accept", "text/xml");
request.setRequestHeader("User-Agent", "GoodData CL/1.2.38-BETA");
request.setRequestHeader("User-Agent", "GoodData CL/1.2.39-BETA");
return request;
}

Expand Down
2 changes: 1 addition & 1 deletion gooddata-snaplogic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-cl-snaplogic</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion notification-distro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-alert</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion notification/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.38-BETA</version>
<version>1.2.39-BETA</version>
</parent>

<artifactId>gooddata-notification</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ else if (defaults.getProperty(name) != null) {
}

if(cp.containsKey(CLI_PARAM_VERSION[0])) {
l.info("GoodData Notification Tool version 1.2.38-BETA" +
l.info("GoodData Notification Tool version 1.2.39-BETA" +
((BUILD_NUMBER.length()>0) ? ", build "+BUILD_NUMBER : "."));
System.exit(0);

Expand Down
Loading

0 comments on commit d5fb04c

Please sign in to comment.