Skip to content

Commit

Permalink
Fixes box#872
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Jain committed Feb 23, 2021
1 parent c3cd6c3 commit 96acffb
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 3 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sourceCompatibility = 1.7

group = 'com.box'
archivesBaseName = 'box-java-sdk'
version = '2.53.0'
version = '2.53.0-872'

repositories {
mavenCentral()
Expand Down Expand Up @@ -148,7 +148,7 @@ tasks.withType(Test) {
}

jacoco {
append = true
//append = true
destinationFile = file("$buildDir/jacoco/test.exec")
}

Expand Down
25 changes: 24 additions & 1 deletion src/main/java/com/box/sdk/BoxGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import com.box.sdk.BoxGroupMembership.Permission;
import com.box.sdk.BoxGroupMembership.Role;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
Expand Down Expand Up @@ -258,7 +260,7 @@ public Iterator<BoxGroupMembership.Info> iterator() {
* @return info about the new group membership.
*/
public BoxGroupMembership.Info addMembership(BoxUser user) {
return this.addMembership(user, null);
return this.addMembership(user, null, null);
}

/**
Expand All @@ -268,6 +270,19 @@ public BoxGroupMembership.Info addMembership(BoxUser user) {
* @return info about the new group membership.
*/
public BoxGroupMembership.Info addMembership(BoxUser user, Role role) {
return this.addMembership(user, role, null);
}

/**
* Adds a member to this group with the specified role.
* @param user the member to be added to this group.
* @param role the role of the user in this group. Can be null to assign the default role.
* @param configurablePermissions the configurable permission of the user as a group admin.
* Can be null to give all group admin permissions.
* @return info about the new group membership.
*/
public BoxGroupMembership.Info addMembership(BoxUser user, Role role,
Map<BoxGroupMembership.Permission, Boolean> configurablePermissions) {
BoxAPIConnection api = this.getAPI();

JsonObject requestJSON = new JsonObject();
Expand All @@ -277,6 +292,14 @@ public BoxGroupMembership.Info addMembership(BoxUser user, Role role) {
requestJSON.add("role", role.toJSONString());
}

if (configurablePermissions != null) {
JsonObject configurablePermissionJson = new JsonObject();
for (Permission attrKey : configurablePermissions.keySet()) {
configurablePermissionJson.set(attrKey.toJSONValue(), configurablePermissions.get(attrKey));
}
requestJSON.add("configurable_permissions", configurablePermissionJson);
}

URL url = ADD_MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL());
BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
request.setBody(requestJSON.toString());
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/box/sdk/BoxGroupMembership.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
Expand Down Expand Up @@ -102,6 +104,11 @@ public class Info extends BoxResource.Info {
*/
private Date modifiedAt;

/**
* @see #getPermissions()
*/
private Map<Permission, Boolean> configurablePermissions;

/**
* Constructs an empty Info object.
*/
Expand Down Expand Up @@ -181,6 +188,42 @@ public Date getModifiedAt() {
return this.modifiedAt;
}

/**
* Gets the configurablePermissions that the current user has on the group as group admin.
*
* @return the configurablePermissions that the current user has on the group as group admin.
*/
public Map<Permission, Boolean> getConfigurablePermissions() {
return this.configurablePermissions;
}

/**
* Sets the configurablePermissions that the current user has on the group as group admin.
* @param configurablePermissions a Map representing the group admin configurable permissions
*/
public void setConfigurablePermissions(Map<Permission, Boolean> configurablePermissions) {
this.configurablePermissions = configurablePermissions;
this.addPendingChange("configurable_permissions", this.configurablePermissionJson());
}

/**
* append new configurable permissions to the previous existing list.
* @param permission the group admin permission one wants to enable or disable of the user on the group.
* @param value the true/false value of the attribute to set.
*/
public void appendConfigurablePermissions(Permission permission, Boolean value) {
this.configurablePermissions.put(permission, value);
this.addPendingChange("configurable_permissions", this.configurablePermissionJson());
}

private JsonObject configurablePermissionJson() {
JsonObject configurablePermissionJson = new JsonObject();
for (Permission attrKey : this.configurablePermissions.keySet()) {
configurablePermissionJson.set(attrKey.toJSONValue(), this.configurablePermissions.get(attrKey));
}
return configurablePermissionJson;
}

/**
* {@inheritDoc}
*/
Expand All @@ -189,6 +232,27 @@ public BoxGroupMembership getResource() {
return BoxGroupMembership.this;
}

private Map<Permission, Boolean> parseConfigurablePermissions(JsonObject jsonObject) {
if (jsonObject == null) {
return null;
}
Map<Permission, Boolean> permissions = new HashMap<Permission, Boolean>();
for (JsonObject.Member member : jsonObject) {
String memberName = member.getName();
boolean memberValue = member.getValue().asBoolean();
if (memberName.equals("can_create_accounts")) {
permissions.put(Permission.CAN_CREATE_ACCOUNTS, memberValue);
} else if (memberName.equals("can_edit_accounts")) {
permissions.put(Permission.CAN_EDIT_ACCOUNTS, memberValue);
} else if (memberName.equals("can_instant_login")) {
permissions.put(Permission.CAN_INSTANT_LOGIN, memberValue);
} else if (memberName.equals("can_run_reports")) {
permissions.put(Permission.CAN_RUN_REPORTS, memberValue);
}
}
return permissions;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -229,6 +293,9 @@ protected void parseJSONMember(JsonObject.Member member) {
} else if (memberName.equals("modified_at")) {
this.modifiedAt = BoxDateFormat.parse(value.asString());

} else if (memberName.equals("configurable_permissions")) {
this.configurablePermissions = this.parseConfigurablePermissions(value.asObject());

}
} catch (Exception e) {
throw new BoxDeserializationException(memberName, value.toString(), e);
Expand Down Expand Up @@ -284,4 +351,43 @@ String toJSONString() {
return this.jsonValue;
}
}

/**
* Enumerates the possible permissions that a user can have as a group admin.
*/
public enum Permission {
/**
* The user can create accounts.
*/
CAN_CREATE_ACCOUNTS("can_create_accounts"),

/**
* The user can edit accounts.
*/
CAN_EDIT_ACCOUNTS("can_edit_accounts"),

/**
* The user can instant login as another user.
*/
CAN_INSTANT_LOGIN("can_instant_login"),

/**
* The user can run reports.
*/
CAN_RUN_REPORTS("can_run_reports");

private final String jsonValue;

private Permission(String jsonValue) {
this.jsonValue = jsonValue;
}

static Permission fromJSONValue(String jsonValue) {
return Permission.valueOf(jsonValue.toUpperCase());
}

String toJSONValue() {
return this.jsonValue;
}
}
}

0 comments on commit 96acffb

Please sign in to comment.