From 1588795adc9d5c49a697911bd2ed0cd26ac97ae6 Mon Sep 17 00:00:00 2001 From: Afra Hussaindeen Date: Wed, 25 Sep 2024 11:02:38 +0530 Subject: [PATCH] Update the GET applications API to exclude system apps when the `excludeSystemApps=true` query parameter is included. --- .../management/v1/ApplicationsApi.java | 4 +- .../management/v1/ApplicationsApiService.java | 2 +- .../ServerApplicationManagementService.java | 44 ++++++++++++++++--- .../v1/impl/ApplicationsApiServiceImpl.java | 4 +- .../src/main/resources/applications.yaml | 10 +++++ 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApi.java b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApi.java index f0cd6db86e..f331b807a3 100644 --- a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApi.java +++ b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApi.java @@ -481,9 +481,9 @@ public Response getAllApplicationTemplates( @Valid@ApiParam(value = "Maximum @ApiResponse(code = 500, message = "Server Error", response = Error.class), @ApiResponse(code = 501, message = "Not Implemented", response = Error.class) }) - public Response getAllApplications( @Valid @Min(1)@ApiParam(value = "Maximum number of records to return. ", defaultValue="30") @DefaultValue("30") @QueryParam("limit") Integer limit, @Valid@ApiParam(value = "Number of records to skip for pagination. ", defaultValue="0") @DefaultValue("0") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew', and 'eq' operations with 'and', 'or' logical operators. Please note that 'and' and 'or' operators in filters follow the general precedence of logical operators ex: A and B or C and D = (A and B) or (C and D)). Currently supports only filtering based on the 'name', the 'clientId', and the 'issuer' attributes. /applications?filter=name+eq+user_portal
/applications?filter=name+co+prod+or+clientId+co+123 ") @QueryParam("filter") String filter, @Valid@ApiParam(value = "Define the order in which the retrieved records should be sorted. _This parameter is not supported yet._ ", allowableValues="ASC, DESC") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. _This parameter is not supported yet._ ") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Specifies the required parameters in the response. Currently supports for only 'advancedConfigurations', 'templateId', 'templateVersion', 'clientId', 'issuer', 'applicationEnabled' and 'associatedRoles.allowedAudience' attributes. /applications?attributes=advancedConfigurations,templateId,templateVersion,clientId,applicationEnabled,associatedRoles.allowedAudience ") @QueryParam("attributes") String attributes) { + public Response getAllApplications( @Valid @Min(1)@ApiParam(value = "Maximum number of records to return. ", defaultValue="30") @DefaultValue("30") @QueryParam("limit") Integer limit, @Valid@ApiParam(value = "Number of records to skip for pagination. ", defaultValue="0") @DefaultValue("0") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew', and 'eq' operations with 'and', 'or' logical operators. Please note that 'and' and 'or' operators in filters follow the general precedence of logical operators ex: A and B or C and D = (A and B) or (C and D)). Currently supports only filtering based on the 'name', the 'clientId', and the 'issuer' attributes. /applications?filter=name+eq+user_portal
/applications?filter=name+co+prod+or+clientId+co+123 ") @QueryParam("filter") String filter, @Valid@ApiParam(value = "Define the order in which the retrieved records should be sorted. _This parameter is not supported yet._ ", allowableValues="ASC, DESC") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. _This parameter is not supported yet._ ") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Specifies the required parameters in the response. Currently supports for only 'advancedConfigurations', 'templateId', 'templateVersion', 'clientId', 'issuer', 'applicationEnabled' and 'associatedRoles.allowedAudience' attributes. /applications?attributes=advancedConfigurations,templateId,templateVersion,clientId,applicationEnabled,associatedRoles.allowedAudience ") @QueryParam("attributes") String attributes, @Valid@ApiParam(value = "Specifies whether to exclude system applications when exporting applications. ", defaultValue="false") @DefaultValue("false") @QueryParam("excludeSystemApps") Boolean excludeSystemApps) { - return delegate.getAllApplications(limit, offset, filter, sortOrder, sortBy, attributes ); + return delegate.getAllApplications(limit, offset, filter, sortOrder, sortBy, attributes, excludeSystemApps ); } @Valid diff --git a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApiService.java b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApiService.java index c90527e09b..2f22652efd 100644 --- a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApiService.java +++ b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/application/management/v1/ApplicationsApiService.java @@ -88,7 +88,7 @@ public interface ApplicationsApiService { public Response getAllApplicationTemplates(Integer limit, Integer offset, SearchContext searchContext); - public Response getAllApplications(Integer limit, Integer offset, String filter, String sortOrder, String sortBy, String attributes); + public Response getAllApplications(Integer limit, Integer offset, String filter, String sortOrder, String sortBy, String attributes, Boolean excludeSystemApps); public Response getApplication(String applicationId); diff --git a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/core/ServerApplicationManagementService.java b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/core/ServerApplicationManagementService.java index 98b26b3272..69609e5e35 100644 --- a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/core/ServerApplicationManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/core/ServerApplicationManagementService.java @@ -252,7 +252,8 @@ public class ServerApplicationManagementService { private ServerApplicationMetadataService applicationMetadataService; public ApplicationListResponse getAllApplications(Integer limit, Integer offset, String filter, String sortOrder, - String sortBy, String requiredAttributes) { + String sortBy, String requiredAttributes, + Boolean excludeSystemApps) { handleNotImplementedCapabilities(sortOrder, sortBy); String tenantDomain = ContextLoader.getTenantDomainFromContext(); @@ -279,9 +280,39 @@ public ApplicationListResponse getAllApplications(Integer limit, Integer offset, int totalResults = getApplicationManagementService() .getCountOfApplications(tenantDomain, username, filter); - ApplicationBasicInfo[] filteredAppList = getApplicationManagementService() - .getApplicationBasicInfo(tenantDomain, username, filter, offset, limit); - int resultsInCurrentPage = filteredAppList.length; + List filteredAppList = new ArrayList<>(); + int currentOffset = offset; + int remainingLimit = limit; + + while (filteredAppList.size() < limit && currentOffset < totalResults) { + ApplicationBasicInfo[] batch = getApplicationManagementService() + .getApplicationBasicInfo(tenantDomain, username, filter, currentOffset, remainingLimit); + + if (batch.length == 0) { + break; + } + + for (ApplicationBasicInfo app : batch) { + if (Boolean.TRUE.equals(excludeSystemApps) && + (CONSOLE_APP.equals(app.getApplicationName()) || + MY_ACCOUNT_APP.equals(app.getApplicationName()))) { + continue; + } + filteredAppList.add(app); + if (filteredAppList.size() == limit) { + break; + } + } + + currentOffset += batch.length; + remainingLimit = limit - filteredAppList.size(); + } + + int resultsInCurrentPage = filteredAppList.size(); + + if (Boolean.TRUE.equals(excludeSystemApps)) { + totalResults = totalResults - 2; // Subtract 2 for CONSOLE_APP and MY_ACCOUNT_APP + } List requestedAttributeList = new ArrayList<>(); if (StringUtils.isNotEmpty(requiredAttributes)) { @@ -299,7 +330,8 @@ public ApplicationListResponse getAllApplications(Integer limit, Integer offset, } if (CollectionUtils.isNotEmpty(requestedAttributeList)) { - List serviceProviderList = getSpWithRequiredAttributes(filteredAppList, + List serviceProviderList = getSpWithRequiredAttributes( + filteredAppList.toArray(new ApplicationBasicInfo[0]), requestedAttributeList); return new ApplicationListResponse() @@ -318,7 +350,7 @@ public ApplicationListResponse getAllApplications(Integer limit, Integer offset, .totalResults(totalResults) .startIndex(offset + 1) .count(resultsInCurrentPage) - .applications(getApplicationListItems(filteredAppList)) + .applications(getApplicationListItems(filteredAppList.toArray(new ApplicationBasicInfo[0]))) .links(Util.buildPaginationLinks(limit, offset, totalResults, APPLICATION_MANAGEMENT_PATH_COMPONENT, requiredAttributes, filter) .entrySet() diff --git a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/impl/ApplicationsApiServiceImpl.java b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/impl/ApplicationsApiServiceImpl.java index d6fef0ca9b..b96aadf4a2 100644 --- a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/impl/ApplicationsApiServiceImpl.java +++ b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/java/org/wso2/carbon/identity/api/server/application/management/v1/impl/ApplicationsApiServiceImpl.java @@ -69,10 +69,10 @@ public class ApplicationsApiServiceImpl implements ApplicationsApiService { @Override public Response getAllApplications(Integer limit, Integer offset, String filter, String sortOrder, String sortBy, - String requiredAttributes) { + String requiredAttributes, Boolean excludeSystemApps) { ApplicationListResponse listResponse = applicationManagementService - .getAllApplications(limit, offset, filter, sortOrder, sortBy, requiredAttributes); + .getAllApplications(limit, offset, filter, sortOrder, sortBy, requiredAttributes, excludeSystemApps); return Response.ok().entity(listResponse).build(); } diff --git a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/resources/applications.yaml b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/resources/applications.yaml index e0b0a05343..5c05216990 100644 --- a/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/resources/applications.yaml +++ b/components/org.wso2.carbon.identity.api.server.application.management/org.wso2.carbon.identity.api.server.application.management.v1/src/main/resources/applications.yaml @@ -38,6 +38,7 @@ paths: - $ref: '#/components/parameters/sortOrderQueryParam' - $ref: '#/components/parameters/sortByQueryParam' - $ref: '#/components/parameters/attributesQueryParam' + - $ref: '#/components/parameters/excludeSystemAppsQueryParam' responses: '200': description: OK @@ -2701,6 +2702,15 @@ components: /applications?attributes=advancedConfigurations,templateId,templateVersion,clientId,applicationEnabled,associatedRoles.allowedAudience schema: type: string + excludeSystemAppsQueryParam: + in: query + name: excludeSystemApps + required: false + description: | + Specifies whether to exclude system applications when exporting applications. + schema: + type: boolean + default: false exportSecretsQueryParam: in: query name: exportSecrets