From 1ef5e85696a2ebabe171a018178d02be935c2b9d Mon Sep 17 00:00:00 2001 From: shanggeeth Date: Sun, 19 Nov 2023 17:33:59 +0530 Subject: [PATCH] Add request initiating username to privileged carbon context when starting tenant flow --- .../organization/management/handler/SharedRoleMgtHandler.java | 4 ++++ .../listener/SharingOrganizationCreatorUserEventHandler.java | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/components/org.wso2.carbon.identity.organization.management.handler/src/main/java/org/wso2/carbon/identity/organization/management/handler/SharedRoleMgtHandler.java b/components/org.wso2.carbon.identity.organization.management.handler/src/main/java/org/wso2/carbon/identity/organization/management/handler/SharedRoleMgtHandler.java index 1d105420c..371337d44 100644 --- a/components/org.wso2.carbon.identity.organization.management.handler/src/main/java/org/wso2/carbon/identity/organization/management/handler/SharedRoleMgtHandler.java +++ b/components/org.wso2.carbon.identity.organization.management.handler/src/main/java/org/wso2/carbon/identity/organization/management/handler/SharedRoleMgtHandler.java @@ -202,10 +202,14 @@ private void createSharedRolesOnNewRoleCreation(Map eventPropert String sharedOrganizationId = sharedApplications.get(taskId).getOrganizationId(); String shareAppTenantDomain = getOrganizationManager().resolveTenantDomain(sharedOrganizationId); + String associatedUserName = + PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext() .setTenantDomain(shareAppTenantDomain, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext() + .setUsername(associatedUserName); RoleBasicInfo sharedRoleInfo = getRoleManagementServiceV2().addRole(mainRoleName, Collections.emptyList(), Collections.emptyList(), diff --git a/components/org.wso2.carbon.identity.organization.management.organization.user.sharing/src/main/java/org/wso2/carbon/identity/organization/management/organization/user/sharing/listener/SharingOrganizationCreatorUserEventHandler.java b/components/org.wso2.carbon.identity.organization.management.organization.user.sharing/src/main/java/org/wso2/carbon/identity/organization/management/organization/user/sharing/listener/SharingOrganizationCreatorUserEventHandler.java index 8c4b0f07f..fe6643e8c 100644 --- a/components/org.wso2.carbon.identity.organization.management.organization.user.sharing/src/main/java/org/wso2/carbon/identity/organization/management/organization/user/sharing/listener/SharingOrganizationCreatorUserEventHandler.java +++ b/components/org.wso2.carbon.identity.organization.management.organization.user.sharing/src/main/java/org/wso2/carbon/identity/organization/management/organization/user/sharing/listener/SharingOrganizationCreatorUserEventHandler.java @@ -74,9 +74,11 @@ public void handleEvent(Event event) throws IdentityEventException { return; } String associatedUserId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserId(); + String associatedUserName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(associatedUserName); Role organizationCreatorRole = buildOrgCreatorRole(associatedUserId); Role administratorRole = buildAdministratorRole(associatedUserId); getRoleManager().createRole(orgId, organizationCreatorRole); @@ -94,6 +96,7 @@ public void handleEvent(Event event) throws IdentityEventException { if (!OrganizationManagementUtil.isOrganization(tenantDomain)) { return; } + String associatedUserName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String associatedUserId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserId(); String associatedOrgId = PrivilegedCarbonContext.getThreadLocalCarbonContext() .getUserResidentOrganizationId(); @@ -103,6 +106,7 @@ public void handleEvent(Event event) throws IdentityEventException { try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(associatedUserName); userSharingService.shareOrganizationUser(orgId, associatedUserId, associatedOrgId); String userId = userSharingService .getUserAssociationOfAssociatedUserByOrgId(associatedUserId, orgId)