From 7d7b25095a6f1155b774c04542493ea543bfc184 Mon Sep 17 00:00:00 2001 From: Afra Hussaindeen Date: Fri, 17 Jan 2025 07:09:29 +0530 Subject: [PATCH] Add userId in successful user-registration response. --- .../dto/SuccessfulUserCreationDTO.java | 23 +++++++++--- .../user/endpoint/impl/MeApiServiceImpl.java | 12 +++++++ .../src/main/resources/api.identity.user.yaml | 4 +++ .../signup/UserSelfRegistrationManager.java | 7 ++-- .../UserSelfRegistrationManagerTest.java | 36 ++++++++++++++++--- 5 files changed, 71 insertions(+), 11 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.user.governance/src/gen/java/org/wso2/carbon/identity/user/endpoint/dto/SuccessfulUserCreationDTO.java b/components/org.wso2.carbon.identity.api.user.governance/src/gen/java/org/wso2/carbon/identity/user/endpoint/dto/SuccessfulUserCreationDTO.java index c933444545..0d3c6e4bd4 100644 --- a/components/org.wso2.carbon.identity.api.user.governance/src/gen/java/org/wso2/carbon/identity/user/endpoint/dto/SuccessfulUserCreationDTO.java +++ b/components/org.wso2.carbon.identity.api.user.governance/src/gen/java/org/wso2/carbon/identity/user/endpoint/dto/SuccessfulUserCreationDTO.java @@ -15,12 +15,11 @@ */ package org.wso2.carbon.identity.user.endpoint.dto; -import io.swagger.annotations.*; -import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import javax.validation.Valid; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Pattern; @ApiModel(description = "") public class SuccessfulUserCreationDTO { @@ -34,6 +33,9 @@ public class SuccessfulUserCreationDTO { @Valid private String notificationChannel = null; + @Valid + private String userId = null; + /** * Status code of the operation. **/ @@ -70,6 +72,18 @@ public void setNotificationChannel(String notificationChannel) { this.notificationChannel = notificationChannel; } + /** + * User Id. + **/ + @ApiModelProperty(value = "User Id.") + @JsonProperty("userId") + public String getUserId() { + return userId; + } + public void setUserId(String userId) { + this.userId = userId; + } + @Override public String toString() { @@ -79,6 +93,7 @@ public String toString() { sb.append(" code: ").append(code).append("\n"); sb.append(" message: ").append(message).append("\n"); sb.append(" notificationChannel: ").append(notificationChannel).append("\n"); + sb.append(" userId: ").append(userId).append("\n"); sb.append("}\n"); return sb.toString(); diff --git a/components/org.wso2.carbon.identity.api.user.governance/src/main/java/org/wso2/carbon/identity/user/endpoint/impl/MeApiServiceImpl.java b/components/org.wso2.carbon.identity.api.user.governance/src/main/java/org/wso2/carbon/identity/user/endpoint/impl/MeApiServiceImpl.java index 6516a669eb..1c27ab79be 100644 --- a/components/org.wso2.carbon.identity.api.user.governance/src/main/java/org/wso2/carbon/identity/user/endpoint/impl/MeApiServiceImpl.java +++ b/components/org.wso2.carbon.identity.api.user.governance/src/main/java/org/wso2/carbon/identity/user/endpoint/impl/MeApiServiceImpl.java @@ -224,6 +224,12 @@ private SuccessfulUserCreationDTO buildSuccessResponseForInternalChannels( successDTO.setCode(notificationResponseBean.getCode()); successDTO.setMessage(notificationResponseBean.getMessage()); successDTO.setNotificationChannel(notificationResponseBean.getNotificationChannel()); + + // Add userId if not null + if (notificationResponseBean.getUser().getNullableUserId() != null) { + successDTO.setUserId(notificationResponseBean.getUser().getNullableUserId()); + } + return successDTO; } @@ -242,6 +248,12 @@ private SuccessfulUserCreationExternalResponseDTO buildSuccessResponseForExterna successDTO.setMessage(notificationResponseBean.getMessage()); successDTO.setNotificationChannel(notificationResponseBean.getNotificationChannel()); successDTO.setConfirmationCode(notificationResponseBean.getRecoveryId()); + + // Add userId if not null + if (notificationResponseBean.getUser().getNullableUserId() != null) { + successDTO.setUserId(notificationResponseBean.getUser().getNullableUserId()); + } + return successDTO; } diff --git a/components/org.wso2.carbon.identity.api.user.governance/src/main/resources/api.identity.user.yaml b/components/org.wso2.carbon.identity.api.user.governance/src/main/resources/api.identity.user.yaml index 90b1eba25b..efcbb0f983 100644 --- a/components/org.wso2.carbon.identity.api.user.governance/src/main/resources/api.identity.user.yaml +++ b/components/org.wso2.carbon.identity.api.user.governance/src/main/resources/api.identity.user.yaml @@ -499,6 +499,10 @@ definitions: type: string example: "EMAIL" description: Account confirmation notification sent channel. + userId: + type: string + example: "USER-ID" + description: User Id. #----------------------------------------------------- # The SelfRegistrationUser Object diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java index 56aa56fe31..380699bd97 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java @@ -237,9 +237,10 @@ public NotificationResponseBean registerUser(User user, String password, Claim[] .isNotEmpty(preferredChannel)) { claimsMap.put(IdentityRecoveryConstants.PREFERRED_CHANNEL_CLAIM, preferredChannel); } - userStoreManager - .addUser(IdentityUtil.addDomainToName(user.getUserName(), user.getUserStoreDomain()), password, - userRoles, claimsMap, null); + org.wso2.carbon.user.core.common.User registeredUser = ((AbstractUserStoreManager) userStoreManager) + .addUserWithID(IdentityUtil.addDomainToName(user.getUserName(), user.getUserStoreDomain()), + password, userRoles, claimsMap, null); + user.setUserId(registeredUser.getUserID()); } catch (UserStoreException e) { Throwable cause = e; while (cause != null) { diff --git a/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java b/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java index 639d0cbf98..ccaf4ad22a 100644 --- a/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java +++ b/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java @@ -238,6 +238,7 @@ public class UserSelfRegistrationManagerTest { private final String TEST_USERSTORE_DOMAIN = "PRIMARY"; private final String TEST_SECONDARY_USERSTORE_DOMAIN = "SECONDARY"; private final String TEST_USER_NAME = "dummyUser"; + private final String TEST_USER_ID = "dummyUserId"; private final String TEST_INVALID_USER_NAME = "IS"; private final String TEST_CLAIM_URI = "ttp://wso2.org/claims/emailaddress"; private final String TEST_CLAIM_VALUE = "dummyuser@wso2.com"; @@ -1149,7 +1150,11 @@ public void testRegisterUser() throws Exception { User user = getUser(); mockConfigurations("true", "true"); - when(userStoreManager.isExistingRole(eq(IdentityRecoveryConstants.SELF_SIGNUP_ROLE))).thenReturn(true); + when(userRealm.getUserStoreManager()).thenReturn(abstractUserStoreManager); + when(abstractUserStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(userStoreManager); + when(abstractUserStoreManager.isExistingRole(eq(IdentityRecoveryConstants.SELF_SIGNUP_ROLE))).thenReturn(true); + when(abstractUserStoreManager.addUserWithID(anyString(), anyString(), any(), any(), isNull())) + .thenReturn(getRegisteredUser()); when(privilegedCarbonContext.getOSGiService(any(), isNull())).thenReturn(notificationChannelManager); when(notificationChannelManager.resolveCommunicationChannel(anyString(), anyString(), anyString(), any())) .thenReturn(NotificationChannels.EMAIL_CHANNEL.getChannelType()); @@ -1161,7 +1166,8 @@ public void testRegisterUser() throws Exception { User registeredUser = notificationResponseBean.getUser(); assertEquals(user.getUserName(), registeredUser.getUserName()); - verify(userStoreManager).addUser(anyString(), anyString(), any(), any(), isNull()); + assertEquals(registeredUser.getNullableUserId(), TEST_USER_ID); + verify(abstractUserStoreManager).addUserWithID(anyString(), anyString(), any(), any(), isNull()); verify(consentManger).addConsent(any()); verify(identityEventService, atLeastOnce()).handleEvent(any()); } @@ -1172,6 +1178,10 @@ public void testRegisterUserNotificationExternallyAccountLockedOnCreation() thro User user = new User(); user.setUserName(TEST_USER_NAME); Property property = new Property(IdentityRecoveryConstants.Consent.CONSENT, consentData); + when(userRealm.getUserStoreManager()).thenReturn(abstractUserStoreManager); + when(abstractUserStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(userStoreManager); + when(abstractUserStoreManager.addUserWithID(anyString(), anyString(), any(), any(), isNull())) + .thenReturn(getRegisteredUser()); try (MockedStatic mockedUtils = mockStatic(Utils.class)) { mockedUtils.when(() -> Utils.getSignUpConfigs(eq(ACCOUNT_LOCK_ON_CREATION), anyString())) @@ -1192,7 +1202,9 @@ public void testRegisterUserNotificationExternallyAccountLockedOnCreation() thro assertEquals(IdentityRecoveryConstants.SuccessEvents .SUCCESS_STATUS_CODE_SUCCESSFUL_USER_CREATION_EXTERNAL_VERIFICATION.getCode(), notificationResponseBean.getCode()); - verify(userStoreManager).addUser(anyString(), anyString(), any(), any(), isNull()); + User registeredUser = notificationResponseBean.getUser(); + verify(abstractUserStoreManager).addUserWithID(anyString(), anyString(), any(), any(), isNull()); + assertEquals(registeredUser.getNullableUserId(), TEST_USER_ID); verify(consentManger).addConsent(any()); verify(identityEventService, atLeastOnce()).handleEvent(any()); } @@ -1210,6 +1222,10 @@ public void testRegisterUserVerifiedPreferredChannel() throws Exception { .thenReturn("false"); when(consentUtilityService.filterPIIsFromReceipt(any(), any())).thenReturn( new HashSet<>(Arrays.asList(emailVerifiedClaimURI))); + when(userRealm.getUserStoreManager()).thenReturn(abstractUserStoreManager); + when(abstractUserStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(userStoreManager); + when(abstractUserStoreManager.addUserWithID(anyString(), anyString(), any(), any(), isNull())) + .thenReturn(getRegisteredUser()); try (MockedStatic mockedUtils = mockStatic(Utils.class)) { mockedUtils.when(() -> Utils.getSignUpConfigs(eq(ACCOUNT_LOCK_ON_CREATION), anyString())) @@ -1235,7 +1251,9 @@ public void testRegisterUserVerifiedPreferredChannel() throws Exception { assertEquals(IdentityRecoveryConstants.SuccessEvents .SUCCESS_STATUS_CODE_SUCCESSFUL_USER_CREATION_WITH_VERIFIED_CHANNEL.getCode(), notificationResponseBean.getCode()); - verify(userStoreManager).addUser(anyString(), anyString(), any(), any(), isNull()); + User registeredUser = notificationResponseBean.getUser(); + verify(abstractUserStoreManager).addUserWithID(anyString(), anyString(), any(), any(), isNull()); + assertEquals(registeredUser.getNullableUserId(), TEST_USER_ID); verify(consentManger).addConsent(any()); verify(identityEventService, atLeastOnce()).handleEvent(any()); } @@ -1779,6 +1797,16 @@ private User getUser() { return user; } + private org.wso2.carbon.user.core.common.User getRegisteredUser() { + + org.wso2.carbon.user.core.common.User user = new org.wso2.carbon.user.core.common.User(); + user.setUsername(TEST_USER_NAME); + user.setUserStoreDomain(TEST_USERSTORE_DOMAIN); + user.setTenantDomain(TEST_TENANT_DOMAIN_NAME); + user.setUserID(TEST_USER_ID); + return user; + } + private void mockMultiAttributeEnabled(Boolean isEnabled) throws ClaimMetadataException { mockedIdentityUtil.when(() -> IdentityUtil.getProperty(