it sends now the email with a temp password, missing to save it in DB

This commit is contained in:
Massimiliano Assante 2020-07-22 18:33:54 +02:00
parent cb5a3ae1a9
commit fd69234e0b
3 changed files with 61 additions and 10 deletions

View File

@ -41,6 +41,12 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>event-publisher-portal</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>

View File

@ -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);
}
}

View File

@ -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,6 +87,15 @@ 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);
@ -91,7 +103,8 @@ public class InvitesManager {
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,7 +135,8 @@ 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("=")
@ -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 );