From 96acffbe5d739e8ac6ba6d1e7fd6c3d687fcb710 Mon Sep 17 00:00:00 2001 From: Rohit Jain Date: Tue, 23 Feb 2021 17:17:11 -0500 Subject: [PATCH] Fixes #872 --- build.gradle | 4 +- src/main/java/com/box/sdk/BoxGroup.java | 25 ++++- .../java/com/box/sdk/BoxGroupMembership.java | 106 ++++++++++++++++++ 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 7c9bee20f..43ac00c6f 100644 --- a/build.gradle +++ b/build.gradle @@ -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() @@ -148,7 +148,7 @@ tasks.withType(Test) { } jacoco { - append = true + //append = true destinationFile = file("$buildDir/jacoco/test.exec") } diff --git a/src/main/java/com/box/sdk/BoxGroup.java b/src/main/java/com/box/sdk/BoxGroup.java index f660bc1cc..9f5571d35 100644 --- a/src/main/java/com/box/sdk/BoxGroup.java +++ b/src/main/java/com/box/sdk/BoxGroup.java @@ -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; @@ -258,7 +260,7 @@ public Iterator 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); } /** @@ -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 configurablePermissions) { BoxAPIConnection api = this.getAPI(); JsonObject requestJSON = new JsonObject(); @@ -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()); diff --git a/src/main/java/com/box/sdk/BoxGroupMembership.java b/src/main/java/com/box/sdk/BoxGroupMembership.java index 2f78980ac..ee54f0e72 100644 --- a/src/main/java/com/box/sdk/BoxGroupMembership.java +++ b/src/main/java/com/box/sdk/BoxGroupMembership.java @@ -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; @@ -102,6 +104,11 @@ public class Info extends BoxResource.Info { */ private Date modifiedAt; + /** + * @see #getPermissions() + */ + private Map configurablePermissions; + /** * Constructs an empty Info object. */ @@ -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 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 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} */ @@ -189,6 +232,27 @@ public BoxGroupMembership getResource() { return BoxGroupMembership.this; } + private Map parseConfigurablePermissions(JsonObject jsonObject) { + if (jsonObject == null) { + return null; + } + Map permissions = new HashMap(); + 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} */ @@ -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); @@ -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; + } + } }