diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreService.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreService.java index 6ad67ae0b..89d8e984e 100644 --- a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreService.java +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreService.java @@ -19,7 +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 java.util.List; @@ -35,7 +37,7 @@ public interface InvitationCoreService { * @return The created invitation. * @throws UserInvitationMgtException If an error occurs while creating the invitation. */ - List createInvitation(Invitation invitation) throws UserInvitationMgtException; + List createInvitations(InvitationDO invitation) throws UserInvitationMgtException; /** * Accepts the invitation with the given confirmation code. diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImpl.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImpl.java index c276636a9..c0e4a7abb 100644 --- a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImpl.java +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImpl.java @@ -42,7 +42,10 @@ 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; import org.wso2.carbon.identity.organization.user.invitation.management.models.RoleAssignments; import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService; import org.wso2.carbon.identity.role.v2.mgt.core.exception.IdentityRoleManagementException; @@ -78,6 +81,8 @@ import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.EVENT_PROP_TENANT_DOMAIN; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.EVENT_PROP_USER_NAME; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_ACCEPT_INVITATION; +import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_ACTIVE_INVITATION_EXISTS; +import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_CONSOLE_ACCESS_RESTRICTED; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_CONSTRUCT_REDIRECT_URL; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_CREATE_INVITATION; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_EVENT_HANDLE; @@ -89,18 +94,23 @@ import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_INVALID_INVITATION_ID; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_INVALID_ROLE; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_INVITATION_EXPIRED; +import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_INVITED_USER_EMAIL_NOT_FOUND; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_NO_INVITATION_FOR_USER; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_ROLE_EXISTENCE; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_UNABLE_TO_RESEND_INVITATION; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE_VALUE; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_USER_ALREADY_EXISTS; +import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_USER_ALREADY_EXISTS_INVITED_ORGANIZATION; +import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_USER_NOT_FOUND; +import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.FAIL_STATUS; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.FILTER_STATUS_EQ; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.INVITED_USER_GROUP_NAME_PREFIX; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ORG_USER_INVITATION_DEFAULT_REDIRECT_URL; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ORG_USER_INVITATION_USER_DOMAIN; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.STATUS_EXPIRED; import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.STATUS_PENDING; +import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.SUCCESS_STATUS; /** * Implementation of the invitation core service which manages the invitations of the organization users. @@ -111,75 +121,51 @@ public class InvitationCoreServiceImpl implements InvitationCoreService { private static final UserInvitationDAO userInvitationDAO = new UserInvitationDAOImpl(); @Override - public List createInvitation(Invitation invitation) throws UserInvitationMgtException { - - invitation.setInvitedOrganizationId(Utils.getOrganizationId()); - validateInvitationPayload(invitation); - validateRoleAssignments(invitation); + public List createInvitations(InvitationDO invitationDO) throws UserInvitationMgtException { + + String orgId = Utils.getOrganizationId(); + validateInvitationPayload(invitationDO, orgId); + validateRoleAssignments(invitationDO, orgId); + List createdInvitationsList = new ArrayList<>(); + Invitation invitation = new Invitation(); + invitation.setInvitedOrganizationId(orgId); OrganizationManager organizationManager = UserInvitationMgtDataHolder.getInstance() .getOrganizationManagerService(); - Invitation createdInvitation = null; - List createdInvitationsList = new ArrayList<>(); - List skippedUsersList = new ArrayList<>(); - String managedOrganization = null; - String emailClaim = null; - for (String username : invitation.getUsernamesList()) { + for (String username : invitationDO.getUsernamesList()) { + CreatedInvitation createdInvitation = new CreatedInvitation(); + createdInvitation.setUsername(username); + InvitationResult validationResult = userValidationResult(invitationDO, username, orgId); try { - invitation.setUsername(username); - // Checking the parent organization id - String parentOrgId = organizationManager.getParentOrganizationId(invitation.getInvitedOrganizationId()); - // Picking the parent organization's tenant domain - String parentTenantDomain = organizationManager.resolveTenantDomain(parentOrgId); - int parentTenantId = IdentityTenantUtil.getTenantId(parentTenantDomain); - String invitedTenantDomain = organizationManager - .resolveTenantDomain(invitation.getInvitedOrganizationId()); - String userDomainQualifiedUserName = UserCoreUtil - .addDomainToName(username, invitation.getUserDomain()); - AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(parentTenantId); - String invitedUserId = userStoreManager.getUserIDFromUserName(userDomainQualifiedUserName); - if (StringUtils.isNotEmpty(invitedUserId)) { - 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)); - } - if (userStoreManager.isExistingUser(userDomainQualifiedUserName)) { - emailClaim = userStoreManager.getUserClaimValue(userDomainQualifiedUserName, - CLAIM_EMAIL_ADDRESS, null); - } - if (isValidInviteRequest(invitation, username) && StringUtils.isNotEmpty(emailClaim)) { + 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) + .getUserClaimValue(userDomainQualifiedUserName, CLAIM_EMAIL_ADDRESS, null); + invitation.setUsername(username); invitation.setEmail(emailClaim); + invitation.setUserDomain(invitationDO.getUserDomain()); + invitation.setUserRedirectUrl(invitationDO.getUserRedirectUrl()); + invitation.setRoleAssignments(invitationDO.getRoleAssignments()); invitation.setUserOrganizationId(parentOrgId); invitation.setStatus(STATUS_PENDING); - invitation.setInvitationId(UUID.randomUUID().toString()); invitation.setConfirmationCode(UUID.randomUUID().toString()); userInvitationDAO.createInvitation(invitation); - createdInvitation = userInvitationDAO.getInvitationByInvitationId(invitation.getInvitationId()); - createdInvitation.setUsernamesList(invitation.getUsernamesList()); - createdInvitation.setSkippedUsersList(skippedUsersList); - processingRoleAssignments(createdInvitation.getRoleAssignments(), invitedTenantDomain); + Invitation createdInvitationInfo = userInvitationDAO + .getInvitationByInvitationId(invitation.getInvitationId()); // Trigger the event for invitation creation - triggerInvitationAddNotification(createdInvitation); - createdInvitationsList.add(createdInvitation); - } else { - skippedUsersList.add(username); + 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); } } - if (!skippedUsersList.isEmpty()) { - invitation.setSkippedUsersList(skippedUsersList); - createdInvitationsList.add(invitation); - } return createdInvitationsList; } @@ -363,7 +349,8 @@ public Invitation resendInvitation(String username, String domain) throws UserIn } } - private void validateInvitationPayload(Invitation invitation) throws UserInvitationMgtServerException { + private void validateInvitationPayload(InvitationDO invitation, String invitedOrgId) + throws UserInvitationMgtServerException { if (StringUtils.isEmpty(invitation.getUserDomain())) { invitation.setUserDomain(IdentityUtil.getProperty(ORG_USER_INVITATION_USER_DOMAIN)); @@ -373,7 +360,7 @@ private void validateInvitationPayload(Invitation invitation) throws UserInvitat try { String invitationAcceptanceURL = ServiceURLBuilder.create() .addPath(defaultInvitationAcceptanceURL) - .setOrganization(invitation.getInvitedOrganizationId()) + .setOrganization(invitedOrgId) .build() .getAbsolutePublicURL(); invitation.setUserRedirectUrl(invitationAcceptanceURL); @@ -549,7 +536,7 @@ private boolean isUserExistAtInvitedOrganization(String domainQualifiedUserName) return false; } - private boolean isConsoleAudienceAvailableInRole(Invitation invitation, String invitedTenantDomain) + private boolean isConsoleAudienceAvailableInRole(InvitationDO invitation, String invitedTenantDomain) throws IdentityRoleManagementException { if (ArrayUtils.isNotEmpty(invitation.getRoleAssignments())) { @@ -565,16 +552,16 @@ private boolean isConsoleAudienceAvailableInRole(Invitation invitation, String i return false; } - private void validateRoleAssignments(Invitation invitation) throws UserInvitationMgtException { + private void validateRoleAssignments(InvitationDO invitation, String orgId) throws UserInvitationMgtException { OrganizationManager organizationManager = UserInvitationMgtDataHolder.getInstance() .getOrganizationManagerService(); + RoleManagementService roleManagementService = getRoleManagementService(); try { - String invitedTenantDomain = organizationManager - .resolveTenantDomain(invitation.getInvitedOrganizationId()); + String invitedTenantDomain = organizationManager.resolveTenantDomain(orgId); if (ArrayUtils.isNotEmpty(invitation.getRoleAssignments())) { for (RoleAssignments roleAssignment : invitation.getRoleAssignments()) { - if (!getRoleManagementService().isExistingRole(roleAssignment.getRole(), invitedTenantDomain)) { + if (!roleManagementService.isExistingRole(roleAssignment.getRole(), invitedTenantDomain)) { throw new UserInvitationMgtClientException(ERROR_CODE_INVALID_ROLE.getCode(), ERROR_CODE_INVALID_ROLE.getMessage(), String.format(ERROR_CODE_INVALID_ROLE.getDescription(), roleAssignment.getRole())); @@ -597,59 +584,101 @@ private boolean isInvitedUserHasConsoleAccess(String userId, String tenantDomain FrameworkConstants.Application.CONSOLE_APP.equals(p.getAudienceName())); } - private boolean isValidInviteRequest(Invitation invitation, String username) throws UserInvitationMgtException { + private InvitationResult userValidationResult(InvitationDO invitation, String username, String invitedOrgId) + throws UserInvitationMgtException { - boolean isValid = true; + InvitationResult result = new InvitationResult(); try { OrganizationManager organizationManager = UserInvitationMgtDataHolder.getInstance() .getOrganizationManagerService(); // Checking the parent organization id - String parentOrgId = organizationManager.getParentOrganizationId(invitation.getInvitedOrganizationId()); + String parentOrgId = organizationManager.getParentOrganizationId(invitedOrgId); // Picking the parent organization's tenant domain String parentTenantDomain = organizationManager.resolveTenantDomain(parentOrgId); int parentTenantId = IdentityTenantUtil.getTenantId(parentTenantDomain); String invitedTenantDomain = organizationManager - .resolveTenantDomain(invitation.getInvitedOrganizationId()); + .resolveTenantDomain(invitedOrgId); boolean isActiveInvitationAvailable = isActiveInvitationAvailable(username, - invitation.getUserDomain(), parentOrgId, invitation.getInvitedOrganizationId()); + invitation.getUserDomain(), parentOrgId, invitedOrgId); String userDomainQualifiedUserName = UserCoreUtil .addDomainToName(username, invitation.getUserDomain()); AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(parentTenantId); String invitedUserId = userStoreManager.getUserIDFromUserName(userDomainQualifiedUserName); if (isUserExistAtInvitedOrganization(userDomainQualifiedUserName)) { - isValid = false; if (LOG.isDebugEnabled()) { LOG.debug("User " + username + " is already exists in the organization " - + invitation.getInvitedOrganizationId()); + + invitedOrgId); } + result.setStatus(FAIL_STATUS); + result.setErrorMsg(ERROR_CODE_USER_ALREADY_EXISTS_INVITED_ORGANIZATION); + return result; } if (isActiveInvitationAvailable) { - isValid = false; if (LOG.isDebugEnabled()) { LOG.debug("Active invitation is already available for the user: " + username - + " in the organization: " + invitation.getInvitedOrganizationId()); + + " in the organization: " + invitedOrgId); } + result.setStatus(FAIL_STATUS); + result.setErrorMsg(ERROR_CODE_ACTIVE_INVITATION_EXISTS); + return result; } if (!userStoreManager.isExistingUser(userDomainQualifiedUserName)) { - isValid = false; if (LOG.isDebugEnabled()) { LOG.debug("User: " + username + " is not exists in the organization: " + parentOrgId); } + result.setStatus(FAIL_STATUS); + result.setErrorMsg(ERROR_CODE_USER_NOT_FOUND); + return result; + } + String emailClaim = getUserStoreManager(parentOrgId, userDomainQualifiedUserName) + .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); + } + result.setStatus(FAIL_STATUS); + result.setErrorMsg(ERROR_CODE_INVITED_USER_EMAIL_NOT_FOUND); + return result; } if (isConsoleAudienceAvailableInRole(invitation, invitedTenantDomain) && !isInvitedUserHasConsoleAccess(invitedUserId, parentTenantDomain)) { - isValid = false; if (LOG.isDebugEnabled()) { - LOG.debug("The user: " + invitation.getUsername() + " is not having" + + LOG.debug("The user: " + username + " is not having" + " the console access."); } + result.setStatus(FAIL_STATUS); + result.setErrorMsg(ERROR_CODE_CONSOLE_ACCESS_RESTRICTED); + return result; } } catch (UserStoreException | OrganizationManagementException | IdentityRoleManagementException e) { throw new UserInvitationMgtServerException(ERROR_CODE_CREATE_INVITATION.getCode(), ERROR_CODE_CREATE_INVITATION.getMessage(), - String.format(ERROR_CODE_CREATE_INVITATION.getDescription(), invitation.getUsername()), e); + String.format(ERROR_CODE_CREATE_INVITATION.getDescription(), username), e); + } + 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 isValid; + return userStoreManager; } } diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/constant/UserInvitationMgtConstants.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/constant/UserInvitationMgtConstants.java index da2490723..36c7cc60c 100644 --- a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/constant/UserInvitationMgtConstants.java +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/constant/UserInvitationMgtConstants.java @@ -30,6 +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"; // Filter Constants public static final String STATUS_PENDING = "PENDING"; @@ -68,7 +70,7 @@ public enum ErrorMessage { // Service layer errors ERROR_CODE_USER_NOT_FOUND("10011", "Invalid user identification provided.", - "Could not find an user with given username %s."), + "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."), @@ -89,7 +91,7 @@ public enum ErrorMessage { "The filter attribute '%s' is not supported."), ERROR_CODE_ACTIVE_INVITATION_EXISTS("10018", "Invitation already exists.", - "An active invitation already exists for the user %s."), + "An active invitation already exists for the user."), ERROR_CODE_INVITATION_EXPIRED("10019", "Invitation expired.", "The invitation for the user %s has expired."), @@ -125,10 +127,13 @@ public enum ErrorMessage { "Could not find a role with given roleId %s."), ERROR_CODE_INVITED_USER_EMAIL_NOT_FOUND("10030", "Failed to resolve the email of the invited user.", - "Could not find the email of the invited user %s."), + "Could not find the email of the invited user."), ERROR_CODE_CONSOLE_ACCESS_RESTRICTED("10031", "Invited user doesn't have console access.", "Could not find any role with a console access to create an invitation."), + 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 while checking the role existence.", "Could not resolve the role existence from the given list."), diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/CreatedInvitation.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/CreatedInvitation.java new file mode 100644 index 000000000..ab9aef067 --- /dev/null +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/CreatedInvitation.java @@ -0,0 +1,30 @@ +package org.wso2.carbon.identity.organization.user.invitation.management.models; + +/** + * Model that contains the created invitation details. + */ +public class CreatedInvitation { + + private String username; + private InvitationResult result; + + public InvitationResult getResult() { + + return result; + } + + public void setResult(InvitationResult result) { + + this.result = result; + } + + public String getUsername() { + + return username; + } + + public void setUsername(String username) { + + this.username = username; + } +} diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/Invitation.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/Invitation.java index 9b90d7496..20621a2e1 100644 --- a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/Invitation.java +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/Invitation.java @@ -19,7 +19,6 @@ package org.wso2.carbon.identity.organization.user.invitation.management.models; import java.sql.Timestamp; -import java.util.List; /** * Model that contains the invitation related details. @@ -29,7 +28,6 @@ public class Invitation { private String invitationId; private String confirmationCode; private String username; - private List usernamesList; private String userDomain; private String email; private String userOrganizationId; @@ -41,7 +39,6 @@ public class Invitation { private Timestamp expiredAt; private String userRedirectUrl; private RoleAssignments[] roleAssignments; - private List skippedUsersList; public String getInvitationId() { @@ -73,16 +70,6 @@ public void setUsername(String username) { this.username = username; } - public List getUsernamesList() { - - return usernamesList; - } - - public void setUsernamesList(List usernames) { - - this.usernamesList = usernames; - } - public String getUserDomain() { return userDomain; @@ -195,14 +182,4 @@ public void setRoleAssignments(RoleAssignments[] roleAssignments) { this.roleAssignments = roleAssignments != null ? roleAssignments.clone() : null; } - - public List getSkippedUsersList() { - - return skippedUsersList; - } - - public void setSkippedUsersList(List usernames) { - - this.skippedUsersList = usernames; - } } diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/InvitationDO.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/InvitationDO.java new file mode 100644 index 000000000..dc2e6789e --- /dev/null +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/InvitationDO.java @@ -0,0 +1,57 @@ +package org.wso2.carbon.identity.organization.user.invitation.management.models; + +import java.util.List; + +/** + * Model that contains the invitation data object. + */ +public class InvitationDO { + + private List usernamesList; + private String userDomain; + private RoleAssignments[] roleAssignments; + private String userRedirectUrl; + + public String getUserRedirectUrl() { + + return userRedirectUrl; + } + + public void setUserRedirectUrl(String userRedirectUrl) { + + this.userRedirectUrl = userRedirectUrl; + } + + public List getUsernamesList() { + + return usernamesList; + } + + public void setUsernamesList(List usernamesList) { + + this.usernamesList = usernamesList; + } + + public String getUserDomain() { + + return userDomain; + } + + public void setUserDomain(String userDomain) { + + this.userDomain = userDomain; + } + + public RoleAssignments[] getRoleAssignments() { + + if (roleAssignments == null) { + return null; + } + return roleAssignments.clone(); + } + + public void setRoleAssignments(RoleAssignments[] roleAssignments) { + + this.roleAssignments = roleAssignments != null ? roleAssignments.clone() : null; + } +} diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/InvitationResult.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/InvitationResult.java new file mode 100644 index 000000000..d334fd899 --- /dev/null +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/main/java/org/wso2/carbon/identity/organization/user/invitation/management/models/InvitationResult.java @@ -0,0 +1,32 @@ +package org.wso2.carbon.identity.organization.user.invitation.management.models; + +import org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants; + +/** + * Model that contains the created invitation result details. + */ +public class InvitationResult { + + private UserInvitationMgtConstants.ErrorMessage errorMsg; + private String status; + + public UserInvitationMgtConstants.ErrorMessage getErrorMsg() { + + return errorMsg; + } + + public void setErrorMsg(UserInvitationMgtConstants.ErrorMessage errorMsg) { + + this.errorMsg = errorMsg; + } + + public String getStatus() { + + return status; + } + + public void setStatus(String status) { + + this.status = status; + } +} diff --git a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/test/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImplTest.java b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/test/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImplTest.java index d518b30a1..c9f65bc08 100644 --- a/components/org.wso2.carbon.identity.organization.user.invitation.management/src/test/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImplTest.java +++ b/components/org.wso2.carbon.identity.organization.user.invitation.management/src/test/java/org/wso2/carbon/identity/organization/user/invitation/management/InvitationCoreServiceImplTest.java @@ -36,7 +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.RoleAssignments; import org.wso2.carbon.identity.organization.user.invitation.management.util.TestUtils; import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService; @@ -48,7 +50,6 @@ import java.nio.file.Paths; import java.sql.Connection; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -60,6 +61,7 @@ import static org.powermock.api.mockito.PowerMockito.mock; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import static org.wso2.carbon.identity.organization.user.invitation.management.constants.InvitationTestConstants.INV_01_CONF_CODE; @@ -95,9 +97,6 @@ public class InvitationCoreServiceImplTest extends PowerMockTestCase { private final UserInvitationDAO userInvitationDAO = new UserInvitationDAOImpl(); private InvitationCoreServiceImpl invitationCoreService; private final String [] roleList = {"1224", "12345"}; - String[] username1 = {INV_01_UN}; - String[] username2 = {INV_02_UN}; - String[] username3 = {INV_03_UN}; @BeforeClass public void setUp() throws Exception { @@ -114,16 +113,16 @@ public void setUp() throws Exception { Connection connection2 = getConnection(); Connection connection3 = getConnection(); - Invitation invitation1 = buildInvitation(INV_01_INVITATION_ID, INV_01_CONF_CODE, Arrays.asList(username1), + Invitation invitation1 = buildInvitation(INV_01_INVITATION_ID, INV_01_CONF_CODE, INV_01_UN, "DEFAULT", INV_01_EMAIL, "https://localhost:8080/travel-manager-001/invitations/accept", INV_01_USER_ORG_ID, INV_01_INV_ORG_ID, null, "PENDING"); - Invitation invitation2 = buildInvitation(INV_02_INVITATION_ID, INV_02_CONF_CODE, Arrays.asList(username2), + Invitation invitation2 = buildInvitation(INV_02_INVITATION_ID, INV_02_CONF_CODE, INV_02_UN, "DEFAULT", INV_02_EMAIL, "https://localhost:8080/travel-manager-001/invitations/accept", INV_02_USER_ORG_ID, INV_02_INV_ORG_ID, null, "PENDING"); RoleAssignments roleAssignments2 = buildRoleAssignments(roleList); - Invitation invitation3 = buildInvitation(INV_03_INVITATION_ID, INV_03_CONF_CODE, Arrays.asList(username3), + Invitation invitation3 = buildInvitation(INV_03_INVITATION_ID, INV_03_CONF_CODE, INV_03_UN, "DEFAULT", INV_03_EMAIL, "https://localhost:8080/travel-manager-001/invitations/accept", INV_03_USER_ORG_ID, INV_03_INV_ORG_ID, new RoleAssignments[]{roleAssignments2}, "PENDING"); @@ -254,11 +253,11 @@ public void testDeleteInvitationWithNotOwnedInvitationId() throws Exception { @Test(priority = 9) public void testCreateInvitationWithNonExistingUserInParent() throws Exception { - Invitation invitation1 = buildInvitation(null, - null, Collections.singletonList("samson"), "DEFAULT", - null, "https://localhost:8080/travel-manager-001/invitations/accept", - null, null, - null, null); + InvitationDO invitation1 = new InvitationDO(); + invitation1.setUsernamesList(Collections.singletonList("samson")); + invitation1.setUserDomain("DEFAULT"); + invitation1.setRoleAssignments(null); + invitation1.setUserRedirectUrl("https://localhost:8080/travel-manager-001/invitations/accept"); OrganizationManager organizationManager = mock(OrganizationManager.class); RealmService realmService = mock(RealmService.class); @@ -280,7 +279,9 @@ public void testCreateInvitationWithNonExistingUserInParent() throws Exception { when(IdentityDatabaseUtil.getDBConnection(true)).thenReturn(getConnection()); when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(-1234); mockIdentityTenantUtil(); - invitationCoreService.createInvitation(invitation1); + List createdInvitation = invitationCoreService.createInvitations(invitation1); + assertNotNull(createdInvitation); + assertEquals(createdInvitation.get(0).getResult().getStatus(), "Fail"); } @@ -301,19 +302,13 @@ private void populateH2Base(Connection connection, Invitation invitation) throws when(IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection); when(IdentityUtil.getProperty(anyString())).thenReturn("1440"); - if (invitation.getUsernamesList() != null) { - for (String username : invitation.getUsernamesList()) { - invitation.setUsername(username); - } - if (invitation.getRoleAssignments() != null) { - for (RoleAssignments roleAssignments : invitation.getRoleAssignments()) { - for (String role : roleList) { - roleAssignments.setRole(role); - } + if (invitation.getRoleAssignments() != null) { + for (RoleAssignments roleAssignments : invitation.getRoleAssignments()) { + for (String role : roleList) { + roleAssignments.setRole(role); } } } - userInvitationDAO.createInvitation(invitation); } @@ -326,14 +321,14 @@ private void mockCarbonContextForTenant() { thenReturn("dc828181-e1a8-4f5e-8936-f154f4aefa75"); } - private Invitation buildInvitation(String invitationId, String confirmationCode, List username, + private Invitation buildInvitation(String invitationId, String confirmationCode, String username, String userDomain, String email, String userRedirectUrl, String userOrgId, String invitedOrgId, RoleAssignments[] roleAssignments, String status) { Invitation invitation = new Invitation(); invitation.setInvitationId(invitationId); invitation.setConfirmationCode(confirmationCode); - invitation.setUsernamesList(username); + invitation.setUsername(username); invitation.setUserDomain(userDomain); invitation.setEmail(email); invitation.setUserRedirectUrl(userRedirectUrl);