From 5113a49408ed86c495f857a416a3c939fc02830a Mon Sep 17 00:00:00 2001 From: AnuradhaSK Date: Tue, 17 Oct 2023 13:28:10 +0530 Subject: [PATCH] resolved associated role names in shared app --- .../pom.xml | 7 ++- .../internal/OrgApplicationMgtDataHolder.java | 24 ++++++++++- .../OrgApplicationMgtServiceComponent.java | 19 ++++++++ .../FragmentApplicationMgtListener.java | 43 ++++++++++++++++--- pom.xml | 8 +++- 5 files changed, 91 insertions(+), 10 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/pom.xml b/components/org.wso2.carbon.identity.organization.management.application/pom.xml index 025cf3b7d..50a930795 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/pom.xml +++ b/components/org.wso2.carbon.identity.organization.management.application/pom.xml @@ -86,7 +86,10 @@ org.wso2.carbon.identity.framework org.wso2.carbon.identity.claim.metadata.mgt - + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.role.v2.mgt.core + org.testng @@ -181,6 +184,8 @@ org.wso2.carbon.identity.event.event; version="${carbon.identity.package.import.version.range}", org.wso2.carbon.identity.event; version="${carbon.identity.package.import.version.range}", org.wso2.carbon.identity.event.services; version="${carbon.identity.package.import.version.range}", + org.wso2.carbon.identity.role.v2.mgt.core.*; + version="${carbon.identity.package.import.version.range}", diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtDataHolder.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtDataHolder.java index d4f92bad4..938a7b185 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtDataHolder.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtDataHolder.java @@ -26,6 +26,7 @@ import org.wso2.carbon.identity.organization.management.application.listener.ApplicationSharingManagerListener; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService; +import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService; import org.wso2.carbon.idp.mgt.IdpManager; import org.wso2.carbon.user.core.service.RealmService; @@ -46,8 +47,9 @@ public class OrgApplicationMgtDataHolder { private IdpManager idpManager; private ApplicationSharingManagerListener applicationSharingManagerListener; private IdentityEventService identityEventService; - private ClaimMetadataManagementService claimMetadataManagementService; + private RoleManagementService roleManagementServiceV2; + private OrgApplicationMgtDataHolder() { } @@ -248,4 +250,24 @@ public void setIdentityEventService(IdentityEventService identityEventService) { this.identityEventService = identityEventService; } + + /** + * Get {@link RoleManagementService}. + * + * @return RoleManagementService. + */ + public RoleManagementService getRoleManagementServiceV2() { + + return roleManagementServiceV2; + } + + /** + * Set {@link RoleManagementService}. + * + * @param roleManagementServiceV2 Instance of {@link RoleManagementService}. + */ + public void setRoleManagementServiceV2(RoleManagementService roleManagementServiceV2) { + + this.roleManagementServiceV2 = roleManagementServiceV2; + } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtServiceComponent.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtServiceComponent.java index 18b43c22f..a39ef0b38 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtServiceComponent.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/internal/OrgApplicationMgtServiceComponent.java @@ -42,6 +42,7 @@ import org.wso2.carbon.identity.organization.management.application.listener.OrganizationCreationHandler; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService; +import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService; import org.wso2.carbon.idp.mgt.IdpManager; import org.wso2.carbon.user.core.service.RealmService; @@ -238,4 +239,22 @@ protected void unsetClaimMetaDataManagementService(ClaimMetadataManagementServic log.debug("Unset the claim metadata management service."); OrgApplicationMgtDataHolder.getInstance().setClaimMetadataManagementService(null); } + + @Reference( + name = "org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService", + service = org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetRoleManagementServiceV2") + protected void setRoleManagementServiceV2(RoleManagementService roleManagementService) { + + OrgApplicationMgtDataHolder.getInstance().setRoleManagementServiceV2(roleManagementService); + log.debug("RoleManagementServiceV2 set in OrgApplicationMgtServiceComponent bundle."); + } + + protected void unsetRoleManagementServiceV2(RoleManagementService roleManagementService) { + + OrgApplicationMgtDataHolder.getInstance().setRoleManagementServiceV2(null); + log.debug("RoleManagementServiceV2 unset in OrgApplicationMgtServiceComponent bundle."); + } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListener.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListener.java index 0758bc8e0..1cf35d9f0 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListener.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/FragmentApplicationMgtListener.java @@ -47,11 +47,14 @@ import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementClientException; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; +import org.wso2.carbon.identity.role.v2.mgt.core.IdentityRoleManagementException; +import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import static java.lang.String.format; import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.DELETE_FRAGMENT_APPLICATION; @@ -218,11 +221,11 @@ public boolean doPostGetServiceProvider(ServiceProvider serviceProvider, String AssociatedRolesConfig associatedRolesConfigOfMainApp = mainApplication.getAssociatedRolesConfig(); if (associatedRolesConfigOfMainApp != null) { AssociatedRolesConfig associatedRolesConfigForSharedApp = - getAssociatedRolesConfigForSharedApp(associatedRolesConfigOfMainApp); + getAssociatedRolesConfigForSharedApp(associatedRolesConfigOfMainApp, tenantDomain); serviceProvider.setAssociatedRolesConfig(associatedRolesConfigForSharedApp); } } - } catch (OrganizationManagementException e) { + } catch (OrganizationManagementException | IdentityRoleManagementException e) { throw new IdentityApplicationManagementException ("Error while retrieving the fragment application details.", e); } @@ -231,14 +234,37 @@ public boolean doPostGetServiceProvider(ServiceProvider serviceProvider, String } private AssociatedRolesConfig getAssociatedRolesConfigForSharedApp( - AssociatedRolesConfig associatedRolesConfigOfMainApp) { + AssociatedRolesConfig associatedRolesConfigOfMainApp, String tenantDomainOfSharedApp) + throws IdentityRoleManagementException { String allowedAudience = associatedRolesConfigOfMainApp.getAllowedAudience(); List mainAppRoles = associatedRolesConfigOfMainApp.getRoles(); AssociatedRolesConfig associatedRolesConfigForSharedApp = new AssociatedRolesConfig(); associatedRolesConfigForSharedApp.setAllowedAudience(allowedAudience); - // TODO resolve to shared role id reference and re build. - List associatedRolesOfSharedApp = mainAppRoles; + List mainAppRoleIds = mainAppRoles.stream().map(RoleV2::getId).collect(Collectors.toList()); + Map mainRoleToSharedRoleMappingsBySubOrg = + getRoleManagementServiceV2().getMainRoleToSharedRoleMappingsBySubOrg(mainAppRoleIds, + tenantDomainOfSharedApp); + + List associatedRolesOfSharedApp = mainRoleToSharedRoleMappingsBySubOrg.entrySet().stream() + .map(entry -> { + String sharedRoleId = entry.getValue(); + String mainRoleId = entry.getKey(); + + // Find the main role by ID and retrieve its name. + String mainRoleName = mainAppRoles.stream() + .filter(role -> role.getId().equals(mainRoleId)) + .findFirst() + .map(RoleV2::getName) + .orElse(null); + + RoleV2 sharedRole = new RoleV2(); + sharedRole.setId(sharedRoleId); + sharedRole.setName(mainRoleName); + return sharedRole; + }) + .collect(Collectors.toList()); + associatedRolesConfigForSharedApp.setRoles(associatedRolesOfSharedApp); return associatedRolesConfigForSharedApp; } @@ -329,6 +355,11 @@ private OrganizationManager getOrganizationManager() { return OrgApplicationMgtDataHolder.getInstance().getOrganizationManager(); } + private RoleManagementService getRoleManagementServiceV2() { + + return OrgApplicationMgtDataHolder.getInstance().getRoleManagementServiceV2(); + } + /** * Add application roles claim mapping to the filtered claim mappings. * diff --git a/pom.xml b/pom.xml index a9b24887c..a9f197438 100644 --- a/pom.xml +++ b/pom.xml @@ -235,7 +235,11 @@ org.wso2.carbon.identity.governance ${org.wso2.carbon.identity.governance.version} - + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.role.v2.mgt.core + ${carbon.identity.framework.version} + com.google.code.findbugs annotations @@ -494,7 +498,7 @@ [4.7.0,5.0.0) - 5.25.369 + 5.25.374 [5.20.0, 7.0.0)