Skip to content

Commit

Permalink
Add userId in successful user-registration response.
Browse files Browse the repository at this point in the history
  • Loading branch information
AfraHussaindeen committed Jan 17, 2025
1 parent 4ea5d99 commit 7d7b250
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,6 +33,9 @@ public class SuccessfulUserCreationDTO {
@Valid
private String notificationChannel = null;

@Valid
private String userId = null;

/**
* Status code of the operation.
**/
Expand Down Expand Up @@ -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() {

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

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]";
Expand Down Expand Up @@ -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());
Expand All @@ -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());
}
Expand All @@ -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<Utils> mockedUtils = mockStatic(Utils.class)) {
mockedUtils.when(() -> Utils.getSignUpConfigs(eq(ACCOUNT_LOCK_ON_CREATION), anyString()))
Expand All @@ -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());
}
Expand All @@ -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<Utils> mockedUtils = mockStatic(Utils.class)) {
mockedUtils.when(() -> Utils.getSignUpConfigs(eq(ACCOUNT_LOCK_ON_CREATION), anyString()))
Expand All @@ -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());
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 7d7b250

Please sign in to comment.