Skip to content

Commit

Permalink
Improve sub org application deletion when accessing through tenant pe…
Browse files Browse the repository at this point in the history
…rspective
  • Loading branch information
ShanChathusanda93 committed Nov 30, 2024
1 parent 3833c81 commit 466935c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,45 +467,51 @@ private void handleAddedApplicationAudienceRolesOnAppUpdate(List<RoleV2> addedAp
public boolean doPreDeleteApplication(String applicationName, String tenantDomain, String userName)
throws IdentityApplicationManagementException {

try {
// If the deleting application is an application of tenant(i.e primary org) nothing to do here.
if (!OrganizationManagementUtil.isOrganization(tenantDomain)) {
return true;
}
if (StringUtils.isEmpty(PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getApplicationResidentOrganizationId())) {
try {
// If the deleting application is an application of tenant(i.e primary org) nothing to do here.
if (!OrganizationManagementUtil.isOrganization(tenantDomain)) {
return true;
}

ServiceProvider sharedApplication = getApplicationByName(applicationName, tenantDomain);
if (sharedApplication == null) {
return false;
}
String sharedAppId = sharedApplication.getApplicationResourceId();
String sharedAppOrgId = organizationManager.resolveOrganizationId(tenantDomain);
// Resolve the main application details.
String mainAppId = orgApplicationManager.getMainApplicationIdForGivenSharedApp(sharedAppId, sharedAppOrgId);
if (mainAppId == null) {
return false;
}
int mainAppTenantId = applicationManagementService.getTenantIdByApp(mainAppId);
String mainAppTenantDomain = IdentityTenantUtil.getTenantDomain(mainAppTenantId);
ServiceProvider sharedApplication = getApplicationByName(applicationName, tenantDomain);
if (sharedApplication == null) {
return false;
}
String sharedAppId = sharedApplication.getApplicationResourceId();
String sharedAppOrgId = organizationManager.resolveOrganizationId(tenantDomain);
// Resolve the main application details.
String mainAppId = orgApplicationManager.getMainApplicationIdForGivenSharedApp(sharedAppId,
sharedAppOrgId);
if (mainAppId == null) {
return false;
}
int mainAppTenantId = applicationManagementService.getTenantIdByApp(mainAppId);
String mainAppTenantDomain = IdentityTenantUtil.getTenantDomain(mainAppTenantId);

String allowedAudienceForRoleAssociationInMainApp =
applicationManagementService.getAllowedAudienceForRoleAssociation(mainAppId,
mainAppTenantDomain);
boolean hasAppAudiencedRoles =
RoleConstants.APPLICATION.equalsIgnoreCase(allowedAudienceForRoleAssociationInMainApp);
if (hasAppAudiencedRoles) {
// Handle role deletion in application deletion post actions.
return true;
}

String allowedAudienceForRoleAssociationInMainApp =
applicationManagementService.getAllowedAudienceForRoleAssociation(mainAppId, mainAppTenantDomain);
boolean hasAppAudiencedRoles =
RoleConstants.APPLICATION.equalsIgnoreCase(allowedAudienceForRoleAssociationInMainApp);
if (hasAppAudiencedRoles) {
// Handle role deletion in application deletion post actions.
return true;
// Handing organization audienced roles associated case.
List<RoleV2> associatedRolesOfMainApplication = applicationManagementService
.getAssociatedRolesOfApplication(mainAppId, mainAppTenantDomain);
handleOrganizationAudiencedSharedRoleDeletion(associatedRolesOfMainApplication, mainAppId,
mainAppTenantDomain, sharedAppOrgId);
} catch (OrganizationManagementException | IdentityRoleManagementException e) {
throw new IdentityApplicationManagementException(
"Error while deleting organization roles associated to the app.", e);
}

// Handing organization audienced roles associated case.
List<RoleV2> associatedRolesOfMainApplication = applicationManagementService
.getAssociatedRolesOfApplication(mainAppId, mainAppTenantDomain);
handleOrganizationAudiencedSharedRoleDeletion(associatedRolesOfMainApplication, mainAppId,
mainAppTenantDomain, sharedAppOrgId);
} catch (OrganizationManagementException | IdentityRoleManagementException e) {
throw new IdentityApplicationManagementException(
"Error while deleting organization roles associated to the app.", e);
return super.doPreDeleteApplication(applicationName, tenantDomain, userName);
}
return super.doPreDeleteApplication(applicationName, tenantDomain, userName);
return true;
}

private void handleOrganizationAudiencedSharedRoleDeletion(List<RoleV2> rolesList, String mainApplicationId,
Expand Down Expand Up @@ -583,22 +589,18 @@ public boolean doPostGetAllowedAudienceForRoleAssociation(AssociatedRolesConfig
String applicationUUID, String tenantDomain)
throws IdentityApplicationManagementException {

try {
if (!OrganizationManagementUtil.isOrganization(tenantDomain)) {
return true;
}
// Resolve the allowed audience for associated roles of shared application from main application details.
String mainAppId = applicationManagementService.getMainAppId(applicationUUID);
int mainAppTenantId = applicationManagementService.getTenantIdByApp(mainAppId);
String mainAppTenantDomain = IdentityTenantUtil.getTenantDomain(mainAppTenantId);
String resolvedAllowedAudienceFromMainApp =
applicationManagementService.getAllowedAudienceForRoleAssociation(mainAppId, mainAppTenantDomain);
allowedAudienceForRoleAssociation.setAllowedAudience(resolvedAllowedAudienceFromMainApp);
} catch (OrganizationManagementException e) {
throw new IdentityApplicationManagementException(String.format(
"Error while fetching the allowed audience for role association of application with: %s.",
applicationUUID), e);
String mainAppId = applicationManagementService.getMainAppId(applicationUUID);
// If the main application id is null, then this is the main application. We can skip this operation based on
// that.
if (StringUtils.isEmpty(mainAppId)) {
return true;
}
// Resolve the allowed audience for associated roles of shared application from main application details.
int mainAppTenantId = applicationManagementService.getTenantIdByApp(mainAppId);
String mainAppTenantDomain = IdentityTenantUtil.getTenantDomain(mainAppTenantId);
String resolvedAllowedAudienceFromMainApp =
applicationManagementService.getAllowedAudienceForRoleAssociation(mainAppId, mainAppTenantDomain);
allowedAudienceForRoleAssociation.setAllowedAudience(resolvedAllowedAudienceFromMainApp);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@
<properties>

<!-- Carbon kernel version -->
<carbon.kernel.version>4.9.17</carbon.kernel.version>
<carbon.kernel.package.import.version.range>[4.7.0, 5.0.0)</carbon.kernel.package.import.version.range>
<carbon.kernel.feature.version>4.6.0</carbon.kernel.feature.version>
<carbon.kernel.version>4.10.25</carbon.kernel.version>
<carbon.kernel.package.import.version.range>[4.10.0, 5.0.0)</carbon.kernel.package.import.version.range>
<carbon.kernel.feature.version>4.10.0</carbon.kernel.feature.version>

<carbon.commons.imp.pkg.version.range>[4.7.0, 5.0.0)</carbon.commons.imp.pkg.version.range>

Expand Down

0 comments on commit 466935c

Please sign in to comment.