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 825f9f82e..9a8b30b07 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 @@ -67,10 +67,17 @@ public void handleEvent(Event event) throws IdentityEventException { if (StringUtils.isEmpty(associatedOrgId)) { associatedOrgId = getOrganizationManager().resolveOrganizationId(Utils.getTenantDomain()); } - userSharingService.shareOrganizationUser(orgId, associatedUserId, associatedOrgId); - String userId = userSharingService.getUserAssociationOfAssociatedUserByOrgId(associatedUserId, orgId) - .getUserId(); - assignUserToAdminRole(userId, orgId, tenantDomain); + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); + userSharingService.shareOrganizationUser(orgId, associatedUserId, associatedOrgId); + String userId = userSharingService + .getUserAssociationOfAssociatedUserByOrgId(associatedUserId, orgId) + .getUserId(); + assignUserToAdminRole(userId, orgId, tenantDomain); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } } catch (OrganizationManagementException e) { throw new IdentityEventException("An error occurred while sharing the organization creator to the " + "organization : " + orgId, e); @@ -93,15 +100,9 @@ private void assignUserToAdminRole(String userId, String organizationId, String try { String adminRoleId = OrganizationUserSharingDataHolder.getInstance().getRoleManagementService() .getRoleIdByName(adminRoleName, RoleConstants.ORGANIZATION, organizationId, tenantDomain); - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); - OrganizationUserSharingDataHolder.getInstance().getRoleManagementService() - .updateUserListOfRole(adminRoleId, - Collections.singletonList(userId), Collections.emptyList(), tenantDomain); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } + OrganizationUserSharingDataHolder.getInstance().getRoleManagementService() + .updateUserListOfRole(adminRoleId, Collections.singletonList(userId), Collections.emptyList(), + tenantDomain); } catch (IdentityRoleManagementException e) { throw new IdentityEventException("An error occurred while assigning the user to the administrator role", e); }