Skip to content

Commit

Permalink
feat: improve password reset email
Browse files Browse the repository at this point in the history
Signed-off-by: Oleh Astappiev <[email protected]>
  • Loading branch information
astappiev committed Apr 2, 2024
1 parent d606ace commit 91ad009
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 63 deletions.
82 changes: 41 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
<version>2.16.0</version>
</dependency>

<!-- ################ ImageIO (Image processing, Thumbnails) ################ -->
Expand Down Expand Up @@ -427,7 +427,7 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.14.2</version>
<version>10.15.0</version>
</dependency>
</dependencies>
</plugin>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/l3s/learnweb/user/PasswordBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void submit() {
int tokenId = tokenDao.insert(user.getId(), Token.TokenType.PASSWORD_RESET, HashHelper.sha256(token), LocalDateTime.now().plusDays(1));
String link = url + tokenId + ":" + token;

Mail mail = MailFactory.buildPasswordChangeEmail(user.getUsername(), link).build(user.getLocale());
mail.setRecipient(this.identifier);
Mail mail = MailFactory.buildPasswordChangeEmail(user.getDisplayName(), user.getEmail(), link).build(user.getLocale());
mail.setRecipient(user.getEmail());
mail.send();
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/de/l3s/mail/MailFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ public static MessageBuilder buildConfirmEmail(String username, String confirmUr
.defaultFooter();
}

public static MessageBuilder buildPasswordChangeEmail(String username, String changeUrl) {
public static MessageBuilder buildPasswordChangeEmail(String username, String email, String changeUrl) {
return new MessageBuilder("email_password.retrieve_title", Learnweb.config().getAppName())
.add(new Paragraph(new Text("greeting")).append(" ").append(username).append(","))
.add(new Paragraph(new Text("email_password.change", Learnweb.config().getAppName(), username)).append(new LineBreak()).append(new Link(changeUrl)))
.add(new Paragraph(new Text("email_password.reset_requested", Learnweb.config().getAppName(), email)).append(new LineBreak())
.append(new Text("email_password.change_link")).append(new LineBreak()).append(new Link(changeUrl)))
.add(new Paragraph(new Text("email_password.expired_warning")).append(new LineBreak()).append(new Text("email.you_can_ignore")))
.defaultFooter();
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/resources/de/l3s/learnweb/lang/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ email.confirm_address = please use this link to confirm your mail address:
email.confirm_address_subject = Confirmation request from {0}
email.regards = Best regards,
email.team = {0} Team
email.you_can_ignore = You can just ignore this email, if you haven't requested it.
email.you_can_ignore = If you did not request a new password, you can ignore this email, or let us know by replying to this email.

email_address = Email address
email_address_or_username = Email address or username
Expand All @@ -379,9 +379,10 @@ email_forum_notifications.resent_updates = recent updates on your groups forums:

email_has_been_sent = An email has been sent to you.

email_password.change = you can change the password of your {0} account {1} by clicking on this link:
email_password.reset_requested = We have received a request to reset the password for the {0} account associated with {1}.
email_password.change_link = You can reset your password by clicking the link below:
email_password.expired_warning = The link will expire in 24 hours.
email_password.retrieve_title = Retrieve {0} password
email_password.retrieve_title = Reset your {0} password

email_validation = Email validation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ email.confirm_address = Bitte benutzen Sie diesen Link, um Ihre E-Mail-Adresse z
email.confirm_address_subject = Bestätigungsanfrage von {0}
email.regards = Mit freundlichen Grüßen,
email.team = {0} Team
email.you_can_ignore = Sie können diese E-Mail einfach ignorieren, wenn Sie sie nicht angefordert haben.
email.you_can_ignore = Wenn Sie kein neues Passwort angefordert haben, können Sie diese E-Mail ignorieren oder Sie können uns informieren, indem Sie auf diese E-Mail antworten.

email_address = E-Mail-Adresse

Expand All @@ -263,7 +263,8 @@ email_forum_notifications.resent_updates = die letzten Aktualisierungen in den F

email_has_been_sent = Es wurde eine E-Mail an dich verschickt.

email_password.change = Sie können das Passwort Ihres {0}-Kontos {1} ändern, indem Sie auf diesen Link klicken:
email_password.reset_requested = wir haben eine Anfrage zum Zurücksetzen des Passworts für das {0} Konto erhalten, das mit {1} verbunden ist.
email_password.change_link = Sie können Ihr Passwort zurücksetzen, indem Sie auf diesen Link klicken:
email_password.expired_warning = Der Link wird in 24 Stunden ablaufen.
email_password.retrieve_title = Passwort für {0} abrufen

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ edit_profile = Modifica il profilo
edit_resource = Modifica Risorsa
email.regards = Cordiali saluti,
email.you_can_ignore = Se non avete richiesto una nuova password, potete ignorare questa e-mail o comunicarcelo rispondendo a questa e-mail.
email_address = Indirizzo E-mail
email_confirm_required_text = {0},<br/><br/>per completare la registrazione devi confermare l'indirizzo e-mail.<br/><br/>Abbiamo inviato un link di conferma a &quot;{1}&quot;.<br/>Controlla la tua casella di posta elettronica.<br/><br/>Se necessario puoi anche inviare di nuovo il messaggio o cambiare l'indirizzo mail specificato.
Expand All @@ -331,6 +333,8 @@ email_confirmed = L'indirizzo è stato confermato.

email_has_been_sent = Ti è stata inviata una e-mail.

email_password.reset_requested = Abbiamo ricevuto una richiesta di reimpostazione della password per l'account {0} associato a {1}.
email_password.change_link = È possibile reimpostare la password facendo clic sul link sottostante:
email_validation = Convalida indirizzo e-mail
enable_editing = Permettere la modifica
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ email.confirm_address = Будь ласка, використовуйте це
email.confirm_address_subject = Запит підтвердження від {0}
email.regards = З найкращими побажаннями,
email.team = Команда {0}
email.you_can_ignore = Ви можете просто проігнорувати цей лист, якщо ви його не запитували.
email.you_can_ignore = Якщо ви не отримували запит на новий пароль, ви можете проігнорувати цей лист або повідомити нам про це, відповівши на нього.
email_address = Електронна пошта
Expand All @@ -339,7 +339,8 @@ email_forum_notifications.resent_updates = Останні оновлення н
email_has_been_sent = Електронний лист було вам надіслано.
email_password.change = Ви можете змінити пароль свого облікового запису {0} {1}, натиснувши на це посилання:
email_password.reset_requested = Ви можете змінити пароль свого облікового запису {0} {1}, натиснувши на це посилання:
email_password.change_link = Ви можете відновити свій пароль, натиснувши на посилання нижче:
email_password.expired_warning = Термін дії посилання закінчується через 24 години.
email_password.retrieve_title = Отримати пароль {0}
Expand Down
20 changes: 11 additions & 9 deletions src/test/java/de/l3s/mail/message/MessageBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,26 @@ void testConfirmationEmail() {

@Test
void testConfirmPasswordEmail() {
MessageBuilder builder = MailFactory.buildPasswordChangeEmail("testuser1", "https://learnweb/change");
assertEquals("Retrieve Learnweb password", builder.getSubject(msg));
MessageBuilder builder = MailFactory.buildPasswordChangeEmail("testuser1", "[email protected]", "https://learnweb/change");
assertEquals("Reset your Learnweb password", builder.getSubject(msg));
assertEquals("""
Hello testuser1,
you can change the password of your Learnweb account testuser1 by clicking on this link:
We have received a request to reset the password for the Learnweb account associated with [email protected].
You can reset your password by clicking the link below:
https://learnweb/change
The link will expire in 24 hours.
You can just ignore this email, if you haven't requested it.
If you did not request a new password, you can ignore this email, or let us know by replying to this email.
_____________________________________
Best regards,
Learnweb Team""", builder.buildPlainText(msg));
assertEquals("<html><head><meta charset=\"UTF-8\"></head><body><p>Hello testuser1,</p><p>you can change the password of your Learnweb"
+ " account testuser1 by clicking on this link:<br/><a href=\"https://learnweb/change\">https://learnweb/change</a></p><p>The link will"
+ " expire in 24 hours.<br/>You can just ignore this email, if you haven't requested it.</p><footer><hr/>Best regards,<br/>Learnweb Team"
+ "</footer></body></html>", builder.buildHtmlText(msg));
assertEquals("<html><head><meta charset=\"UTF-8\"></head><body><p>Hello testuser1,</p><p>We have received a request to reset the password"
+ " for the Learnweb account associated with [email protected].<br/>You can reset your password by clicking the link below:<br/>"
+ "<a href=\"https://learnweb/change\">https://learnweb/change</a></p><p>The link will expire in 24 hours.<br/>"
+ "If you did not request a new password, you can ignore this email, or let us know by replying to this email.</p>"
+ "<footer><hr/>Best regards,<br/>Learnweb Team</footer></body></html>", builder.buildHtmlText(msg));
}

@Test
Expand Down Expand Up @@ -180,7 +182,7 @@ void testRenderHtml() throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(htmlFile, StandardCharsets.UTF_8))) {
bw.write(MailFactory.buildConfirmEmail("testuser1", "https://learnweb/hash").buildHtmlText(msg));
bw.write("<br/><hr/><br/>");
bw.write(MailFactory.buildPasswordChangeEmail("testuser1", "https://learnweb/change").buildHtmlText(msg));
bw.write(MailFactory.buildPasswordChangeEmail("testuser1", "[email protected]", "https://learnweb/change").buildHtmlText(msg));
bw.write("<br/><hr/><br/>");
bw.write(MailFactory.buildSuspiciousAlertEmail(suspiciousRequests).buildHtmlText(msg));
bw.write("<br/><hr/><br/>");
Expand Down

0 comments on commit 91ad009

Please sign in to comment.