1
|
/**
|
2
|
* Copyright (C) 2021 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.service.security;
|
10
|
|
11
|
import java.time.Duration;
|
12
|
import java.util.Map;
|
13
|
|
14
|
import org.apache.commons.collections4.map.HashedMap;
|
15
|
import org.apache.commons.text.StringSubstitutor;
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.core.env.Environment;
|
19
|
import org.springframework.mail.MailException;
|
20
|
import org.springframework.mail.SimpleMailMessage;
|
21
|
import org.springframework.mail.javamail.JavaMailSender;
|
22
|
|
23
|
import com.google.common.util.concurrent.RateLimiter;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.config.CdmConfigurationKeys;
|
26
|
import eu.etaxonomy.cdm.api.config.SendEmailConfigurer;
|
27
|
import eu.etaxonomy.cdm.api.security.PasswordResetRequest;
|
28
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
29
|
import eu.etaxonomy.cdm.persistence.dao.permission.IUserDao;
|
30
|
|
31
|
/**
|
32
|
* @author a.kohlbecker
|
33
|
* @since Nov 18, 2021
|
34
|
*/
|
35
|
public abstract class AccountSelfManagementService implements IRateLimitedService {
|
36
|
|
37
|
protected static Logger logger = Logger.getLogger(PasswordResetRequest.class);
|
38
|
|
39
|
public static final int RATE_LIMTER_TIMEOUT_SECONDS = 2;
|
40
|
|
41
|
public static final double PERMITS_PER_SECOND = 0.3;
|
42
|
|
43
|
@Autowired
|
44
|
protected IUserDao userDao;
|
45
|
|
46
|
@Autowired
|
47
|
protected IUserService userService;
|
48
|
|
49
|
@Autowired
|
50
|
protected JavaMailSender emailSender;
|
51
|
|
52
|
@Autowired
|
53
|
protected Environment env;
|
54
|
|
55
|
private Duration rateLimiterTimeout = null;
|
56
|
|
57
|
protected RateLimiter emailResetToken_rateLimiter = RateLimiter.create(PERMITS_PER_SECOND);
|
58
|
|
59
|
protected RateLimiter resetPassword_rateLimiter = RateLimiter.create(PERMITS_PER_SECOND);
|
60
|
|
61
|
/**
|
62
|
* Uses the {@link StringSubstitutor} as simple template engine.
|
63
|
* Below named values are automatically resolved, more can be added via the
|
64
|
* <code>valuesMap</code> parameter.
|
65
|
*
|
66
|
* @param userEmail
|
67
|
* The TO-address
|
68
|
* @param userName
|
69
|
* Used to set the value for <code>${userName}</code>
|
70
|
* @param subjectTemplate
|
71
|
* A {@link StringSubstitutor} template for the email subject
|
72
|
* @param bodyTemplate
|
73
|
* A {@link StringSubstitutor} template for the email body
|
74
|
* @param additionalValuesMap
|
75
|
* Additional named values for to be replaced in the template strings.
|
76
|
*/
|
77
|
public void sendEmail(String userEmail, String userName, String subjectTemplate, String bodyTemplate, Map<String, String> additionalValuesMap) throws MailException {
|
78
|
|
79
|
String from = env.getProperty(SendEmailConfigurer.FROM_ADDRESS);
|
80
|
String dataSourceBeanId = env.getProperty(CdmConfigurationKeys.CDM_DATA_SOURCE_ID);
|
81
|
String supportEmailAddress = env.getProperty(CdmConfigurationKeys.MAIL_ADDRESS_SUPPORT);
|
82
|
if(additionalValuesMap == null) {
|
83
|
additionalValuesMap = new HashedMap<>();
|
84
|
}
|
85
|
if(supportEmailAddress != null) {
|
86
|
additionalValuesMap.put("supportEmailAddress", supportEmailAddress);
|
87
|
}
|
88
|
additionalValuesMap.put("userName", userName);
|
89
|
additionalValuesMap.put("dataBase", dataSourceBeanId);
|
90
|
StringSubstitutor substitutor = new StringSubstitutor(additionalValuesMap);
|
91
|
|
92
|
// TODO use MimeMessages for better email layout?
|
93
|
// TODO user Thymeleaf instead for HTML support?
|
94
|
SimpleMailMessage message = new SimpleMailMessage();
|
95
|
|
96
|
message.setFrom(from);
|
97
|
message.setTo(userEmail);
|
98
|
|
99
|
message.setSubject(substitutor.replace(subjectTemplate));
|
100
|
message.setText(substitutor.replace(bodyTemplate));
|
101
|
|
102
|
emailSender.send(message);
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public Duration getRateLimiterTimeout() {
|
107
|
if(rateLimiterTimeout == null) {
|
108
|
rateLimiterTimeout = Duration.ofSeconds(RATE_LIMTER_TIMEOUT_SECONDS);
|
109
|
}
|
110
|
return rateLimiterTimeout;
|
111
|
}
|
112
|
|
113
|
|
114
|
@Override
|
115
|
public void setRateLimiterTimeout(Duration timeout) {
|
116
|
this.rateLimiterTimeout = timeout;
|
117
|
}
|
118
|
|
119
|
|
120
|
@Override
|
121
|
public void setRate(double rate) {
|
122
|
resetPassword_rateLimiter.setRate(rate);
|
123
|
emailResetToken_rateLimiter.setRate(rate);
|
124
|
}
|
125
|
|
126
|
}
|