From fe44d6f64690ccc620be5e74520f04e9971ce650 Mon Sep 17 00:00:00 2001 From: SujanSanjula96 Date: Mon, 30 Sep 2024 17:38:16 +0530 Subject: [PATCH] Improve app sharing logic during organization creation --- .../listener/OrganizationCreationHandler.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java index 64c64f3da..3149c5b3f 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java @@ -53,6 +53,7 @@ import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.SHARE_WITH_ALL_CHILDREN; import static org.wso2.carbon.identity.organization.management.application.util.OrgApplicationManagerUtil.setIsAppSharedProperty; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.IS_APP_SHARED; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.SUPER_ORG_ID; import static org.wso2.carbon.identity.organization.management.service.util.Utils.getAuthenticatedUsername; @@ -143,17 +144,13 @@ private void addSharedApplicationsToOrganization(Organization organization) if (mainApplication != null && Arrays.stream(mainApplication.getSpProperties()) .anyMatch(p -> SHARE_WITH_ALL_CHILDREN.equalsIgnoreCase( p.getName()) && Boolean.parseBoolean(p.getValue()))) { - String mainAppOrgId = getOrganizationManager().resolveOrganizationId(mainApplication - .getTenantDomain()); - List applicationSharedOrganizations = getOrgApplicationManager() - .getApplicationSharedOrganizations(mainAppOrgId, - mainApplication.getApplicationResourceId()); - // Having an empty list implies that this is the first organization to which the application is - // shared with. - boolean updateIsAppSharedProperty = CollectionUtils.isEmpty(applicationSharedOrganizations); getOrgApplicationManager().shareApplication(parentOrgId, organization.getId(), mainApplication, true); - if (updateIsAppSharedProperty) { + // Check whether the application is shared with any child organization using `isAppShared` property. + boolean isAppShared = isAppShared(mainApplication); + if (!isAppShared) { + // Update the `isAppShared` property of the main application to true if it hasn't been shared + // previously. updateApplicationWithIsAppSharedProperty(true, mainApplication); } } @@ -238,6 +235,18 @@ private void updateApplicationWithIsAppSharedProperty(boolean isAppShared, Servi } } + /** + * Return the value of the `isAppShared` property of the main application. + * + * @param mainApplication The main application service provider object. + * @return True if the `isAppShared` property of the main application is set as true. + */ + private boolean isAppShared(ServiceProvider mainApplication) { + + return Arrays.stream(mainApplication.getSpProperties()) + .anyMatch(p -> IS_APP_SHARED.equalsIgnoreCase(p.getName()) && Boolean.parseBoolean(p.getValue())); + } + private ApplicationManagementService getApplicationManagementService() { return OrgApplicationMgtDataHolder.getInstance().getApplicationManagementService();