Skip to content

Commit

Permalink
resolved associated role names in shared app
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuradhaSK committed Oct 17, 2023
1 parent 1db510d commit 5113a49
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.claim.metadata.mgt</artifactId>
</dependency>

<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.role.v2.mgt.core</artifactId>
</dependency>
<!--Test Dependencies-->
<dependency>
<groupId>org.testng</groupId>
Expand Down Expand Up @@ -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}",
</Import-Package>
</instructions>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -46,8 +47,9 @@ public class OrgApplicationMgtDataHolder {
private IdpManager idpManager;
private ApplicationSharingManagerListener applicationSharingManagerListener;
private IdentityEventService identityEventService;

private ClaimMetadataManagementService claimMetadataManagementService;
private RoleManagementService roleManagementServiceV2;

private OrgApplicationMgtDataHolder() {

}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<RoleV2> mainAppRoles = associatedRolesConfigOfMainApp.getRoles();
AssociatedRolesConfig associatedRolesConfigForSharedApp = new AssociatedRolesConfig();
associatedRolesConfigForSharedApp.setAllowedAudience(allowedAudience);
// TODO resolve to shared role id reference and re build.
List<RoleV2> associatedRolesOfSharedApp = mainAppRoles;
List<String> mainAppRoleIds = mainAppRoles.stream().map(RoleV2::getId).collect(Collectors.toList());
Map<String, String> mainRoleToSharedRoleMappingsBySubOrg =
getRoleManagementServiceV2().getMainRoleToSharedRoleMappingsBySubOrg(mainAppRoleIds,
tenantDomainOfSharedApp);

List<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 = 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;
}
Expand Down Expand Up @@ -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.
*
Expand Down
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@
<artifactId>org.wso2.carbon.identity.governance</artifactId>
<version>${org.wso2.carbon.identity.governance.version}</version>
</dependency>

<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.role.v2.mgt.core</artifactId>
<version>${carbon.identity.framework.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand Down Expand Up @@ -494,7 +498,7 @@
<carbon.multitenancy.package.import.version.range>[4.7.0,5.0.0)
</carbon.multitenancy.package.import.version.range>

<carbon.identity.framework.version>5.25.369</carbon.identity.framework.version>
<carbon.identity.framework.version>5.25.374</carbon.identity.framework.version>
<carbon.identity.package.import.version.range>[5.20.0, 7.0.0)
</carbon.identity.package.import.version.range>

Expand Down

0 comments on commit 5113a49

Please sign in to comment.