Skip to content

Commit

Permalink
Merge pull request #393 from SujanSanjula96/improve-app-sharing
Browse files Browse the repository at this point in the history
Improve app sharing logic during organization creation
  • Loading branch information
SujanSanjula96 authored Oct 2, 2024
2 parents e947a46 + fe44d6f commit 8fad889
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<BasicOrganization> 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);
}
}
Expand Down Expand Up @@ -242,6 +239,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();
Expand Down

0 comments on commit 8fad889

Please sign in to comment.