Skip to content

Commit

Permalink
feat: Add Email validation feature when registering - MEED-1958 (#558)
Browse files Browse the repository at this point in the history
this change will allow to add a new step in registration process for
email verification and validation. To enable it, the addons will be able
to add a simple attribute in session
`request.getSession().setAttribute(ExternalRegisterHandler.REQUIRE_EMAIL_VALIDATION,
"true");`
  • Loading branch information
boubaker committed Mar 16, 2023
1 parent a61210b commit 0c7959e
Show file tree
Hide file tree
Showing 57 changed files with 659 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ default String sendExternalRegisterEmail(String sender,
throw new UnsupportedOperationException();
}

public boolean sendExternalConfirmationAccountEmail(String sender, Locale locale, StringBuilder url);
public boolean sendAccountCreatedConfirmationEmail(String sender, Locale locale, StringBuilder url);

public boolean allowChangePassword(String username) throws Exception; // NOSONAR

Expand All @@ -85,4 +85,28 @@ default String sendExternalRegisterEmail(String sender,

public ChangePasswordConnector getActiveChangePasswordConnector();

/**
* Remove used Token
*
* @param tokenId
* @param type
*/
void deleteToken(String tokenId, String type);

/**
* Sends verification email to user to continue registration
*
* @param data
* @param username
* @param firstName
* @param lastName
* @param email
* @param password
* @param locale
* @param url
* @return true if sent, else false
*/
boolean sendAccountVerificationEmail(String data, String username, String firstName, String lastName, String email,
String password, Locale locale, StringBuilder url);

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String sendExternalRegisterEmail(String sender, String email, Locale loca
}

@Override
public boolean sendExternalConfirmationAccountEmail(String sender, Locale locale, StringBuilder url) {
public boolean sendAccountCreatedConfirmationEmail(String sender, Locale locale, StringBuilder url) {
return false;
}

Expand Down Expand Up @@ -82,4 +82,15 @@ public String getExternalRegistrationURL(String tokenId, String lang) {
public ChangePasswordConnector getActiveChangePasswordConnector() {
return this.mockChangePasswordConnector;
}

@Override
public void deleteToken(String tokenId, String type) {
// Delete Token
}

@Override
public boolean sendAccountVerificationEmail(String data, String username, String firstName, String lastName, String email,
String password, Locale locale, StringBuilder url) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.exoplatform.web.login.recovery;

import static org.exoplatform.web.security.security.CookieTokenService.EMAIL_VALIDATION_TOKEN;
import static org.exoplatform.web.security.security.CookieTokenService.EXTERNAL_REGISTRATION_TOKEN;
import static org.exoplatform.web.security.security.CookieTokenService.FORGOT_PASSWORD_TOKEN;
import static org.exoplatform.web.security.security.CookieTokenService.ONBOARD_TOKEN;
Expand Down Expand Up @@ -127,6 +128,11 @@ public Credentials verifyToken(String tokenId, String type) {
return token.getPayload();
}

@Override
public void deleteToken(String tokenId, String type) {
remindPasswordTokenService.deleteToken(tokenId, type);
}

@Override
public Credentials verifyToken(String tokenId) {
return verifyToken(tokenId, "");
Expand Down Expand Up @@ -317,7 +323,50 @@ private String buildExternalEmailBody(String sender, String space, String link,
}

@Override
public boolean sendExternalConfirmationAccountEmail(String username, Locale locale, StringBuilder url) {
public boolean sendAccountVerificationEmail(String data, String username, String firstName, String lastName, String email, String password, Locale locale, StringBuilder url) {
try {
ResourceBundle bundle = bundleService.getResourceBundle(bundleService.getSharedResourceBundleNames(), locale);

Credentials credentials = new Credentials(data, password);
String tokenId = remindPasswordTokenService.createToken(credentials, EMAIL_VALIDATION_TOKEN);

StringBuilder redirectUrl = new StringBuilder();
redirectUrl.append(url);
redirectUrl.append("/").append(ExternalRegisterHandler.NAME);
redirectUrl.append("?action=" + ExternalRegisterHandler.VALIDATE_EXTERNAL_EMAIL_ACTION);
redirectUrl.append("&token=" + tokenId);

String emailBody = buildExternalVerificationAccountEmailBody(firstName + " " + lastName,
username,
redirectUrl.toString(),
bundle);
String emailSubject = bundle.getString("external.verification.account.email.subject") + " "
+ brandingService.getCompanyName() + "!";

String senderName = MailUtils.getSenderName();
String from = MailUtils.getSenderEmail();
if (senderName != null && !senderName.trim().isEmpty()) {
from = senderName + " <" + from + ">";
}

Message message = new Message();
message.setFrom(from);
message.setTo(email);
message.setSubject(emailSubject);
message.setBody(emailBody);
message.setMimeType("text/html");

mailService.sendMessage(message);
} catch (Exception ex) {
log.error("Failure to send external confirmation account email", ex);
return false;
}

return true;
}

@Override
public boolean sendAccountCreatedConfirmationEmail(String username, Locale locale, StringBuilder url) {

try {
User user = orgService.getUserHandler().findUserByName(username);
Expand Down Expand Up @@ -379,6 +428,28 @@ private String buildExternalConfirmationAccountEmailBody(String dispalyName,
return content;
}

private String buildExternalVerificationAccountEmailBody(String dispalyName,
String username,
String link,
ResourceBundle bundle) {
String content;
InputStream input = this.getClass()
.getClassLoader()
.getResourceAsStream("conf/external_verification_account_email_template.html");
if (input == null) {
content = "";
} else {
content = resolveLanguage(input, bundle);
}

content = content.replaceAll("\\$\\{DISPLAY_NAME\\}", dispalyName);
content = content.replaceAll("\\$\\{COMPANY_NAME\\}", brandingService.getCompanyName());
content = content.replaceAll("\\$\\{USERNAME\\}", username);
content = content.replaceAll("\\$\\{LOGIN_LINK\\}", link);

return content;
}

@Override
public boolean sendRecoverPasswordEmail(User user, Locale defaultLocale, HttpServletRequest req) {
if (user == null) {
Expand Down Expand Up @@ -455,6 +526,7 @@ private String buildRecoverEmailBody(User user, ResourceBundle bundle, String li
}

content = content.replaceAll("\\$\\{FIRST_NAME\\}", user.getFirstName());
content = content.replaceAll("\\$\\{COMPANY_NAME\\}", brandingService.getCompanyName());
content = content.replaceAll("\\$\\{USERNAME\\}", user.getUserName());
content = content.replaceAll("\\$\\{RESET_PASSWORD_LINK\\}", link);

Expand Down Expand Up @@ -547,4 +619,5 @@ public String getPasswordRecoverURL(String tokenId, String lang) {
public ChangePasswordConnector getActiveChangePasswordConnector() {
return this.changePasswordConnectorMap.get(this.changePasswordConnectorName);
}

}

0 comments on commit 0c7959e

Please sign in to comment.