Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
format the code

Formatting

Address review changes
  • Loading branch information
Kanapriya committed Dec 12, 2023
1 parent 497733e commit 821d1c6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package org.wso2.carbon.identity.organization.user.invitation.management;

import org.wso2.carbon.identity.organization.user.invitation.management.exception.UserInvitationMgtException;
import org.wso2.carbon.identity.organization.user.invitation.management.models.CreatedInvitation;
import org.wso2.carbon.identity.organization.user.invitation.management.models.Invitation;
import org.wso2.carbon.identity.organization.user.invitation.management.models.InvitationDO;
import org.wso2.carbon.identity.organization.user.invitation.management.models.InvitationResult;

import java.util.List;

Expand All @@ -37,7 +37,7 @@ public interface InvitationCoreService {
* @return The list of created invitations.
* @throws UserInvitationMgtException If an error occurs while creating the invitation.
*/
List<CreatedInvitation> createInvitations(InvitationDO invitation) throws UserInvitationMgtException;
List<InvitationResult> createInvitations(InvitationDO invitation) throws UserInvitationMgtException;

/**
* Accepts the invitation with the given confirmation code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.wso2.carbon.identity.organization.user.invitation.management.exception.UserInvitationMgtServerException;
import org.wso2.carbon.identity.organization.user.invitation.management.internal.UserInvitationMgtDataHolder;
import org.wso2.carbon.identity.organization.user.invitation.management.models.AudienceInfo;
import org.wso2.carbon.identity.organization.user.invitation.management.models.CreatedInvitation;
import org.wso2.carbon.identity.organization.user.invitation.management.models.Invitation;
import org.wso2.carbon.identity.organization.user.invitation.management.models.InvitationDO;
import org.wso2.carbon.identity.organization.user.invitation.management.models.InvitationResult;
Expand Down Expand Up @@ -121,27 +120,37 @@ public class InvitationCoreServiceImpl implements InvitationCoreService {
private static final UserInvitationDAO userInvitationDAO = new UserInvitationDAOImpl();

@Override
public List<CreatedInvitation> createInvitations(InvitationDO invitationDO) throws UserInvitationMgtException {
public List<InvitationResult> createInvitations(InvitationDO invitationDO) throws UserInvitationMgtException {

List<InvitationResult> createdInvitationsList = new ArrayList<>();
Invitation invitation = new Invitation();
String orgId = Utils.getOrganizationId();
validateInvitationPayload(invitationDO, orgId);
validateRoleAssignments(invitationDO, orgId);
List<CreatedInvitation> createdInvitationsList = new ArrayList<>();
Invitation invitation = new Invitation();
invitation.setInvitedOrganizationId(orgId);
OrganizationManager organizationManager = UserInvitationMgtDataHolder.getInstance()
.getOrganizationManagerService();
for (String username : invitationDO.getUsernamesList()) {
CreatedInvitation createdInvitation = new CreatedInvitation();
createdInvitation.setUsername(username);
InvitationResult validationResult = userValidationResult(invitationDO, username, orgId);
try {
try {
String parentOrgId = getOrganizationManager().getParentOrganizationId(orgId);
String parentTenantDomain = getOrganizationManager().resolveTenantDomain(parentOrgId);
int parentTenantId = IdentityTenantUtil.getTenantId(parentTenantDomain);
AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(parentTenantId);
for (String username : invitationDO.getUsernamesList()) {
String userDomainQualifiedUserName = UserCoreUtil
.addDomainToName(username, invitationDO.getUserDomain());
String invitedUserId = userStoreManager.getUserIDFromUserName(userDomainQualifiedUserName);
String managedOrganization = OrganizationSharedUserUtil
.getUserManagedOrganizationClaim(userStoreManager, invitedUserId);
/* If the invited user is a shared user, get the corresponding user store manager of the shared user
managed organization. */
if (StringUtils.isNotEmpty(managedOrganization)) {
String userResidentTenantDomain = getOrganizationManager()
.resolveTenantDomain(managedOrganization);
userStoreManager = getAbstractUserStoreManager(IdentityTenantUtil.
getTenantId(userResidentTenantDomain));
}
InvitationResult validationResult = userValidationResult(invitationDO, userStoreManager,
userDomainQualifiedUserName, invitedUserId, username, parentOrgId, orgId, parentTenantDomain);
if (SUCCESS_STATUS.equals(validationResult.getStatus())) {
// Checking the parent organization id
String parentOrgId = organizationManager.getParentOrganizationId(orgId);
String userDomainQualifiedUserName = UserCoreUtil
.addDomainToName(username, invitationDO.getUserDomain());
String emailClaim = getUserStoreManager(parentOrgId, userDomainQualifiedUserName)
String emailClaim = userStoreManager
.getUserClaimValue(userDomainQualifiedUserName, CLAIM_EMAIL_ADDRESS, null);
invitation.setUsername(username);
invitation.setEmail(emailClaim);
Expand All @@ -158,13 +167,12 @@ public List<CreatedInvitation> createInvitations(InvitationDO invitationDO) thro
// Trigger the event for invitation creation
triggerInvitationAddNotification(createdInvitationInfo);
}
createdInvitation.setResult(validationResult);
createdInvitationsList.add(createdInvitation);
} catch (UserStoreException | OrganizationManagementException e) {
throw new UserInvitationMgtServerException(ERROR_CODE_CREATE_INVITATION.getCode(),
ERROR_CODE_CREATE_INVITATION.getMessage(),
String.format(ERROR_CODE_CREATE_INVITATION.getDescription(), invitation.getUsername()), e);
createdInvitationsList.add(validationResult);
}
} catch (UserStoreException | OrganizationManagementException e) {
throw new UserInvitationMgtServerException(ERROR_CODE_CREATE_INVITATION.getCode(),
ERROR_CODE_CREATE_INVITATION.getMessage(),
String.format(ERROR_CODE_CREATE_INVITATION.getDescription(), invitationDO.getUsernamesList()), e);
}
return createdInvitationsList;
}
Expand Down Expand Up @@ -567,8 +575,7 @@ private void validateRoleAssignments(InvitationDO invitation, String orgId) thro
}
} catch (OrganizationManagementException | IdentityRoleManagementException e) {
throw new UserInvitationMgtServerException(ERROR_CODE_ROLE_EXISTENCE.getCode(),
ERROR_CODE_ROLE_EXISTENCE.getMessage(),
String.format(ERROR_CODE_ROLE_EXISTENCE.getDescription()));
ERROR_CODE_ROLE_EXISTENCE.getMessage(), ERROR_CODE_ROLE_EXISTENCE.getDescription());
}
}

Expand All @@ -580,18 +587,15 @@ private boolean isInvitedUserHasConsoleAccess(String userId, String tenantDomain
FrameworkConstants.Application.CONSOLE_APP.equals(p.getAudienceName()));
}

private InvitationResult userValidationResult(InvitationDO invitation, String username, String invitedOrgId)
private InvitationResult userValidationResult(InvitationDO invitation, AbstractUserStoreManager userStoreManager,
String userDomainQualifiedUserName, String invitedUserId,
String username, String parentOrgId, String invitedOrgId,
String parentTenantDomain)
throws UserInvitationMgtException {

InvitationResult result = new InvitationResult();
result.setUsername(username);
try {
String userDomainQualifiedUserName = UserCoreUtil.addDomainToName(username, invitation.getUserDomain());
String invitedTenantDomain = getOrganizationManager().resolveTenantDomain(invitedOrgId);
String parentOrgId = getOrganizationManager().getParentOrganizationId(invitedOrgId);
String parentTenantDomain = getOrganizationManager().resolveTenantDomain(parentOrgId);
int parentTenantId = IdentityTenantUtil.getTenantId(parentTenantDomain);
AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(parentTenantId);
String invitedUserId = userStoreManager.getUserIDFromUserName(userDomainQualifiedUserName);
if (isUserExistAtInvitedOrganization(userDomainQualifiedUserName)) {
if (LOG.isDebugEnabled()) {
LOG.debug("User " + invitedUserId + " is already exists in the organization "
Expand All @@ -605,7 +609,7 @@ private InvitationResult userValidationResult(InvitationDO invitation, String us
invitation.getUserDomain(), parentOrgId, invitedOrgId);
if (isActiveInvitationAvailable) {
if (LOG.isDebugEnabled()) {
LOG.debug("Active invitation is already available for the user: " + username
LOG.debug("Active invitation is already available for the user: " + invitedUserId
+ " in the organization: " + invitedOrgId);
}
result.setStatus(FAIL_STATUS);
Expand All @@ -621,21 +625,21 @@ private InvitationResult userValidationResult(InvitationDO invitation, String us
result.setErrorMsg(ERROR_CODE_USER_NOT_FOUND);
return result;
}
String emailClaim = getUserStoreManager(parentOrgId, userDomainQualifiedUserName)
String emailClaim = userStoreManager
.getUserClaimValue(userDomainQualifiedUserName, CLAIM_EMAIL_ADDRESS, null);
if (StringUtils.isEmpty(emailClaim)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't resolve the email claim of the invited user :" + username);
LOG.debug("Couldn't resolve the email claim of the invited user :" + invitedUserId);
}
result.setStatus(FAIL_STATUS);
result.setErrorMsg(ERROR_CODE_INVITED_USER_EMAIL_NOT_FOUND);
return result;
}
String invitedTenantDomain = getOrganizationManager().resolveTenantDomain(invitedOrgId);
if (isConsoleAudienceAvailableInRole(invitation, invitedTenantDomain) &&
!isInvitedUserHasConsoleAccess(invitedUserId, parentTenantDomain)) {
if (LOG.isDebugEnabled()) {
LOG.debug("The user: " + invitedUserId + " is not having" +
" the console access.");
LOG.debug("The user: " + invitedUserId + " is not having the console access.");
}
result.setStatus(FAIL_STATUS);
result.setErrorMsg(ERROR_CODE_CONSOLE_ACCESS_RESTRICTED);
Expand All @@ -649,26 +653,4 @@ private InvitationResult userValidationResult(InvitationDO invitation, String us
result.setStatus(SUCCESS_STATUS);
return result;
}

private AbstractUserStoreManager getUserStoreManager(String parentOrgId, String userDomainQualifiedUserName)
throws UserStoreException, OrganizationManagementException {

OrganizationManager organizationManager = UserInvitationMgtDataHolder.getInstance()
.getOrganizationManagerService();
// Picking the parent organization's tenant domain
String parentTenantDomain = organizationManager.resolveTenantDomain(parentOrgId);
int parentTenantId = IdentityTenantUtil.getTenantId(parentTenantDomain);
AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(parentTenantId);
String invitedUserId = userStoreManager.getUserIDFromUserName(userDomainQualifiedUserName);
String managedOrganization = OrganizationSharedUserUtil
.getUserManagedOrganizationClaim(userStoreManager, invitedUserId);
/* If the invited user is a shared user, get the corresponding user store manager of the shared user
managed organization. */
if (StringUtils.isNotEmpty(managedOrganization)) {
String userResidentTenantDomain = organizationManager.resolveTenantDomain(managedOrganization);
userStoreManager = getAbstractUserStoreManager(IdentityTenantUtil.
getTenantId(userResidentTenantDomain));
}
return userStoreManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class UserInvitationMgtConstants {
public static final String DEFAULT_USER_STORE_DOMAIN = "DEFAULT";
public static final String DEFAULT_PROFILE = "default";
public static final String INVITED_USER_GROUP_NAME_PREFIX = "invitedOrgUserGroup-";
public static final String SUCCESS_STATUS = "Success";
public static final String FAIL_STATUS = "Fail";
public static final String SUCCESS_STATUS = "Successful";
public static final String FAIL_STATUS = "Failed";

// Filter Constants
public static final String STATUS_PENDING = "PENDING";
Expand Down Expand Up @@ -73,7 +73,7 @@ public enum ErrorMessage {
"Could not find an user with given username."),
ERROR_CODE_CREATE_INVITATION("10012",
"Unable to create the invitation.",
"Could not create the invitation to the user %s."),
"Could not create the invitation to the given user's list %s."),
ERROR_CODE_INVALID_CONFIRMATION_CODE("10013",
"Invalid confirmation code.",
"Could not validate the confirmation code %s."),
Expand Down Expand Up @@ -134,7 +134,7 @@ public enum ErrorMessage {
ERROR_CODE_USER_ALREADY_EXISTS_INVITED_ORGANIZATION("10032",
"Couldn't create the invitation.",
"User is already exists in the invited organization"),
ERROR_CODE_ROLE_EXISTENCE("10032",
ERROR_CODE_ROLE_EXISTENCE("10033",
"Error while checking the role existence.",
"Could not resolve the role existence from the given list."),

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants;

/**
* Model that contains the created invitation result details.
* Model that contains the created invitation status details.
*/
public class InvitationResult {

private String username;
private UserInvitationMgtConstants.ErrorMessage errorMsg;
private String status;

Expand All @@ -47,4 +48,14 @@ public void setStatus(String status) {

this.status = status;
}

public String getUsername() {

return username;
}

public void setUsername(String username) {

this.username = username;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import org.wso2.carbon.identity.organization.user.invitation.management.exception.UserInvitationMgtClientException;
import org.wso2.carbon.identity.organization.user.invitation.management.exception.UserInvitationMgtException;
import org.wso2.carbon.identity.organization.user.invitation.management.internal.UserInvitationMgtDataHolder;
import org.wso2.carbon.identity.organization.user.invitation.management.models.CreatedInvitation;
import org.wso2.carbon.identity.organization.user.invitation.management.models.Invitation;
import org.wso2.carbon.identity.organization.user.invitation.management.models.InvitationDO;
import org.wso2.carbon.identity.organization.user.invitation.management.models.InvitationResult;
import org.wso2.carbon.identity.organization.user.invitation.management.models.RoleAssignments;
import org.wso2.carbon.identity.organization.user.invitation.management.util.TestUtils;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
Expand Down Expand Up @@ -279,9 +279,9 @@ public void testCreateInvitationWithNonExistingUserInParent() throws Exception {
when(IdentityDatabaseUtil.getDBConnection(true)).thenReturn(getConnection());
when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(-1234);
mockIdentityTenantUtil();
List<CreatedInvitation> createdInvitation = invitationCoreService.createInvitations(invitation1);
List<InvitationResult> createdInvitation = invitationCoreService.createInvitations(invitation1);
assertNotNull(createdInvitation);
assertEquals(createdInvitation.get(0).getResult().getStatus(), "Fail");
assertEquals(createdInvitation.get(0).getStatus(), "Failed");
}


Expand Down

0 comments on commit 821d1c6

Please sign in to comment.