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

Commit

Permalink
Refactored the roles handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zsvoboda committed Sep 18, 2012
1 parent cc36794 commit 0ac40d3
Show file tree
Hide file tree
Showing 26 changed files with 53 additions and 1,515 deletions.
2 changes: 1 addition & 1 deletion backend/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-cl-backend</artifactId>
Expand Down
Expand Up @@ -103,15 +103,17 @@ public class GdcRESTApiWrapper {
private JSONObject userLogin = null;
private JSONObject profile;

private static HashMap<String, Integer> ROLES = new HashMap<String, Integer>();
private static HashMap<String, String> ROLES = new HashMap<String, String>();

/* TODO This is fragile and may not work for all projects and/or future versions.
* Use /gdc/projects/{projectId}/roles to retrieve roles for a particular project.
*/
static {
ROLES.put("ADMIN", new Integer(1));
ROLES.put("EDITOR", new Integer(2));
ROLES.put("DASHBOARD ONLY", new Integer(3));
ROLES.put("ADMIN", "adminRole");
ROLES.put("EDITOR", "editorRole");
ROLES.put("DASHBOARD ONLY", "dashboardOnlyRole");
ROLES.put("UNVERIFIED ADMIN", "unverifiedAdminRole");
ROLES.put("READONLY", "readOnlyUserRole");
}

/**
Expand Down Expand Up @@ -1888,6 +1890,26 @@ private JSONObject getCreateUserStructure(GdcUser user) {
return param;
}

private String getRoleUri(String projectId, String role) {

String roleUri = null;

// for backward compatibility

if(ROLES.containsKey(role.toUpperCase())) {
role = ROLES.get(role.toUpperCase());
}

List<GdcRole> roles = getProjectRoles(projectId);
for(GdcRole r : roles) {
String identifier = r.getIdentifier();
if(identifier.equalsIgnoreCase(role)) {
roleUri = r.getUri();
}
}
return roleUri;
}

/**
* Create a new user
*
Expand All @@ -1900,48 +1922,18 @@ private JSONObject getCreateUserStructure(GdcUser user) {
public void addUsersToProject(String projectId, List<String> uris, String role)
throws GdcRestApiException {

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

String roleUri = null;
if (role != null && role.length() > 0) {
Integer roleId = ROLES.get(role.toUpperCase());
if (roleId == null)
throw new InvalidParameterException("The role '" + role + "' is not recognized by the GoodData platform.");
roleUri = PROJECTS_URI + "/" + projectId + PROJECT_ROLES_SUFFIX + "/" + roleId.toString();
}
PostMethod req = createPostMethod(projectsUrl + PROJECT_USERS_SUFFIX);
JSONObject param = getAddUsersToProjectStructure(uris, roleUri);
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 added to 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 adding users " + uris + " to project", ex);
throw new GdcRestApiException("Error adding users " + uris + " to project ", ex);
} finally {
req.releaseConnection();
}
String roleUri = getRoleUri(projectId, role);

addUsersToProjectWithRoleUri(projectId, uris, roleUri);
}

public void addUsersToProjectWithRoleUri(String projectId, List<String> uris, String roleUri)
throws GdcRestApiException {

l.debug("Adding users " + uris + " to project " + projectId);
l.debug("Adding users " + uris + " to project " + projectId + " with roleUri "+ roleUri);
String projectsUrl = getProjectUrl(projectId);

PostMethod req = createPostMethod(projectsUrl + PROJECT_USERS_SUFFIX);
Expand Down Expand Up @@ -2599,12 +2591,11 @@ private JSONObject getInviteStructure(String pid, String eMail, String msg, Stri
content.put("firstname", "");
content.put("lastname", "");
content.put("email", eMail);
String puri = getServerUrl() + getProjectDeleteUri(pid);
if (role != null && role.length() > 0) {
Integer roleId = ROLES.get(role.toUpperCase());
if (roleId == null)
String roleUri = getRoleUri(pid, role);
if (roleUri == null)
throw new InvalidParameterException("The role '" + role + "' is not recognized by the GoodData platform.");
content.put("role", puri + "/" + roleId);
content.put("role", roleUri);
}
JSONObject action = new JSONObject();
action.put("setMessage", msg);
Expand Down Expand Up @@ -3069,7 +3060,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.58");
request.setRequestHeader("User-Agent", "GoodData CL/1.2.59");
return request;
}

Expand Down
4 changes: 2 additions & 2 deletions cli-distro/doc/CLI.md
Expand Up @@ -24,7 +24,7 @@ Project Management Commands:
`InviteUser(email="...", msg="...", role="...");` - invites a new user to the project (must call `CreateProject` or `OpenProject` before)
- email - the invited user's e-mail
- msg - *(optional)* invitation message
- role - *(optional)* initial user's role: `admin`|`editor`|`dashboard only`
- role - *(optional)* initial user's role: admin|adminRole|editor|editorRole|dashboard only|dashboardOnlyRole|readonly|readonlyUserRole

`CreateUser(domain="...", username="...", password="...", firstName="...", lastName="...", company="...", phone="...", country="...", position="...", ssoProvider="...", usersFile="...", append="...");` - creates a new user.
- domain - the GoodData users domain. The domain needs to be created by GoodData admins and associated with your GoodData account
Expand All @@ -47,7 +47,7 @@ Project Management Commands:

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

`DisableUsersInProject(usersFile="...")` - disables users in the usersFile in the open project
- usersFile - the list of user URIs in a file
Expand Down
2 changes: 1 addition & 1 deletion cli-distro/pom.xml
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion cli/pom.xml
Expand Up @@ -27,7 +27,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-cl-cli</artifactId>
Expand Down
Expand Up @@ -267,7 +267,7 @@ protected CliParams parse(CommandLine ln) throws InvalidArgumentException {
CliParams cp = new CliParams();

if (cp.containsKey(CLI_PARAM_VERSION[0])) {
l.info("GoodData CL version 1.2.58");
l.info("GoodData CL version 1.2.59");
System.exit(0);
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/java/com/gooddata/processor/GdcDI.java
Expand Up @@ -384,7 +384,7 @@ protected CliParams parse(CommandLine ln, Properties defaults) throws InvalidArg

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

l.info("GoodData CL version 1.2.58" +
l.info("GoodData CL version 1.2.59" +
((BUILD_NUMBER.length() > 0) ? ", build " + BUILD_NUMBER : "."));
System.exit(0);

Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/resources/com/gooddata/processor/COMMANDS.txt
Expand Up @@ -22,7 +22,7 @@ UseProject(fileName="..."); - loads the current project identifier from the spec
InviteUser(email="...", msg="...", role="..."); - invites a new user to the project (must call CreateProject or OpenProject before)
- email - the invited user's e-mail
- msg - (optional) invitation message
- role - (optional) initial user's role: admin|editor|dashboard only
- role - (optional) initial user's role: admin|adminRole|editor|editorRole|dashboard only|dashboardOnlyRole|readonly|readonlyUserRole

CreateUser(domain="...", username="...", password="...", firstName="...", lastName="...", company="...", phone="...", country="...", position="...", ssoProvider="...", usersFile="...", append="..."); - creates a new user.
- domain - the GoodData users domain. The domain needs to be created by GoodData admins and associated with your GoodData account
Expand All @@ -45,7 +45,7 @@ GetProjectUsers(usersFile="...", field = "...", activeOnly=<true|false>); - get

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

DisableUsersInProject(usersFile="...") - disables users in the usersFile in the open project
- usersFile - the list of user URIs in a file
Expand Down
2 changes: 1 addition & 1 deletion common/pom.xml
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-cl-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion connector/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-cl-connector</artifactId>
Expand Down
Expand Up @@ -241,7 +241,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.58");
request.setRequestHeader("User-Agent", "GoodData CL/1.2.59");
return request;
}

Expand Down
2 changes: 1 addition & 1 deletion notification-distro/pom.xml
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-alert</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion notification/pom.xml
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-notification</artifactId>
Expand Down
Expand Up @@ -371,7 +371,7 @@ else if (defaults.getProperty(name) != null) {
}

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

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -28,7 +28,7 @@

<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
<name>gooddata-cl</name>
<description>GoodData CL command line tool and Java API framework.</description>
<url>http://developer.gooddata.com</url>
Expand Down
2 changes: 1 addition & 1 deletion sfdc/pom.xml
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.gooddata.cl</groupId>
<artifactId>gooddata-cl</artifactId>
<version>1.2.58</version>
<version>1.2.59</version>
</parent>

<artifactId>gooddata-sfdc-lib</artifactId>
Expand Down

0 comments on commit 0ac40d3

Please sign in to comment.