diff --git a/pom.xml b/pom.xml index 28ce665..5b4cf5f 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,12 @@ + + org.gcube.portal + event-publisher-portal + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + provided + javax.servlet javax.servlet-api diff --git a/src/main/java/org/gcube/portal/invites/GenerateSecurePassword.java b/src/main/java/org/gcube/portal/invites/GenerateSecurePassword.java new file mode 100644 index 0000000..bff1ca4 --- /dev/null +++ b/src/main/java/org/gcube/portal/invites/GenerateSecurePassword.java @@ -0,0 +1,20 @@ +package org.gcube.portal.invites; + +import java.security.SecureRandom; +import java.util.Random; + +public class GenerateSecurePassword { + + private static final Random RANDOM = new SecureRandom(); + private static final String ALPHABET = "123ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + public static String generatePassword(int length) { + StringBuilder returnValue = new StringBuilder(length); + + for (int i = 0; i < length; i++) { + returnValue.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length()))); + } + + return new String(returnValue); + } +} diff --git a/src/main/java/org/gcube/portal/invites/InvitesManager.java b/src/main/java/org/gcube/portal/invites/InvitesManager.java index 4fde645..8a9b2af 100644 --- a/src/main/java/org/gcube/portal/invites/InvitesManager.java +++ b/src/main/java/org/gcube/portal/invites/InvitesManager.java @@ -24,8 +24,10 @@ import org.gcube.portal.mailing.message.RecipientType; import org.gcube.portal.mailing.service.EmailTemplateService; import org.gcube.portal.mailing.templates.TemplateUserHasInvited; import org.gcube.portal.mailing.templates.TemplatedJoinMeInvite; +import org.gcube.portal.mailing.templates.TemplatenviteWIthPassword; import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; +import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.GCubeGroup; @@ -44,6 +46,7 @@ public class InvitesManager { public static final String SITEID_ATTR ="siteId"; public static final String INVITEID_ATTR ="inviteId"; public static final String INVITE_PAGE_ENDPOINT = "manage-invite"; + public static final int RANDOM_PASSWD_LENGTH = 8; private static InvitesManager instance; @@ -84,14 +87,24 @@ public class InvitesManager { Invite invite = new Invite(UUID.randomUUID().toString(), username, currScope, email, controlcode, InviteStatus.PENDING, new Date(), fromFullName); InviteOperationResult result = null; + + boolean accountExists = true; + try { + new LiferayUserManager().getUserByEmail(invite.getInvitedEmail()); + } catch (UserManagementSystemException | UserRetrievalFault e) { + _log.info("No user account exist with this email " + invite.getInvitedEmail() + " sending email with password temp."); + accountExists = false; + } + boolean emailResult = false; try { String vreName = PortalContext.getConfiguration().getCurrentGroupName(request); result = store.saveInvite(invite); if (result == InviteOperationResult.ALREADY_INVITED) { invite.setKey(store.isExistingInvite(currScope, email)); - } - emailResult = sendInviteEmail(request, invite, currUser, vreName, name, email, vreDescription); + } + + emailResult = sendInviteEmail(request, invite, currUser, vreName, name, email, vreDescription, accountExists); notifyInviteSent(request, currUser, currScope, invite, vreName); } catch (AddressException e) { @@ -110,7 +123,7 @@ public class InvitesManager { String vreName, String name, String email, - String vreDescription) { + String vreDescription, boolean accountExists) { PortalContext pContext = PortalContext.getConfiguration(); String gatewayURL = pContext.getGatewayURL(request); @@ -122,8 +135,9 @@ public class InvitesManager { long groupId = PortalContext.getConfiguration().getCurrentGroupId(request); - ; - + + + StringBuilder getParamsEncoded = new StringBuilder(URLEncoder.encode(new String(Base64.encodeBase64(INVITEID_ATTR.getBytes())), "UTF-8")) .append("=") .append(URLEncoder.encode(new String(Base64.encodeBase64(invite.getKey().getBytes())), "UTF-8")) @@ -138,11 +152,22 @@ public class InvitesManager { .append("?") .append(getParamsEncoded); - EmailTemplateService.send( - subject, - new TemplatedJoinMeInvite(gatewayName, gatewayURL, currUser, name, vreName, vreDescription, linkToAcceptInvite.toString()), - request, - new Recipient(email), new Recipient(new EmailAddress(currUser.getEmail()), RecipientType.CC)); + String randomPasswd = ""; + if (accountExists) { + EmailTemplateService.send( + subject, + new TemplatedJoinMeInvite(gatewayName, gatewayURL, currUser, name, vreName, vreDescription, linkToAcceptInvite.toString()), + request, + new Recipient(email), new Recipient(new EmailAddress(currUser.getEmail()), RecipientType.CC)); + } + else { //the user account does not exists yet, it needs to be created on KC and a random pwd to be sent to the user in the email + randomPasswd = GenerateSecurePassword.generatePassword(RANDOM_PASSWD_LENGTH); + EmailTemplateService.send( + subject, + new TemplatenviteWIthPassword(gatewayName, gatewayURL, currUser, name, vreName, vreDescription, linkToAcceptInvite.toString(), randomPasswd), + request, + new Recipient(email), new Recipient(new EmailAddress(currUser.getEmail()), RecipientType.CC)); + } _log.debug("Join Me Invite email message sent successfully to " + email );