CCM NG: ChallengeManager: Implemented methods for sending the emails.
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3971 8810af33-2d31-482b-a856-94f89814c4dfpull/2/head
parent
b06a29d432
commit
d668952ccf
|
|
@ -61,6 +61,9 @@ public final class KernelConfig {
|
|||
|
||||
@Setting
|
||||
private boolean secureLoginEnabled = false;
|
||||
|
||||
@Setting
|
||||
private String systemEmailAddress = "libreccm@example.org";
|
||||
|
||||
@Setting
|
||||
private Set<String> supportedLanguages = new HashSet<>(
|
||||
|
|
@ -196,6 +199,14 @@ public final class KernelConfig {
|
|||
this.defaultLanguage = defaultLanguage;
|
||||
}
|
||||
|
||||
public String getSystemEmailAddress() {
|
||||
return systemEmailAddress;
|
||||
}
|
||||
|
||||
public void setSystemEmailAddress(final String systemEmailAddress) {
|
||||
this.systemEmailAddress = systemEmailAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 7;
|
||||
|
|
@ -208,6 +219,7 @@ public final class KernelConfig {
|
|||
hash = 61 * hash + (secureLoginEnabled ? 1 : 0);
|
||||
hash = 61 * hash + Objects.hashCode(supportedLanguages);
|
||||
hash = 61 * hash + Objects.hashCode(defaultLanguage);
|
||||
hash = 61 * hash + Objects.hashCode(systemEmailAddress);
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
|
@ -249,7 +261,12 @@ public final class KernelConfig {
|
|||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(defaultLanguage, other.getDefaultLanguage());
|
||||
if (!Objects.equals(defaultLanguage, other.getDefaultLanguage())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(systemEmailAddress,
|
||||
other.getSystemEmailAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -272,7 +289,8 @@ public final class KernelConfig {
|
|||
+ "rememberLoginEnabeled = %b, "
|
||||
+ "secureLoginEnabled = %b, "
|
||||
+ "supportedLanguages = { \"%s\" }, "
|
||||
+ "defaultLanguage = \"%s\""
|
||||
+ "defaultLanguage = \"%s\", "
|
||||
+ "systemEmailAddress = \"%s\""
|
||||
+ " }",
|
||||
super.toString(),
|
||||
debugEnabled,
|
||||
|
|
@ -284,7 +302,8 @@ public final class KernelConfig {
|
|||
secureLoginEnabled,
|
||||
//supportedLanguages == null ? "" : supportedLanguages.stream().collect(Collectors.joining(", ")),
|
||||
languages,
|
||||
defaultLanguage);
|
||||
defaultLanguage,
|
||||
systemEmailAddress);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
package org.libreccm.security;
|
||||
|
||||
import com.arsdigita.kernel.KernelConfig;
|
||||
import com.arsdigita.mail.Mail;
|
||||
|
||||
import org.apache.commons.lang.text.StrSubstitutor;
|
||||
import org.libreccm.configuration.ConfigurationManager;
|
||||
|
|
@ -34,6 +35,7 @@ import java.util.Objects;
|
|||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
|
|
@ -69,8 +71,13 @@ public class ChallengeManager {
|
|||
return createMail(user, OneTimeAuthTokenPurpose.EMAIL_VERIFICATION);
|
||||
}
|
||||
|
||||
public void sendEmailVerification(final User user) {
|
||||
throw new UnsupportedOperationException();
|
||||
public void sendEmailVerification(final User user)
|
||||
throws MessagingException {
|
||||
final String text = createEmailVerification(user);
|
||||
sendMessage(
|
||||
user,
|
||||
retrieveEmailSubject(OneTimeAuthTokenPurpose.EMAIL_VERIFICATION),
|
||||
text);
|
||||
}
|
||||
|
||||
public void finishEmailVerification(final User user,
|
||||
|
|
@ -100,8 +107,13 @@ public class ChallengeManager {
|
|||
return createMail(user, OneTimeAuthTokenPurpose.ACCOUNT_ACTIVATION);
|
||||
}
|
||||
|
||||
public void sendAccountActivation(final User user) {
|
||||
throw new UnsupportedOperationException();
|
||||
public void sendAccountActivation(final User user)
|
||||
throws MessagingException {
|
||||
final String text = createEmailVerification(user);
|
||||
sendMessage(
|
||||
user,
|
||||
retrieveEmailSubject(OneTimeAuthTokenPurpose.ACCOUNT_ACTIVATION),
|
||||
text);
|
||||
}
|
||||
|
||||
public void finishAccountActivation(final User user,
|
||||
|
|
@ -130,8 +142,13 @@ public class ChallengeManager {
|
|||
return createMail(user, OneTimeAuthTokenPurpose.RECOVER_PASSWORD);
|
||||
}
|
||||
|
||||
public void sendPasswordRecover(final User user) {
|
||||
|
||||
public void sendPasswordRecover(final User user)
|
||||
throws MessagingException {
|
||||
final String text = createEmailVerification(user);
|
||||
sendMessage(
|
||||
user,
|
||||
retrieveEmailSubject(OneTimeAuthTokenPurpose.RECOVER_PASSWORD),
|
||||
text);
|
||||
}
|
||||
|
||||
public void finishPasswordRecover(final User user,
|
||||
|
|
@ -189,6 +206,40 @@ public class ChallengeManager {
|
|||
return substitutor.replace(template);
|
||||
}
|
||||
|
||||
private String retrieveEmailSubject(final OneTimeAuthTokenPurpose purpose) {
|
||||
final Locale locale = globalizationHelper.getNegotiatedLocale();
|
||||
|
||||
final EmailTemplates emailTemplates = configurationManager
|
||||
.findConfiguration(EmailTemplates.class);
|
||||
final LocalizedStringSetting setting;
|
||||
switch (purpose) {
|
||||
case ACCOUNT_ACTIVATION:
|
||||
setting = emailTemplates.getAccountActivationSubject();
|
||||
break;
|
||||
case EMAIL_VERIFICATION:
|
||||
setting = emailTemplates.getEmailVerificationSubject();
|
||||
break;
|
||||
case RECOVER_PASSWORD:
|
||||
setting = emailTemplates.getPasswordRecoverSubject();
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Unsupported value \"%s\" for purpose.",
|
||||
purpose.toString()));
|
||||
}
|
||||
|
||||
final LocalizedString localizedString = setting.getValue();
|
||||
if (localizedString.hasValue(locale)) {
|
||||
return localizedString.getValue(locale);
|
||||
} else {
|
||||
final KernelConfig kernelConfig = configurationManager
|
||||
.findConfiguration(KernelConfig.class);
|
||||
final Locale defaultLocale = new Locale(kernelConfig
|
||||
.getDefaultLanguage());
|
||||
return localizedString.getValue(defaultLocale);
|
||||
}
|
||||
}
|
||||
|
||||
private String retrieveEmailTemplate(
|
||||
final OneTimeAuthTokenPurpose purpose) {
|
||||
|
||||
|
|
@ -256,4 +307,17 @@ public class ChallengeManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
private void sendMessage(final User user,
|
||||
final String subject,
|
||||
final String text) throws MessagingException {
|
||||
final KernelConfig kernelConfig = configurationManager
|
||||
.findConfiguration(KernelConfig.class);
|
||||
|
||||
final Mail mail = new Mail(user.getPrimaryEmailAddress().getAddress(),
|
||||
kernelConfig.getSystemEmailAddress(),
|
||||
"email verification");
|
||||
mail.setBody(text);
|
||||
mail.send();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,16 +34,32 @@ import java.util.Objects;
|
|||
@Configuration
|
||||
public final class EmailTemplates {
|
||||
|
||||
@Setting
|
||||
private LocalizedStringSetting emailVerificationSubject;
|
||||
|
||||
@Setting
|
||||
private LocalizedStringSetting emailVerificationMail;
|
||||
|
||||
@Setting
|
||||
private LocalizedStringSetting passwordRecoverSubject;
|
||||
|
||||
@Setting
|
||||
private LocalizedStringSetting passwordRecoverMail;
|
||||
|
||||
@Setting
|
||||
private LocalizedStringSetting accountActivationSubject;
|
||||
|
||||
@Setting
|
||||
private LocalizedStringSetting accountActivationMail;
|
||||
|
||||
public EmailTemplates() {
|
||||
emailVerificationSubject = new LocalizedStringSetting();
|
||||
emailVerificationSubject.setValue(new LocalizedString());
|
||||
emailVerificationSubject.getValue().addValue(
|
||||
Locale.ENGLISH, "Email verification");
|
||||
emailVerificationSubject.getValue().addValue(
|
||||
Locale.GERMAN, "Prüfung ihrer E-Mail-Adresse");
|
||||
|
||||
emailVerificationMail = new LocalizedStringSetting();
|
||||
emailVerificationMail.setValue(new LocalizedString());
|
||||
emailVerificationMail.getValue().addValue(
|
||||
|
|
@ -65,6 +81,13 @@ public final class EmailTemplates {
|
|||
+ "Bitte beachten Sie, dass Sie den Prozess bis zu folgendem "
|
||||
+ "Zeitpunkt abschließen müssen: ${expires_date}");
|
||||
|
||||
passwordRecoverSubject = new LocalizedStringSetting();
|
||||
passwordRecoverSubject.setValue(new LocalizedString());
|
||||
passwordRecoverSubject.getValue().addValue(
|
||||
Locale.ENGLISH, "Information regarding your password");
|
||||
passwordRecoverSubject.getValue().addValue(
|
||||
Locale.GERMAN, "Zurücksetzen ihres Passwortes");
|
||||
|
||||
passwordRecoverMail = new LocalizedStringSetting();
|
||||
passwordRecoverMail.setValue(new LocalizedString());
|
||||
passwordRecoverMail.getValue().addValue(
|
||||
|
|
@ -86,6 +109,13 @@ public final class EmailTemplates {
|
|||
+ "Bitte beachten Sie, dass den den Prozess bis zu folgenden "
|
||||
+ "Zeitpunkt abschließen müsssen: ${expires_date}");
|
||||
|
||||
accountActivationSubject = new LocalizedStringSetting();
|
||||
accountActivationSubject.setValue(new LocalizedString());
|
||||
accountActivationSubject.getValue().addValue(
|
||||
Locale.ENGLISH, "Activate your account");
|
||||
accountActivationSubject.getValue().addValue(
|
||||
Locale.GERMAN, "Aktivieren Sie Ihr Benutzerkonto");
|
||||
|
||||
accountActivationMail = new LocalizedStringSetting();
|
||||
accountActivationMail.setValue(new LocalizedString());
|
||||
accountActivationMail.getValue().addValue(
|
||||
|
|
@ -108,15 +138,33 @@ public final class EmailTemplates {
|
|||
|
||||
}
|
||||
|
||||
public LocalizedStringSetting getEmailVerificationSubject() {
|
||||
return emailVerificationSubject;
|
||||
}
|
||||
|
||||
public void setEmailVerificationSubject(
|
||||
final LocalizedStringSetting emailVerificationSubject) {
|
||||
this.emailVerificationSubject = emailVerificationSubject;
|
||||
}
|
||||
|
||||
public LocalizedStringSetting getEmailVerificationMail() {
|
||||
return emailVerificationMail;
|
||||
}
|
||||
|
||||
public void setEmailVerificationMail(
|
||||
LocalizedStringSetting emailVerificationMail) {
|
||||
final LocalizedStringSetting emailVerificationMail) {
|
||||
this.emailVerificationMail = emailVerificationMail;
|
||||
}
|
||||
|
||||
public LocalizedStringSetting getPasswordRecoverSubject() {
|
||||
return passwordRecoverSubject;
|
||||
}
|
||||
|
||||
public void setPasswordRecoverSubject(
|
||||
LocalizedStringSetting passwordRecoverSubject) {
|
||||
this.passwordRecoverSubject = passwordRecoverSubject;
|
||||
}
|
||||
|
||||
public LocalizedStringSetting getPasswordRecoverMail() {
|
||||
return passwordRecoverMail;
|
||||
}
|
||||
|
|
@ -126,6 +174,15 @@ public final class EmailTemplates {
|
|||
this.passwordRecoverMail = passwordRecoverMail;
|
||||
}
|
||||
|
||||
public LocalizedStringSetting getAccountActivationSubject() {
|
||||
return accountActivationSubject;
|
||||
}
|
||||
|
||||
public void setAccountActivationSubject(
|
||||
final LocalizedStringSetting accountActivationSubject) {
|
||||
this.accountActivationSubject = accountActivationSubject;
|
||||
}
|
||||
|
||||
public LocalizedStringSetting getAccountActivationMail() {
|
||||
return accountActivationMail;
|
||||
}
|
||||
|
|
@ -138,8 +195,11 @@ public final class EmailTemplates {
|
|||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 5;
|
||||
hash = 53 * hash + Objects.hashCode(emailVerificationSubject);
|
||||
hash = 53 * hash + Objects.hashCode(emailVerificationMail);
|
||||
hash = 53 * hash + Objects.hashCode(passwordRecoverSubject);
|
||||
hash = 53 * hash + Objects.hashCode(passwordRecoverMail);
|
||||
hash = 53 * hash * Objects.hashCode(accountActivationSubject);
|
||||
hash = 53 * hash + Objects.hashCode(accountActivationMail);
|
||||
return hash;
|
||||
}
|
||||
|
|
@ -156,28 +216,46 @@ public final class EmailTemplates {
|
|||
return false;
|
||||
}
|
||||
final EmailTemplates other = (EmailTemplates) obj;
|
||||
if (!Objects.equals(emailVerificationSubject,
|
||||
other.getEmailVerificationSubject())) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(emailVerificationMail,
|
||||
other.getEmailVerificationMail())) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(passwordRecoverSubject,
|
||||
other.getPasswordRecoverSubject())) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(passwordRecoverMail,
|
||||
other.getPasswordRecoverMail())) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(accountActivationMail,
|
||||
other.getAccountActivationMail());
|
||||
if (!Objects.equals(accountActivationMail,
|
||||
other.getAccountActivationMail())) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(accountActivationSubject, other
|
||||
.getAccountActivationSubject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s{ "
|
||||
+ "emailVerificationSubject = { %s }, "
|
||||
+ "emailVerificationMail = { %s }, "
|
||||
+ "passwordRecoverSubject = { %s }"
|
||||
+ "passwordRecoverMail = { %s }, "
|
||||
+ "accountActivationSubject = { %s }"
|
||||
+ "accountActivationMail = { %s }"
|
||||
+ " }",
|
||||
super.toString(),
|
||||
Objects.toString(emailVerificationSubject),
|
||||
Objects.toString(emailVerificationMail),
|
||||
Objects.toString(passwordRecoverSubject),
|
||||
Objects.toString(passwordRecoverMail),
|
||||
Objects.toString(accountActivationSubject),
|
||||
Objects.toString(accountActivationMail));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue