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
|
@Setting
|
||||||
private boolean secureLoginEnabled = false;
|
private boolean secureLoginEnabled = false;
|
||||||
|
|
||||||
|
@Setting
|
||||||
|
private String systemEmailAddress = "libreccm@example.org";
|
||||||
|
|
||||||
@Setting
|
@Setting
|
||||||
private Set<String> supportedLanguages = new HashSet<>(
|
private Set<String> supportedLanguages = new HashSet<>(
|
||||||
|
|
@ -196,6 +199,14 @@ public final class KernelConfig {
|
||||||
this.defaultLanguage = defaultLanguage;
|
this.defaultLanguage = defaultLanguage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSystemEmailAddress() {
|
||||||
|
return systemEmailAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSystemEmailAddress(final String systemEmailAddress) {
|
||||||
|
this.systemEmailAddress = systemEmailAddress;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 7;
|
int hash = 7;
|
||||||
|
|
@ -208,6 +219,7 @@ public final class KernelConfig {
|
||||||
hash = 61 * hash + (secureLoginEnabled ? 1 : 0);
|
hash = 61 * hash + (secureLoginEnabled ? 1 : 0);
|
||||||
hash = 61 * hash + Objects.hashCode(supportedLanguages);
|
hash = 61 * hash + Objects.hashCode(supportedLanguages);
|
||||||
hash = 61 * hash + Objects.hashCode(defaultLanguage);
|
hash = 61 * hash + Objects.hashCode(defaultLanguage);
|
||||||
|
hash = 61 * hash + Objects.hashCode(systemEmailAddress);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -249,7 +261,12 @@ public final class KernelConfig {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Objects.equals(defaultLanguage, other.getDefaultLanguage());
|
if (!Objects.equals(defaultLanguage, other.getDefaultLanguage())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Objects.equals(systemEmailAddress,
|
||||||
|
other.getSystemEmailAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -272,7 +289,8 @@ public final class KernelConfig {
|
||||||
+ "rememberLoginEnabeled = %b, "
|
+ "rememberLoginEnabeled = %b, "
|
||||||
+ "secureLoginEnabled = %b, "
|
+ "secureLoginEnabled = %b, "
|
||||||
+ "supportedLanguages = { \"%s\" }, "
|
+ "supportedLanguages = { \"%s\" }, "
|
||||||
+ "defaultLanguage = \"%s\""
|
+ "defaultLanguage = \"%s\", "
|
||||||
|
+ "systemEmailAddress = \"%s\""
|
||||||
+ " }",
|
+ " }",
|
||||||
super.toString(),
|
super.toString(),
|
||||||
debugEnabled,
|
debugEnabled,
|
||||||
|
|
@ -284,7 +302,8 @@ public final class KernelConfig {
|
||||||
secureLoginEnabled,
|
secureLoginEnabled,
|
||||||
//supportedLanguages == null ? "" : supportedLanguages.stream().collect(Collectors.joining(", ")),
|
//supportedLanguages == null ? "" : supportedLanguages.stream().collect(Collectors.joining(", ")),
|
||||||
languages,
|
languages,
|
||||||
defaultLanguage);
|
defaultLanguage,
|
||||||
|
systemEmailAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
package org.libreccm.security;
|
package org.libreccm.security;
|
||||||
|
|
||||||
import com.arsdigita.kernel.KernelConfig;
|
import com.arsdigita.kernel.KernelConfig;
|
||||||
|
import com.arsdigita.mail.Mail;
|
||||||
|
|
||||||
import org.apache.commons.lang.text.StrSubstitutor;
|
import org.apache.commons.lang.text.StrSubstitutor;
|
||||||
import org.libreccm.configuration.ConfigurationManager;
|
import org.libreccm.configuration.ConfigurationManager;
|
||||||
|
|
@ -34,6 +35,7 @@ import java.util.Objects;
|
||||||
|
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import javax.mail.MessagingException;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -69,8 +71,13 @@ public class ChallengeManager {
|
||||||
return createMail(user, OneTimeAuthTokenPurpose.EMAIL_VERIFICATION);
|
return createMail(user, OneTimeAuthTokenPurpose.EMAIL_VERIFICATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendEmailVerification(final User user) {
|
public void sendEmailVerification(final User user)
|
||||||
throw new UnsupportedOperationException();
|
throws MessagingException {
|
||||||
|
final String text = createEmailVerification(user);
|
||||||
|
sendMessage(
|
||||||
|
user,
|
||||||
|
retrieveEmailSubject(OneTimeAuthTokenPurpose.EMAIL_VERIFICATION),
|
||||||
|
text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finishEmailVerification(final User user,
|
public void finishEmailVerification(final User user,
|
||||||
|
|
@ -100,8 +107,13 @@ public class ChallengeManager {
|
||||||
return createMail(user, OneTimeAuthTokenPurpose.ACCOUNT_ACTIVATION);
|
return createMail(user, OneTimeAuthTokenPurpose.ACCOUNT_ACTIVATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendAccountActivation(final User user) {
|
public void sendAccountActivation(final User user)
|
||||||
throw new UnsupportedOperationException();
|
throws MessagingException {
|
||||||
|
final String text = createEmailVerification(user);
|
||||||
|
sendMessage(
|
||||||
|
user,
|
||||||
|
retrieveEmailSubject(OneTimeAuthTokenPurpose.ACCOUNT_ACTIVATION),
|
||||||
|
text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finishAccountActivation(final User user,
|
public void finishAccountActivation(final User user,
|
||||||
|
|
@ -130,8 +142,13 @@ public class ChallengeManager {
|
||||||
return createMail(user, OneTimeAuthTokenPurpose.RECOVER_PASSWORD);
|
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,
|
public void finishPasswordRecover(final User user,
|
||||||
|
|
@ -189,6 +206,40 @@ public class ChallengeManager {
|
||||||
return substitutor.replace(template);
|
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(
|
private String retrieveEmailTemplate(
|
||||||
final OneTimeAuthTokenPurpose purpose) {
|
final OneTimeAuthTokenPurpose purpose) {
|
||||||
|
|
||||||
|
|
@ -256,4 +307,17 @@ public class ChallengeManager {
|
||||||
return false;
|
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
|
@Configuration
|
||||||
public final class EmailTemplates {
|
public final class EmailTemplates {
|
||||||
|
|
||||||
|
@Setting
|
||||||
|
private LocalizedStringSetting emailVerificationSubject;
|
||||||
|
|
||||||
@Setting
|
@Setting
|
||||||
private LocalizedStringSetting emailVerificationMail;
|
private LocalizedStringSetting emailVerificationMail;
|
||||||
|
|
||||||
|
@Setting
|
||||||
|
private LocalizedStringSetting passwordRecoverSubject;
|
||||||
|
|
||||||
@Setting
|
@Setting
|
||||||
private LocalizedStringSetting passwordRecoverMail;
|
private LocalizedStringSetting passwordRecoverMail;
|
||||||
|
|
||||||
|
@Setting
|
||||||
|
private LocalizedStringSetting accountActivationSubject;
|
||||||
|
|
||||||
@Setting
|
@Setting
|
||||||
private LocalizedStringSetting accountActivationMail;
|
private LocalizedStringSetting accountActivationMail;
|
||||||
|
|
||||||
public EmailTemplates() {
|
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 = new LocalizedStringSetting();
|
||||||
emailVerificationMail.setValue(new LocalizedString());
|
emailVerificationMail.setValue(new LocalizedString());
|
||||||
emailVerificationMail.getValue().addValue(
|
emailVerificationMail.getValue().addValue(
|
||||||
|
|
@ -65,6 +81,13 @@ public final class EmailTemplates {
|
||||||
+ "Bitte beachten Sie, dass Sie den Prozess bis zu folgendem "
|
+ "Bitte beachten Sie, dass Sie den Prozess bis zu folgendem "
|
||||||
+ "Zeitpunkt abschließen müssen: ${expires_date}");
|
+ "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 = new LocalizedStringSetting();
|
||||||
passwordRecoverMail.setValue(new LocalizedString());
|
passwordRecoverMail.setValue(new LocalizedString());
|
||||||
passwordRecoverMail.getValue().addValue(
|
passwordRecoverMail.getValue().addValue(
|
||||||
|
|
@ -86,6 +109,13 @@ public final class EmailTemplates {
|
||||||
+ "Bitte beachten Sie, dass den den Prozess bis zu folgenden "
|
+ "Bitte beachten Sie, dass den den Prozess bis zu folgenden "
|
||||||
+ "Zeitpunkt abschließen müsssen: ${expires_date}");
|
+ "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 = new LocalizedStringSetting();
|
||||||
accountActivationMail.setValue(new LocalizedString());
|
accountActivationMail.setValue(new LocalizedString());
|
||||||
accountActivationMail.getValue().addValue(
|
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() {
|
public LocalizedStringSetting getEmailVerificationMail() {
|
||||||
return emailVerificationMail;
|
return emailVerificationMail;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEmailVerificationMail(
|
public void setEmailVerificationMail(
|
||||||
LocalizedStringSetting emailVerificationMail) {
|
final LocalizedStringSetting emailVerificationMail) {
|
||||||
this.emailVerificationMail = emailVerificationMail;
|
this.emailVerificationMail = emailVerificationMail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalizedStringSetting getPasswordRecoverSubject() {
|
||||||
|
return passwordRecoverSubject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPasswordRecoverSubject(
|
||||||
|
LocalizedStringSetting passwordRecoverSubject) {
|
||||||
|
this.passwordRecoverSubject = passwordRecoverSubject;
|
||||||
|
}
|
||||||
|
|
||||||
public LocalizedStringSetting getPasswordRecoverMail() {
|
public LocalizedStringSetting getPasswordRecoverMail() {
|
||||||
return passwordRecoverMail;
|
return passwordRecoverMail;
|
||||||
}
|
}
|
||||||
|
|
@ -126,6 +174,15 @@ public final class EmailTemplates {
|
||||||
this.passwordRecoverMail = passwordRecoverMail;
|
this.passwordRecoverMail = passwordRecoverMail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalizedStringSetting getAccountActivationSubject() {
|
||||||
|
return accountActivationSubject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccountActivationSubject(
|
||||||
|
final LocalizedStringSetting accountActivationSubject) {
|
||||||
|
this.accountActivationSubject = accountActivationSubject;
|
||||||
|
}
|
||||||
|
|
||||||
public LocalizedStringSetting getAccountActivationMail() {
|
public LocalizedStringSetting getAccountActivationMail() {
|
||||||
return accountActivationMail;
|
return accountActivationMail;
|
||||||
}
|
}
|
||||||
|
|
@ -138,8 +195,11 @@ public final class EmailTemplates {
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 5;
|
int hash = 5;
|
||||||
|
hash = 53 * hash + Objects.hashCode(emailVerificationSubject);
|
||||||
hash = 53 * hash + Objects.hashCode(emailVerificationMail);
|
hash = 53 * hash + Objects.hashCode(emailVerificationMail);
|
||||||
|
hash = 53 * hash + Objects.hashCode(passwordRecoverSubject);
|
||||||
hash = 53 * hash + Objects.hashCode(passwordRecoverMail);
|
hash = 53 * hash + Objects.hashCode(passwordRecoverMail);
|
||||||
|
hash = 53 * hash * Objects.hashCode(accountActivationSubject);
|
||||||
hash = 53 * hash + Objects.hashCode(accountActivationMail);
|
hash = 53 * hash + Objects.hashCode(accountActivationMail);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
@ -156,28 +216,46 @@ public final class EmailTemplates {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final EmailTemplates other = (EmailTemplates) obj;
|
final EmailTemplates other = (EmailTemplates) obj;
|
||||||
|
if (!Objects.equals(emailVerificationSubject,
|
||||||
|
other.getEmailVerificationSubject())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!Objects.equals(emailVerificationMail,
|
if (!Objects.equals(emailVerificationMail,
|
||||||
other.getEmailVerificationMail())) {
|
other.getEmailVerificationMail())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!Objects.equals(passwordRecoverSubject,
|
||||||
|
other.getPasswordRecoverSubject())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!Objects.equals(passwordRecoverMail,
|
if (!Objects.equals(passwordRecoverMail,
|
||||||
other.getPasswordRecoverMail())) {
|
other.getPasswordRecoverMail())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return Objects.equals(accountActivationMail,
|
if (!Objects.equals(accountActivationMail,
|
||||||
other.getAccountActivationMail());
|
other.getAccountActivationMail())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Objects.equals(accountActivationSubject, other
|
||||||
|
.getAccountActivationSubject());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("%s{ "
|
return String.format("%s{ "
|
||||||
|
+ "emailVerificationSubject = { %s }, "
|
||||||
+ "emailVerificationMail = { %s }, "
|
+ "emailVerificationMail = { %s }, "
|
||||||
|
+ "passwordRecoverSubject = { %s }"
|
||||||
+ "passwordRecoverMail = { %s }, "
|
+ "passwordRecoverMail = { %s }, "
|
||||||
|
+ "accountActivationSubject = { %s }"
|
||||||
+ "accountActivationMail = { %s }"
|
+ "accountActivationMail = { %s }"
|
||||||
+ " }",
|
+ " }",
|
||||||
super.toString(),
|
super.toString(),
|
||||||
|
Objects.toString(emailVerificationSubject),
|
||||||
Objects.toString(emailVerificationMail),
|
Objects.toString(emailVerificationMail),
|
||||||
|
Objects.toString(passwordRecoverSubject),
|
||||||
Objects.toString(passwordRecoverMail),
|
Objects.toString(passwordRecoverMail),
|
||||||
|
Objects.toString(accountActivationSubject),
|
||||||
Objects.toString(accountActivationMail));
|
Objects.toString(accountActivationMail));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue