Skip to content

Commit

Permalink
Merge pull request #394 from PasinduYeshan/feature/bulk-v2-role-support
Browse files Browse the repository at this point in the history
Provide v2 role support for `scim2/bulk` API
  • Loading branch information
AnuradhaSK authored Oct 24, 2023
2 parents 43a64fb + 44c1d91 commit dbd0787
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ public BulkRequestData decodeBulkData(String bulkResourceString) throws BadReque
List<BulkRequestContent> usersEndpointOperationList = new ArrayList<>();
List<BulkRequestContent> groupsEndpointOperationList = new ArrayList<>();
List<BulkRequestContent> rolesEndpointOperationList = new ArrayList<>();
List<BulkRequestContent> rolesV2EndpointOperationList = new ArrayList<>();
int failOnErrorsAttribute;
List<String> schemas = new ArrayList<>();
Set<String> encounteredBulkIds = new HashSet<>();
Expand Down Expand Up @@ -930,7 +931,7 @@ public BulkRequestData decodeBulkData(String bulkResourceString) throws BadReque
}
encounteredBulkIds.add(bulkId);
setRequestData(requestType, requestMethod, requestVersion, member, usersEndpointOperationList,
groupsEndpointOperationList, rolesEndpointOperationList);
groupsEndpointOperationList, rolesEndpointOperationList, rolesV2EndpointOperationList);
} else {
String error = "JSON string could not be decoded properly.Required " +
"attribute BULK_ID is missing in the request";
Expand All @@ -939,7 +940,7 @@ public BulkRequestData decodeBulkData(String bulkResourceString) throws BadReque
}
} else {
setRequestData(requestType, requestMethod, requestVersion, member, usersEndpointOperationList,
groupsEndpointOperationList, rolesEndpointOperationList);
groupsEndpointOperationList, rolesEndpointOperationList, rolesV2EndpointOperationList);
}
}
//extract [failOnErrors] attribute from Json string
Expand All @@ -949,6 +950,7 @@ public BulkRequestData decodeBulkData(String bulkResourceString) throws BadReque
bulkRequestDataObject.setUserOperationRequests(usersEndpointOperationList);
bulkRequestDataObject.setGroupOperationRequests(groupsEndpointOperationList);
bulkRequestDataObject.setRoleOperationRequests(rolesEndpointOperationList);
bulkRequestDataObject.setRoleV2OperationRequests(rolesV2EndpointOperationList);

} catch (JSONException e) {
if (logger.isDebugEnabled()) {
Expand All @@ -961,8 +963,10 @@ public BulkRequestData decodeBulkData(String bulkResourceString) throws BadReque
}

private void setRequestData(String requestType, String requestMethod, String requestVersion, JSONObject member,
List<BulkRequestContent> usersEndpointOperationList, List<BulkRequestContent> groupsEndpointOperationList,
List<BulkRequestContent> rolesEndpointOperationList) {
List<BulkRequestContent> usersEndpointOperationList,
List<BulkRequestContent> groupsEndpointOperationList,
List<BulkRequestContent> rolesEndpointOperationList,
List<BulkRequestContent> rolesV2EndpointOperationList) {

// Create user request list.
if (requestType.contains(SCIMConstants.USER_ENDPOINT)) {
Expand All @@ -982,7 +986,11 @@ private void setRequestData(String requestType, String requestMethod, String req
}

// Create role request list.
if (requestType.contains(SCIMConstants.ROLE_ENDPOINT)) {
if (requestType.contains(SCIMConstants.ROLE_V2_ENDPOINT)) {
BulkRequestContent newRequestData =
getBulkRequestContent(member, requestMethod, requestType, requestVersion);
rolesV2EndpointOperationList.add(newRequestData);
} else if (requestType.contains(SCIMConstants.ROLE_ENDPOINT)) {
BulkRequestContent newRequestData =
getBulkRequestContent(member, requestMethod, requestType, requestVersion);
rolesEndpointOperationList.add(newRequestData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ public class BulkRequestData {
private List<BulkRequestContent> userOperationRequests;
private List<BulkRequestContent> groupOperationRequests;
private List<BulkRequestContent> roleOperationRequests;
private List<BulkRequestContent> roleV2OperationRequests;

public BulkRequestData() {

userOperationRequests = new ArrayList<>();
groupOperationRequests = new ArrayList<>();
roleOperationRequests = new ArrayList<>();
roleV2OperationRequests = new ArrayList<>();
schemas = new ArrayList<>();
}

Expand Down Expand Up @@ -73,6 +75,16 @@ public void setRoleOperationRequests(List<BulkRequestContent> roleOperationReque
this.roleOperationRequests = roleOperationRequests;
}

public List<BulkRequestContent> getRoleV2OperationRequests() {

return roleV2OperationRequests;
}

public void setRoleV2OperationRequests(List<BulkRequestContent> roleV2OperationRequests) {

this.roleV2OperationRequests = roleV2OperationRequests;
}

public List<String> getSchemas() {
return schemas;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.wso2.charon3.core.exceptions.BadRequestException;
import org.wso2.charon3.core.extensions.RoleManager;
import org.wso2.charon3.core.extensions.RoleV2Manager;
import org.wso2.charon3.core.extensions.UserManager;
import org.wso2.charon3.core.objects.bulk.BulkRequestContent;
import org.wso2.charon3.core.objects.bulk.BulkRequestData;
Expand All @@ -32,6 +33,7 @@
import org.wso2.charon3.core.protocol.endpoints.GroupResourceManager;
import org.wso2.charon3.core.protocol.endpoints.ResourceManager;
import org.wso2.charon3.core.protocol.endpoints.RoleResourceManager;
import org.wso2.charon3.core.protocol.endpoints.RoleResourceV2Manager;
import org.wso2.charon3.core.protocol.endpoints.UserResourceManager;
import org.wso2.charon3.core.schema.SCIMConstants;

Expand All @@ -46,10 +48,12 @@ public class BulkRequestProcessor {
private UserResourceManager userResourceManager;
private GroupResourceManager groupResourceManager;
private RoleResourceManager roleResourceManager;
private RoleResourceV2Manager roleResourceV2Manager;
private int failOnError;
private int errors;
private UserManager userManager;
private RoleManager roleManager;
private RoleV2Manager roleV2Manager;

public UserResourceManager getUserResourceManager() {
return userResourceManager;
Expand Down Expand Up @@ -77,6 +81,16 @@ public void setRoleResourceManager(RoleResourceManager roleResourceManager) {
this.roleResourceManager = roleResourceManager;
}

public RoleResourceV2Manager getRoleResourceV2Manager() {

return roleResourceV2Manager;
}

public void setRoleResourceV2Manager(RoleResourceV2Manager roleResourceV2Manager) {

this.roleResourceV2Manager = roleResourceV2Manager;
}

public int getFailOnError() {
return failOnError;
}
Expand Down Expand Up @@ -111,11 +125,22 @@ public void setRoleManager(RoleManager roleManager) {
this.roleManager = roleManager;
}

public RoleV2Manager getRoleV2Manager() {

return roleV2Manager;
}

public void setRoleV2Manager(RoleV2Manager roleV2Manager) {

this.roleV2Manager = roleV2Manager;
}

public BulkRequestProcessor() {

userResourceManager = new UserResourceManager();
groupResourceManager = new GroupResourceManager();
roleResourceManager = new RoleResourceManager();
roleResourceV2Manager = new RoleResourceV2Manager();
failOnError = 0;
errors = 0;
userManager = null;
Expand Down Expand Up @@ -150,6 +175,7 @@ public BulkResponseData processBulkRequests(BulkRequestData bulkRequestData) thr
}

}
// Handle v1 role operations.
for (BulkRequestContent bulkRequestContent : bulkRequestData.getRoleOperationRequests()) {
if (failOnError == 0) {
bulkResponseData.addRoleOperation(getBulkResponseContent(bulkRequestContent, userIdMappings,
Expand All @@ -161,6 +187,18 @@ public BulkResponseData processBulkRequests(BulkRequestData bulkRequestData) thr
}
}
}
// Handle v2 role operations.
for (BulkRequestContent bulkRequestContent : bulkRequestData.getRoleV2OperationRequests()) {
if (failOnError == 0) {
bulkResponseData.addRoleOperation(getBulkResponseContent(bulkRequestContent, userIdMappings,
roleResourceV2Manager));
} else {
if (errors < failOnError) {
bulkResponseData.addRoleOperation(getBulkResponseContent(bulkRequestContent,
userIdMappings, roleResourceV2Manager));
}
}
}
bulkResponseData.setSchema(SCIMConstants.BULK_RESPONSE_URI);
return bulkResponseData;
}
Expand All @@ -182,7 +220,9 @@ private BulkResponseContent getBulkResponseContent(BulkRequestContent bulkReques

switch (bulkRequestContent.getMethod()) {
case SCIMConstants.OperationalConstants.POST:
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_V2_ENDPOINT)) {
response = resourceManager.createRole(bulkRequestContent.getData(), roleV2Manager);
} else if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
response = resourceManager.createRole(bulkRequestContent.getData(), roleManager);
} else {
response = resourceManager.create(bulkRequestContent.getData(), userManager,
Expand All @@ -196,7 +236,11 @@ private BulkResponseContent getBulkResponseContent(BulkRequestContent bulkReques

case SCIMConstants.OperationalConstants.PUT: {
String resourceId = extractIDFromPath(bulkRequestContent.getPath());
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_V2_ENDPOINT)) {
resourceId = extractIDFromV2Path(bulkRequestContent.getPath());
response = resourceManager.updateWithPUTRole(resourceId, bulkRequestContent.getData(),
roleV2Manager);
} else if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
response = resourceManager.updateWithPUTRole(resourceId, bulkRequestContent.getData(),
roleManager);
} else {
Expand All @@ -212,7 +256,11 @@ private BulkResponseContent getBulkResponseContent(BulkRequestContent bulkReques

case SCIMConstants.OperationalConstants.PATCH: {
String resourceId = extractIDFromPath(bulkRequestContent.getPath());
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_V2_ENDPOINT)) {
resourceId = extractIDFromV2Path(bulkRequestContent.getPath());
response = resourceManager.updateWithPATCHRole(resourceId, bulkRequestContent.getData(),
roleV2Manager);
} else if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
response = resourceManager.updateWithPATCHRole(resourceId, bulkRequestContent.getData(),
roleManager);
} else {
Expand All @@ -228,7 +276,10 @@ private BulkResponseContent getBulkResponseContent(BulkRequestContent bulkReques

case SCIMConstants.OperationalConstants.DELETE: {
String resourceId = extractIDFromPath(bulkRequestContent.getPath());
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_V2_ENDPOINT)) {
resourceId = extractIDFromV2Path(bulkRequestContent.getPath());
response = resourceManager.deleteRole(resourceId, roleV2Manager);
} else if (bulkRequestContent.getPath().contains(SCIMConstants.ROLE_ENDPOINT)) {
response = resourceManager.deleteRole(resourceId, roleManager);
} else {
response = resourceManager.delete(resourceId, userManager);
Expand All @@ -254,6 +305,16 @@ private String extractIDFromPath(String path) throws BadRequestException {
}
}

private String extractIDFromV2Path(String path) throws BadRequestException {

String [] parts = path.split("[/]");
if (parts[3] != null) {
return parts[3];
}
throw new BadRequestException
("No resource Id is provided in path", ResponseCodeConstants.INVALID_VALUE);
}

private BulkResponseContent createBulkResponseContent(SCIMResponse response, String method,
BulkRequestContent requestContent) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wso2.charon3.core.exceptions.CharonException;
import org.wso2.charon3.core.exceptions.InternalErrorException;
import org.wso2.charon3.core.extensions.RoleManager;
import org.wso2.charon3.core.extensions.RoleV2Manager;
import org.wso2.charon3.core.extensions.UserManager;
import org.wso2.charon3.core.objects.bulk.BulkRequestData;
import org.wso2.charon3.core.objects.bulk.BulkResponseData;
Expand Down Expand Up @@ -50,6 +51,14 @@ public BulkResourceManager() {
bulkRequestProcessor = new BulkRequestProcessor();
}

public SCIMResponse processBulkData(String data, UserManager userManager, RoleManager roleManager,
RoleV2Manager roleV2Manager) {

bulkRequestProcessor.setRoleManager(roleManager);
bulkRequestProcessor.setRoleV2Manager(roleV2Manager);
return processBulkData(data, userManager);
}

public SCIMResponse processBulkData(String data, UserManager userManager, RoleManager roleManager) {

bulkRequestProcessor.setRoleManager(roleManager);
Expand Down

0 comments on commit dbd0787

Please sign in to comment.