Skip to content

Commit

Permalink
Improve organization user invitation flows
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanChathusanda93 committed Sep 3, 2023
1 parent 042e173 commit dcdde38
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_INVALID_CONFIRMATION_CODE;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_INVALID_FILTER;
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_USER;
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_NO_INVITATION_FOR_USER;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_UNABLE_TO_RESEND_INVITATION;
Expand Down Expand Up @@ -226,6 +227,9 @@ public boolean acceptInvitation(String confirmationCode) throws UserInvitationMg
userInvitationDAO.deleteInvitation(invitation.getInvitationId());
}
}
throw new UserInvitationMgtClientException(ERROR_CODE_INVALID_USER.getCode(),
ERROR_CODE_INVALID_USER.getMessage(), String.format(ERROR_CODE_INVALID_USER.getDescription(),
authenticatedUser));
}
throw new UserInvitationMgtException(ERROR_CODE_INVALID_CONFIRMATION_CODE.getCode(),
ERROR_CODE_INVALID_CONFIRMATION_CODE.getMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class UserInvitationMgtConstants {
public static final String EVENT_PROP_GROUP_NAME = "group-name";
public static final String EVENT_PROP_ROLE_ASSIGNMENTS = "role-assignments";
public static final String EVENT_POST_ADD_INVITED_ORG_USER = "POST_ADD_INVITED_ORG_USER";
public static final int SQL_FK_CONSTRAINT_VIOLATION_ERROR_CODE = 547;

/**
* Error messages for organization user invitation management related errors.
Expand Down Expand Up @@ -107,59 +108,65 @@ public enum ErrorMessage {
ERROR_CODE_GET_PARENT_ORG("10026",
"Unable to get the parent organization.",
"Could not get the parent organization for the organization: %s."),
ERROR_CODE_INVALID_USER("10027",
"Invalid user identification provided.",
"Authenticated user %s is not entitled for the invitation."),

// DAO layer errors
ERROR_CODE_STORE_INVITATION("10001",
ERROR_CODE_STORE_INVITATION("10501",
"Unable to store the invitation.",
"Could not store the invitation details for user %s."),
ERROR_CODE_STORE_ROLE_ASSIGNMENTS("10002",
ERROR_CODE_STORE_ROLE_ASSIGNMENTS("10502",
"Unable to store the role assignments of the invitation.",
"Could not store the role assignment details for user %s."),
ERROR_CODE_COMMIT_INVITATION("10003",
ERROR_CODE_COMMIT_INVITATION("10503",
"Unable to store the invitation.",
"Could not store the invitation details."),
ERROR_CODE_RETRIEVE_INVITATION_DETAILS("10004",
ERROR_CODE_RETRIEVE_INVITATION_DETAILS("10504",
"Unable to retrieve the invitation.",
"Could not retrieve the invitation details for invitation id %s."),
ERROR_CODE_RETRIEVE_ROLE_ASSIGNMENTS("10005",
ERROR_CODE_RETRIEVE_ROLE_ASSIGNMENTS("10505",
"Unable to retrieve the role assignments of the invitation.",
"Could not retrieve the role assignments of the invitation for invitation id %s."),
ERROR_CODE_RETRIEVE_INVITATION_BY_CONFIRMATION_ID("10006",
ERROR_CODE_RETRIEVE_INVITATION_BY_CONFIRMATION_ID("10506",
"Unable to retrieve the invitation from confirmation code.",
"Could not retrieve the invitation details for confirmation code %s."),
ERROR_CODE_RETRIEVE_INVITATION_BY_ORG_ID("10007",
ERROR_CODE_RETRIEVE_INVITATION_BY_ORG_ID("10507",
"Unable to retrieve invitations.",
"Could not retrieve the invitations details for organization id %s."),
ERROR_CODE_RETRIEVE_ROLE_ASSIGNMENTS_FOR_INVITATION_BY_ORG_ID("10008",
ERROR_CODE_RETRIEVE_ROLE_ASSIGNMENTS_FOR_INVITATION_BY_ORG_ID("10508",
"Unable to retrieve role assignments.",
"Could not retrieve the role assignments details for invitation belongs to organization id %s."),
ERROR_CODE_RETRIEVE_INVITATIONS_FOR_ORG_ID("10009",
ERROR_CODE_RETRIEVE_INVITATIONS_FOR_ORG_ID("10509",
"Unable to retrieve invitations for organization.",
"Could not retrieve the invitations for organization id %s."),
ERROR_CODE_DELETE_ROLE_ASSIGNMENTS_BY_INVITATION("10010",
ERROR_CODE_DELETE_ROLE_ASSIGNMENTS_BY_INVITATION("10510",
"Unable to delete role assignments.",
"Could not delete role assignments for invitation id %s."),
ERROR_CODE_DELETE_INVITATION_DETAILS("10011",
ERROR_CODE_DELETE_INVITATION_DETAILS("10511",
"Unable to delete invitation details.",
"Could not delete invitation details for invitation id %s."),
ERROR_CODE_DELETE_INVITATION_BY_ID("10012",
ERROR_CODE_DELETE_INVITATION_BY_ID("10512",
"Unable to delete invitation.",
"Could not delete invitation for invitation id %s."),
ERROR_CODE_GET_INVITATION_BY_USER("10013",
ERROR_CODE_GET_INVITATION_BY_USER("10513",
"Unable to retrieve invitation.",
"Could not retrieve invitation for username %s."),
ERROR_CODE_MULTIPLE_INVITATIONS_FOR_USER("10014",
ERROR_CODE_MULTIPLE_INVITATIONS_FOR_USER("10514",
"Multiple invitations found.",
"Multiple invitations found for username %s."),
ERROR_CODE_GET_INVITATION("10015",
ERROR_CODE_GET_INVITATION("10515",
"Unable to get the invitation.",
"Could not get the invitation for invitation id %s."),
ERROR_CODE_CREATE_ORG_ASSOC("10016",
ERROR_CODE_CREATE_ORG_ASSOC("10516",
"Unable to create organization association.",
"Could not create organization association for user %s."),
ERROR_CODE_GET_INVITATION_BY_CONF_CODE("10017",
ERROR_CODE_GET_INVITATION_BY_CONF_CODE("10517",
"Unable to get the invitation.",
"Could not get the invitation with role assignments for confirmation code %s."),
ERROR_CODE_STORE_ROLES_APP_ID_INVALID("10518",
"Unable to store the role assignments.",
"Provided application/s is/are not valid."),

// Event listener errors
ERROR_CODE_DELETE_INVITED_USER_ASSOCIATION("10400",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public interface UserInvitationDAO {
* Create an invitation in the DB layer with the provided details.
*
* @param invitation Contains the invitation details which needs to be stored.
* @throws UserInvitationMgtServerException If an error occurs while creating the invitation.
* @throws UserInvitationMgtException If an error occurs while creating the invitation.
*/
void createInvitation(Invitation invitation) throws UserInvitationMgtServerException;
void createInvitation(Invitation invitation) throws UserInvitationMgtException;

/**
* Get the invitation details by using the confirmation code of the invitation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_RETRIEVE_ROLE_ASSIGNMENTS;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_RETRIEVE_ROLE_ASSIGNMENTS_FOR_INVITATION_BY_ORG_ID;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_STORE_INVITATION;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_STORE_ROLES_APP_ID_INVALID;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.ErrorMessage.ERROR_CODE_STORE_ROLE_ASSIGNMENTS;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.FILTER_STATUS;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.OPERATION_EQ;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.SQL_FK_CONSTRAINT_VIOLATION_ERROR_CODE;
import static org.wso2.carbon.identity.organization.user.invitation.management.constant.UserInvitationMgtConstants.STATUS_PENDING;

/**
Expand All @@ -107,7 +109,7 @@ private static String getInvitationsByOrganizationQuery(String filterParam, Stri
}

@Override
public void createInvitation(Invitation invitation) throws UserInvitationMgtServerException {
public void createInvitation(Invitation invitation) throws UserInvitationMgtException {

try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
try (PreparedStatement invitationCreatePrepStat = connection.prepareStatement(STORE_INVITATION)) {
Expand Down Expand Up @@ -147,6 +149,11 @@ public void createInvitation(Invitation invitation) throws UserInvitationMgtServ
}
invitationRoleAssignmentPrepStat.executeBatch();
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
if (SQL_FK_CONSTRAINT_VIOLATION_ERROR_CODE == e.getErrorCode() &&
StringUtils.containsIgnoreCase(e.getMessage(), "FK_ORG_USER_ROLE_SP_APP")) {
throw handleClientException(ERROR_CODE_STORE_ROLES_APP_ID_INVALID, StringUtils.EMPTY, e);
}
throw handleServerException(ERROR_CODE_STORE_ROLE_ASSIGNMENTS, invitation.getUsername(), e);
}
}
Expand Down Expand Up @@ -332,6 +339,7 @@ public boolean deleteInvitation(String invitationId) throws UserInvitationMgtSer
invitationDeletePrepStat.setString(1, invitationId);
invitationDeletePrepStat.executeUpdate();
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw handleServerException(ERROR_CODE_DELETE_INVITATION_DETAILS, invitationId, e);
}
connection.commit();
Expand Down Expand Up @@ -384,7 +392,7 @@ public Invitation getActiveInvitationByUser(String username, String domain, Stri
public void createOrganizationAssociation(String realUserId, String residentOrgId, String sharedUserId,
String sharedOrgId) throws UserInvitationMgtException {

try (Connection connection = IdentityDatabaseUtil.getDBConnection(true);
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
PreparedStatement createOrgAssocPrepStat = connection.prepareStatement(CREATE_USER_ASSOCIATION_TO_ORG)) {
createOrgAssocPrepStat.setString(1, sharedUserId);
createOrgAssocPrepStat.setString(2, sharedOrgId);
Expand All @@ -400,13 +408,12 @@ public void createOrganizationAssociation(String realUserId, String residentOrgI
public boolean deleteOrgUserAssociationToSharedOrg(String userId, String organizationId)
throws UserInvitationMgtException {

try (Connection connection = IdentityDatabaseUtil.getDBConnection(true);
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
PreparedStatement userOrgDeletePrepStat =
connection.prepareStatement(DELETE_ORG_ASSOCIATION_FOR_SHARED_USER)) {
userOrgDeletePrepStat.setString(1, userId);
userOrgDeletePrepStat.setString(2, organizationId);
userOrgDeletePrepStat.executeUpdate();
connection.commit();
return true;
} catch (SQLException e) {
throw handleServerException(ERROR_CODE_GET_INVITATION_BY_USER, userId, e);
Expand All @@ -417,13 +424,12 @@ public boolean deleteOrgUserAssociationToSharedOrg(String userId, String organiz
public boolean deleteAllAssociationsOfOrgUserToSharedOrgs(String userId, String organizationId)
throws UserInvitationMgtException {

try (Connection connection = IdentityDatabaseUtil.getDBConnection(true);
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
PreparedStatement userOrgDeletePrepStat =
connection.prepareStatement(DELETE_ALL_ORG_ASSOCIATIONS_FOR_SHARED_USER)) {
userOrgDeletePrepStat.setString(1, userId);
userOrgDeletePrepStat.setString(2, organizationId);
userOrgDeletePrepStat.executeUpdate();
connection.commit();
return true;
} catch (SQLException e) {
throw handleServerException(ERROR_CODE_GET_INVITATION_BY_USER, userId, e);
Expand Down

0 comments on commit dcdde38

Please sign in to comment.