Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support attributes and excludeAttributes query params for List roles v2 endpoint #397

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ RoleV2 getRole(String id, Map<String, Boolean> requiredAttributes)
/**
* List roles with Get.
*
* @param node Node
* @param startIndex Start Index
* @param count Count
* @param sortBy Sort by
* @param sortOrder Sort order
* @param node Node
* @param startIndex Start Index
* @param count Count
* @param sortBy Sort by
* @param sortOrder Sort order
* @param requiredAttributes Required attributes
ChanikaRuchini marked this conversation as resolved.
Show resolved Hide resolved
* @return List of roles.
* @throws CharonException CharonException.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
*/
RolesV2GetResponse listRolesWithGET(Node node, Integer startIndex, Integer count, String sortBy, String sortOrder)
RolesV2GetResponse listRolesWithGET(Node node, Integer startIndex, Integer count, String sortBy, String sortOrder,
List<String> requiredAttributes)
throws CharonException, NotImplementedException, BadRequestException;

/**
Expand All @@ -110,13 +112,14 @@ RoleV2 updateRole(RoleV2 oldRole, RoleV2 newRole)
/**
* List roles with Post.
*
* @param searchRequest Search request.
* @param searchRequest Search request.
* @param requiredAttributes Required attributes.
ChanikaRuchini marked this conversation as resolved.
Show resolved Hide resolved
* @return List of roles.
* @throws NotImplementedException NotImplementedException.
* @throws BadRequestException BadRequestException.
* @throws CharonException CharonException.
*/
RolesV2GetResponse listRolesWithPost(SearchRequest searchRequest)
RolesV2GetResponse listRolesWithPost(SearchRequest searchRequest, List<String> requiredAttributes)
throws NotImplementedException, BadRequestException, CharonException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,18 @@ default SCIMResponse deleteRole(String id, RoleV2Manager roleManager) {
/**
* GET method to list roles in roleV2 model.
*
* @param roleManager RoleV2 manager.
* @param filter Filter to be executed.
* @param startIndex Starting index value of the filter.
* @param count Number of required results.
* @param sortBy SortBy.
* @param sortOrder Sorting order.
* @param roleManager RoleV2 manager.
* @param filter Filter to be executed.
* @param startIndex Starting index value of the filter.
* @param count Number of required results.
* @param sortBy SortBy.
* @param sortOrder Sorting order.
* @param attributes Requested attributes.
* @param excludeAttributes Requested exclude attributes.
ChanikaRuchini marked this conversation as resolved.
Show resolved Hide resolved
* @return SCIMResponse.
*/
default SCIMResponse listWithGETRole(RoleV2Manager roleManager, String filter, Integer startIndex, Integer count,
String sortBy, String sortOrder) {
String sortBy, String sortOrder, String attributes, String excludeAttributes) {

return new SCIMResponse(ResponseCodeConstants.CODE_NOT_IMPLEMENTED, ResponseCodeConstants.DESC_NOT_IMPLEMENTED,
Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.wso2.charon3.core.objects.plainobjects.RolesV2GetResponse;
import org.wso2.charon3.core.protocol.ResponseCodeConstants;
import org.wso2.charon3.core.protocol.SCIMResponse;
import org.wso2.charon3.core.schema.AttributeSchema;
import org.wso2.charon3.core.schema.SCIMConstants;
import org.wso2.charon3.core.schema.SCIMResourceSchemaManager;
import org.wso2.charon3.core.schema.SCIMResourceTypeSchema;
Expand Down Expand Up @@ -157,7 +158,8 @@ public SCIMResponse deleteRole(String id, RoleV2Manager roleManager) {

@Override
public SCIMResponse listWithGETRole(RoleV2Manager roleManager, String filter, Integer startIndexInt,
Integer countInt, String sortBy, String sortOrder) {
Integer countInt, String sortBy, String sortOrder, String attributes,
String excludeAttributes) {

try {
if (roleManager == null) {
Expand All @@ -171,10 +173,12 @@ public SCIMResponse listWithGETRole(RoleV2Manager roleManager, String filter, In
// Build node for filtering.
Node rootNode = buildNode(filter, schema);
JSONEncoder encoder = getEncoder();

// Get the list of required attributes which must be given a value
List<String> requestedAttributes = getRequestedAttributes(
(SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
RolesV2GetResponse rolesResponse = roleManager.listRolesWithGET(rootNode, startIndex, count, sortBy,
sortOrder);
return processRoleList(rolesResponse, encoder, startIndex);
sortOrder, requestedAttributes);
return processRoleList(rolesResponse, encoder, startIndex, attributes, excludeAttributes);
} catch (CharonException | InternalErrorException | BadRequestException | NotImplementedException e) {
return encodeSCIMException(e);
} catch (IOException e) {
Expand All @@ -184,6 +188,18 @@ public SCIMResponse listWithGETRole(RoleV2Manager roleManager, String filter, In
}
}

private List<String> getRequestedAttributes(SCIMResourceTypeSchema schema, String requestedAttributes,
String requestedExcludingAttributes) throws CharonException {

ArrayList<AttributeSchema> requestedAttributeSchemas = ResourceManagerUtil.getOnlyRequiredAttributes(
(SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), requestedAttributes, requestedExcludingAttributes);
List<String> requestedAttributesList = new ArrayList<>();
for (AttributeSchema attributeSchema : requestedAttributeSchemas) {
requestedAttributesList.add(attributeSchema.getName());
}
return requestedAttributesList;
}

@Override
public SCIMResponse listWithPOSTRole(String searchRequest, RoleV2Manager roleManager) {

Expand Down Expand Up @@ -222,8 +238,13 @@ public SCIMResponse listWithPOSTRole(String searchRequest, RoleV2Manager roleMan
if (searchRequestObject.getSortOder() == null && searchRequestObject.getSortBy() != null) {
searchRequestObject.setSortOder(SCIMConstants.OperationalConstants.ASCENDING);
}
String attributes = searchRequestObject.getAttributesAsString();
String excludeAttributes = searchRequestObject.getExcludedAttributesAsString();

List<String> requestedAttributes = getRequestedAttributes(
(SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);

RolesV2GetResponse rolesResponse = roleManager.listRolesWithPost(searchRequestObject);
RolesV2GetResponse rolesResponse = roleManager.listRolesWithPost(searchRequestObject, requestedAttributes);

for (RoleV2 role : rolesResponse.getRoles()) {
ServerSideValidator.validateRetrievedSCIMObjectInList(role, schema,
Expand Down Expand Up @@ -415,7 +436,8 @@ private Node buildNode(String filter, SCIMResourceTypeSchema schema) throws BadR
* @throws CharonException CharonException.
* @throws BadRequestException BadRequestException.
*/
private SCIMResponse processRoleList(RolesV2GetResponse rolesResponse, JSONEncoder encoder, int startIndex)
private SCIMResponse processRoleList(RolesV2GetResponse rolesResponse, JSONEncoder encoder, int startIndex,
String attributes, String excludeAttributes)
throws CharonException, BadRequestException {

if (rolesResponse == null) {
Expand All @@ -425,8 +447,8 @@ private SCIMResponse processRoleList(RolesV2GetResponse rolesResponse, JSONEncod
rolesResponse.setRoles(Collections.emptyList());
}
for (RoleV2 role : rolesResponse.getRoles()) {
ServerSideValidator.validateSCIMObjectForRequiredAttributes(role,
SCIMSchemaDefinitions.SCIM_ROLE_V2_SCHEMA);
ServerSideValidator.validateRetrievedSCIMObject(role, SCIMSchemaDefinitions.SCIM_ROLE_V2_SCHEMA,
attributes, excludeAttributes);
}
// Create a listed resource object out of the returned groups list.
ListedResource listedResource = createListedResource(rolesResponse, startIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1120,23 +1120,23 @@ public static class SCIMRoleSchemaDefinition {
SCIMAttributeSchema.createSCIMAttributeSchema(SCIMConstants.RoleSchemaConstants.AUDIENCE_VALUE_URI,
SCIMConstants.RoleSchemaConstants.VALUE, SCIMDefinitions.DataType.STRING, false,
SCIMConstants.RoleSchemaConstants.AUDIENCE_VALUE_DESC, false, false,
SCIMDefinitions.Mutability.IMMUTABLE, SCIMDefinitions.Returned.DEFAULT,
SCIMDefinitions.Mutability.IMMUTABLE, SCIMDefinitions.Returned.ALWAYS,
SCIMDefinitions.Uniqueness.NONE, null, null, null);

// A human-readable name of role's audience.
public static final SCIMAttributeSchema AUDIENCE_DISPLAY =
SCIMAttributeSchema.createSCIMAttributeSchema(SCIMConstants.RoleSchemaConstants.AUDIENCE_DISPLAY_URI,
SCIMConstants.RoleSchemaConstants.DISPLAY, SCIMDefinitions.DataType.STRING, false,
SCIMConstants.RoleSchemaConstants.AUDIENCE_DISPLAY_DESC, false, false,
SCIMDefinitions.Mutability.IMMUTABLE, SCIMDefinitions.Returned.DEFAULT,
SCIMDefinitions.Mutability.IMMUTABLE, SCIMDefinitions.Returned.ALWAYS,
SCIMDefinitions.Uniqueness.NONE, null, null, null);

// Type of the role's audience (eg: application, organization).
public static final SCIMAttributeSchema AUDIENCE_TYPE =
SCIMAttributeSchema.createSCIMAttributeSchema(SCIMConstants.RoleSchemaConstants.AUDIENCE_TYPE_URI,
SCIMConstants.RoleSchemaConstants.TYPE, SCIMDefinitions.DataType.STRING, false,
SCIMConstants.RoleSchemaConstants.AUDIENCE_TYPE_DESC, false, false,
SCIMDefinitions.Mutability.IMMUTABLE, SCIMDefinitions.Returned.DEFAULT,
SCIMDefinitions.Mutability.IMMUTABLE, SCIMDefinitions.Returned.ALWAYS,
SCIMDefinitions.Uniqueness.NONE, new ArrayList<>(
Arrays.asList(SCIMConstants.RoleSchemaConstants.APPLICATION,
SCIMConstants.RoleSchemaConstants.ORGANIZATION)), null, null);
Expand All @@ -1157,7 +1157,7 @@ public static class SCIMRoleSchemaDefinition {
SCIMAttributeSchema.createSCIMAttributeSchema(SCIMConstants.RoleSchemaConstants.AUDIENCE_URI,
SCIMConstants.RoleSchemaConstants.AUDIENCE, SCIMDefinitions.DataType.COMPLEX, false,
SCIMConstants.RoleSchemaConstants.AUDIENCE_DESC, false, false,
SCIMDefinitions.Mutability.READ_WRITE, SCIMDefinitions.Returned.DEFAULT,
SCIMDefinitions.Mutability.READ_WRITE, SCIMDefinitions.Returned.ALWAYS,
SCIMDefinitions.Uniqueness.NONE, null, null,
new ArrayList<>(Arrays.asList(AUDIENCE_VALUE, AUDIENCE_DISPLAY, AUDIENCE_TYPE)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public static Map<String, Boolean> getOnlyRequiredAttributesURIs(SCIMResourceTyp
String requestedExcludingAttributes)
throws CharonException {

ArrayList<AttributeSchema> attributeSchemaArrayList =
getOnlyRequiredAttributes(schema, requestedAttributes, requestedExcludingAttributes);
return convertSchemasToURIs(attributeSchemaArrayList);
}

public static ArrayList<AttributeSchema> getOnlyRequiredAttributes(SCIMResourceTypeSchema schema,
String requestedAttributes,
String requestedExcludingAttributes)
throws CharonException {

ArrayList<AttributeSchema> attributeSchemaArrayList = (ArrayList<AttributeSchema>)
CopyUtil.deepCopy(schema.getAttributesList());

Expand Down Expand Up @@ -105,7 +115,7 @@ public static Map<String, Boolean> getOnlyRequiredAttributesURIs(SCIMResourceTyp
requestedAttributes, requestedExcludingAttributes,
requestedAttributesList, requestedExcludingAttributesList);
}
return convertSchemasToURIs(attributeSchemaArrayList);
return attributeSchemaArrayList;
}

/*
Expand Down
Loading