From c328a8942d571c25355cba88dda4ce2600203ee6 Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Fri, 10 Jan 2025 14:04:25 +0530 Subject: [PATCH 1/4] Address multiple users exist in different user stores when recovering username --- .../impl/UserAccountRecoveryManager.java | 2 +- .../username/UsernameRecoveryManagerImpl.java | 5 ++++- .../carbon/identity/recovery/util/Utils.java | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/UserAccountRecoveryManager.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/UserAccountRecoveryManager.java index e9c5e32d8..e5a138a0c 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/UserAccountRecoveryManager.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/UserAccountRecoveryManager.java @@ -138,7 +138,7 @@ public RecoveryChannelInfoDTO retrieveUsersRecoveryInformationForUsername(Map properties = new HashMap<>(); properties.put(IdentityEventConstants.EventProperty.USER_NAME, username); properties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, user.getTenantDomain()); - properties.put(IdentityEventConstants.EventProperty.USER_STORE_DOMAIN, user.getUserStoreDomain()); + properties.put(IdentityEventConstants.EventProperty.USER_STORE_DOMAIN, userStoreDomain); properties.put(IdentityEventConstants.EventProperty.NOTIFICATION_CHANNEL, notificationChannel); if (metaProperties != null) { for (String key : metaProperties.keySet()) { diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java index 1a3e977fb..de78b9f7a 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java @@ -1066,10 +1066,25 @@ public static void validateEmailUsername(User user) throws IdentityRecoveryClien */ public static User buildUser(String username, String tenantDomain) { + String[] parts = username.split(","); User user = new User(); - user.setUserName(UserCoreUtil.removeDomainFromName(username)); user.setTenantDomain(tenantDomain); - user.setUserStoreDomain(IdentityUtil.extractDomainFromName(username)); + + for (String part : parts) { + String domainFreeName = UserCoreUtil.removeDomainFromName(part); + if (user.getUserName() != null) { + user.setUserName(user.getUserName() + "," + domainFreeName); + } else { + user.setUserName(domainFreeName); + } + + String userStoreDomain = IdentityUtil.extractDomainFromName(part); + if (user.getUserStoreDomain() != null) { + user.setUserStoreDomain(user.getUserStoreDomain() + "," + userStoreDomain); + } else { + user.setUserStoreDomain(userStoreDomain); + } + } return user; } From cd32e9f09220b2876ca7117d46552e4ade91ba3c Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Fri, 10 Jan 2025 14:24:09 +0530 Subject: [PATCH 2/4] Fix test failures --- .../impl/username/UsernameRecoveryManagerImpl.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java index a6e91f316..cec8957c6 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java @@ -355,13 +355,22 @@ private void triggerNotification(User user, String notificationChannel, String e String combinedUsernames = user.getUserName(); String[] usernames = combinedUsernames.split(","); + String[] userStoreDomains = null; + if (user.getUserStoreDomain() != null) { + userStoreDomains = user.getUserStoreDomain().split(","); + } + int userIndex = 0; for (String username : usernames) { - String userStoreDomain = user.getUserStoreDomain().split(",")[userIndex]; - userIndex++; HashMap properties = new HashMap<>(); properties.put(IdentityEventConstants.EventProperty.USER_NAME, username); properties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, user.getTenantDomain()); + + String userStoreDomain = user.getUserStoreDomain(); + if (userStoreDomains != null && userStoreDomains.length > userIndex) { + userStoreDomain = userStoreDomains[userIndex]; + } + userIndex++; properties.put(IdentityEventConstants.EventProperty.USER_STORE_DOMAIN, userStoreDomain); properties.put(IdentityEventConstants.EventProperty.NOTIFICATION_CHANNEL, notificationChannel); if (metaProperties != null) { From 3b0a1ba34f301ae4e281ad1c345446c61477b2af Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Fri, 10 Jan 2025 14:26:41 +0530 Subject: [PATCH 3/4] Update license --- .../service/impl/username/UsernameRecoveryManagerImpl.java | 7 ++++--- .../java/org/wso2/carbon/identity/recovery/util/Utils.java | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java index cec8957c6..60f2ac6f2 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/internal/service/impl/username/UsernameRecoveryManagerImpl.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2020, WSO2 LLC. (http://www.wso2.org) + * Copyright (c) 2020-2025, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * 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 * @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.wso2.carbon.identity.recovery.internal.service.impl.username; import org.apache.commons.collections.MapUtils; diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java index de78b9f7a..9d830e2e0 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2024, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2016-2025, 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 From 6c38590adff6b65b9af0c9c9648b6864c85155e2 Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Fri, 10 Jan 2025 16:32:17 +0530 Subject: [PATCH 4/4] Address review comments --- .../java/org/wso2/carbon/identity/recovery/util/Utils.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java index 9d830e2e0..d050095d8 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/util/Utils.java @@ -1066,11 +1066,13 @@ public static void validateEmailUsername(User user) throws IdentityRecoveryClien */ public static User buildUser(String username, String tenantDomain) { - String[] parts = username.split(","); + /* The User Account Recovery process can identify multiple users that match the specified conditions. + In such cases, the usernames are represented as a comma-separated list. */ + String[] usernameSegments = username.split(","); User user = new User(); user.setTenantDomain(tenantDomain); - for (String part : parts) { + for (String part : usernameSegments) { String domainFreeName = UserCoreUtil.removeDomainFromName(part); if (user.getUserName() != null) { user.setUserName(user.getUserName() + "," + domainFreeName);