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 8a243b114..464632673 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/pom.xml +++ b/components/org.wso2.carbon.identity.organization.management.application/pom.xml @@ -1,18 +1,20 @@ @@ -86,7 +88,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 +186,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..9e7c3d130 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 @@ -1,7 +1,7 @@ /* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com). + * Copyright (c) 2022-2023, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, + * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at @@ -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..9799f3451 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2022-2023, WSO2 LLC. (http://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -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 b336a5eb3..58395f6c7 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 @@ -24,10 +24,12 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; +import org.wso2.carbon.identity.application.common.model.AssociatedRolesConfig; import org.wso2.carbon.identity.application.common.model.Claim; import org.wso2.carbon.identity.application.common.model.ClaimConfig; import org.wso2.carbon.identity.application.common.model.ClaimMapping; import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig; +import org.wso2.carbon.identity.application.common.model.RoleV2; import org.wso2.carbon.identity.application.common.model.ServiceProvider; import org.wso2.carbon.identity.application.common.model.ServiceProviderProperty; import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig; @@ -45,10 +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.RoleManagementService; +import org.wso2.carbon.identity.role.v2.mgt.core.exception.IdentityRoleManagementException; 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; @@ -210,8 +216,16 @@ public boolean doPostGetServiceProvider(ServiceProvider serviceProvider, String .setUseUserstoreDomainInRoles(mainApplication .getLocalAndOutBoundAuthenticationConfig().isUseUserstoreDomainInRoles()); } + + // Set application's associated roles. + AssociatedRolesConfig associatedRolesConfigOfMainApp = mainApplication.getAssociatedRolesConfig(); + if (associatedRolesConfigOfMainApp != null) { + AssociatedRolesConfig associatedRolesConfigForSharedApp = + getAssociatedRolesConfigForSharedApp(associatedRolesConfigOfMainApp, tenantDomain); + serviceProvider.setAssociatedRolesConfig(associatedRolesConfigForSharedApp); + } } - } catch (OrganizationManagementException e) { + } catch (OrganizationManagementException | IdentityRoleManagementException e) { throw new IdentityApplicationManagementException ("Error while retrieving the fragment application details.", e); } @@ -219,6 +233,44 @@ public boolean doPostGetServiceProvider(ServiceProvider serviceProvider, String return super.doPostGetServiceProvider(serviceProvider, applicationName, tenantDomain); } + private AssociatedRolesConfig getAssociatedRolesConfigForSharedApp( + AssociatedRolesConfig associatedRolesConfigOfMainApp, String tenantDomainOfSharedApp) + throws IdentityRoleManagementException { + + String allowedAudience = associatedRolesConfigOfMainApp.getAllowedAudience(); + RoleV2[] mainAppRoles = associatedRolesConfigOfMainApp.getRoles(); + List mainappRoleList = Arrays.asList(mainAppRoles); + AssociatedRolesConfig associatedRolesConfigForSharedApp = new AssociatedRolesConfig(); + associatedRolesConfigForSharedApp.setAllowedAudience(allowedAudience); + List mainAppRoleIds = + mainappRoleList.stream().map(RoleV2::getId).collect(Collectors.toList()); + Map mainRoleToSharedRoleMappingsBySubOrg = + getRoleManagementServiceV2().getMainRoleToSharedRoleMappingsBySubOrg(mainAppRoleIds, + tenantDomainOfSharedApp); + + RoleV2[] 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 = mainappRoleList.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; + }) + .toArray(RoleV2[]::new); + + associatedRolesConfigForSharedApp.setRoles(associatedRolesOfSharedApp); + return associatedRolesConfigForSharedApp; + } + @Override public boolean doPreDeleteApplication(String applicationName, String tenantDomain, String userName) throws IdentityApplicationManagementException { @@ -305,6 +357,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. *