mail configuartion
This commit is contained in:
parent
ee8e84b316
commit
e0143ab27e
|
@ -27,6 +27,7 @@ import eu.dnetlib.organizations.repository.SystemConfigurationRepository;
|
|||
import eu.dnetlib.organizations.repository.UserRepository;
|
||||
import eu.dnetlib.organizations.repository.readonly.UserViewRepository;
|
||||
import eu.dnetlib.organizations.utils.DatabaseUtils;
|
||||
import eu.dnetlib.organizations.utils.MailDispatcher;
|
||||
|
||||
@RestController
|
||||
public class AdminController extends AbstractDnetController {
|
||||
|
@ -46,6 +47,9 @@ public class AdminController extends AbstractDnetController {
|
|||
@Autowired
|
||||
private DatabaseUtils dbUtils;
|
||||
|
||||
@Autowired
|
||||
private MailDispatcher mailDispatcher;
|
||||
|
||||
@PostMapping(value = "/registration_api/newUser")
|
||||
public Map<String, Integer> newUser(final @RequestBody UserRegistration user, final Authentication authentication) {
|
||||
|
||||
|
@ -59,6 +63,10 @@ public class AdminController extends AbstractDnetController {
|
|||
res.put("status", 2);
|
||||
} else {
|
||||
dbUtils.newUser(email, fullname, organization, user.getReferencePerson(), user.getRequestMessage(), user.getCountries());
|
||||
|
||||
final UserView savedUser = userViewRepository.findById(email).get();
|
||||
mailDispatcher.sendRequestRegistrationMail(savedUser);
|
||||
|
||||
res.put("status", 1);
|
||||
}
|
||||
return res;
|
||||
|
@ -89,9 +97,18 @@ public class AdminController extends AbstractDnetController {
|
|||
}
|
||||
|
||||
@PostMapping("/api/users")
|
||||
public Iterable<UserView> saveUser(@RequestBody final UserView userView, final Authentication authentication) {
|
||||
public Iterable<UserView> updateUser(@RequestBody final UserView userView, final Authentication authentication) {
|
||||
if (UserInfo.getEmail(authentication).equals(userView.getEmail())) { throw new RuntimeException("You can't edit your own user"); }
|
||||
dbUtils.saveUser(userView);
|
||||
|
||||
dbUtils.updateUser(userView);
|
||||
|
||||
if (userView.getRole().equals(UserRole.USER.toString()) ||
|
||||
userView.getRole().equals(UserRole.NATIONAL_ADMIN.toString()) ||
|
||||
userView.getRole().equals(UserRole.ADMIN.toString())) {
|
||||
|
||||
mailDispatcher.sendUpdatedUserMail(userView);
|
||||
}
|
||||
|
||||
return users(authentication);
|
||||
}
|
||||
|
||||
|
|
|
@ -31,14 +31,35 @@ public class SystemConfiguration implements Serializable {
|
|||
@Column(name = "readonly")
|
||||
private Boolean readonly;
|
||||
|
||||
public SystemConfiguration() {}
|
||||
@Column(name = "smtp_enabled")
|
||||
private Boolean smtpEnabled;
|
||||
|
||||
public SystemConfiguration(final String id, final String title, final String homepageMessage, final Boolean readonly) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.homepageMessage = homepageMessage;
|
||||
this.readonly = readonly;
|
||||
}
|
||||
@Column(name = "smtp_host")
|
||||
private String smtpHost;
|
||||
|
||||
@Column(name = "smtp_port")
|
||||
private Integer smtpPort;
|
||||
|
||||
@Column(name = "smtp_user")
|
||||
private String smtpUser;
|
||||
|
||||
@Column(name = "smtp_password")
|
||||
private String smtpPassword;
|
||||
|
||||
@Column(name = "smtp_from_mail")
|
||||
private String smtpFromMail;
|
||||
|
||||
@Column(name = "smtp_from_name")
|
||||
private String smtpFromName;
|
||||
|
||||
@Column(name = "smtp_to_mail_admin")
|
||||
private String smtpToMailAdmin;
|
||||
|
||||
@Column(name = "smtp_new_user_message")
|
||||
private String smtpNewUserMessage;
|
||||
|
||||
@Column(name = "smtp_update_user_message")
|
||||
private String smtpUpdateUserMessage;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
|
@ -72,4 +93,84 @@ public class SystemConfiguration implements Serializable {
|
|||
this.readonly = readonly;
|
||||
}
|
||||
|
||||
public Boolean getSmtpEnabled() {
|
||||
return smtpEnabled;
|
||||
}
|
||||
|
||||
public void setSmtpEnabled(final Boolean smtpEnabled) {
|
||||
this.smtpEnabled = smtpEnabled;
|
||||
}
|
||||
|
||||
public String getSmtpHost() {
|
||||
return smtpHost;
|
||||
}
|
||||
|
||||
public void setSmtpHost(final String smtpHost) {
|
||||
this.smtpHost = smtpHost;
|
||||
}
|
||||
|
||||
public Integer getSmtpPort() {
|
||||
return smtpPort;
|
||||
}
|
||||
|
||||
public void setSmtpPort(final Integer smtpPort) {
|
||||
this.smtpPort = smtpPort;
|
||||
}
|
||||
|
||||
public String getSmtpUser() {
|
||||
return smtpUser;
|
||||
}
|
||||
|
||||
public void setSmtpUser(final String smtpUser) {
|
||||
this.smtpUser = smtpUser;
|
||||
}
|
||||
|
||||
public String getSmtpPassword() {
|
||||
return smtpPassword;
|
||||
}
|
||||
|
||||
public void setSmtpPassword(final String smtpPassword) {
|
||||
this.smtpPassword = smtpPassword;
|
||||
}
|
||||
|
||||
public String getSmtpFromMail() {
|
||||
return smtpFromMail;
|
||||
}
|
||||
|
||||
public void setSmtpFromMail(final String smtpFromMail) {
|
||||
this.smtpFromMail = smtpFromMail;
|
||||
}
|
||||
|
||||
public String getSmtpFromName() {
|
||||
return smtpFromName;
|
||||
}
|
||||
|
||||
public void setSmtpFromName(final String smtpFromName) {
|
||||
this.smtpFromName = smtpFromName;
|
||||
}
|
||||
|
||||
public String getSmtpToMailAdmin() {
|
||||
return smtpToMailAdmin;
|
||||
}
|
||||
|
||||
public void setSmtpToMailAdmin(final String smtpToMailAdmin) {
|
||||
this.smtpToMailAdmin = smtpToMailAdmin;
|
||||
}
|
||||
|
||||
public String getSmtpNewUserMessage() {
|
||||
return smtpNewUserMessage;
|
||||
}
|
||||
|
||||
public void setSmtpNewUserMessage(final String smtpNewUserMessage) {
|
||||
this.smtpNewUserMessage = smtpNewUserMessage;
|
||||
}
|
||||
|
||||
public String getSmtpUpdateUserMessage() {
|
||||
return smtpUpdateUserMessage;
|
||||
}
|
||||
|
||||
public void setSmtpUpdateUserMessage(final String smtpUpdateUserMessage) {
|
||||
this.smtpUpdateUserMessage = smtpUpdateUserMessage;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -337,7 +337,7 @@ public class DatabaseUtils {
|
|||
}
|
||||
|
||||
@Transactional
|
||||
public void saveUser(@RequestBody final UserView userView) {
|
||||
public void updateUser(@RequestBody final UserView userView) {
|
||||
final User user = userRepository.findById(userView.getEmail()).orElseThrow(() -> new RuntimeException("User not found"));
|
||||
user.setFullname(userView.getFullname());
|
||||
user.setOrganization(userView.getOrganization());
|
||||
|
|
|
@ -0,0 +1,123 @@
|
|||
package eu.dnetlib.organizations.utils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import eu.dnetlib.common.exceptions.DnetException;
|
||||
import eu.dnetlib.common.utils.mail.EmailFactory;
|
||||
import eu.dnetlib.common.utils.mail.EmailMessage;
|
||||
import eu.dnetlib.organizations.controller.UserRole;
|
||||
import eu.dnetlib.organizations.model.SystemConfiguration;
|
||||
import eu.dnetlib.organizations.model.view.UserView;
|
||||
import eu.dnetlib.organizations.repository.SystemConfigurationRepository;
|
||||
|
||||
@Component
|
||||
public class MailDispatcher {
|
||||
|
||||
@Autowired
|
||||
private SystemConfigurationRepository systemConfigurationRepository;
|
||||
|
||||
private final EmailFactory emailFactory = new EmailFactory();
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
private String fromMail;
|
||||
private String fromName;
|
||||
private String toMailAdmin;
|
||||
|
||||
private String newUserMessage;
|
||||
private String updateUserMessage;
|
||||
|
||||
private static final Log log = LogFactory.getLog(MailDispatcher.class);
|
||||
|
||||
@PostConstruct
|
||||
public void configure() {
|
||||
|
||||
final SystemConfiguration conf = systemConfigurationRepository.findById(SystemConfiguration.DEFAULT_ID).get();
|
||||
|
||||
this.enabled = conf.getSmtpEnabled();
|
||||
this.fromMail = conf.getSmtpFromMail();
|
||||
this.fromName = conf.getSmtpFromName();
|
||||
this.toMailAdmin = conf.getSmtpToMailAdmin();
|
||||
this.newUserMessage = conf.getSmtpNewUserMessage();
|
||||
this.updateUserMessage = conf.getSmtpUpdateUserMessage();
|
||||
|
||||
if (StringUtils.isNotBlank(conf.getSmtpHost())) {
|
||||
emailFactory.setSmtpHost(conf.getSmtpHost());
|
||||
} else {
|
||||
emailFactory.setSmtpHost("localhost");
|
||||
}
|
||||
if (conf.getSmtpPort() != null) {
|
||||
emailFactory.setSmtpPort(conf.getSmtpPort());
|
||||
} else {
|
||||
emailFactory.setSmtpPort(587);
|
||||
}
|
||||
if (StringUtils.isNotBlank(conf.getSmtpUser())) {
|
||||
emailFactory.setSmtpUser(conf.getSmtpUser());
|
||||
emailFactory.setSmtpPassword(conf.getSmtpPassword());
|
||||
} else {
|
||||
emailFactory.setSmtpUser(null);
|
||||
emailFactory.setSmtpPassword(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendRequestRegistrationMail(final UserView user) {
|
||||
if (StringUtils.isNotBlank(newUserMessage)) {
|
||||
final String message = prepareMessage(this.newUserMessage, user);
|
||||
|
||||
for (final String to : StringUtils.split(toMailAdmin, ",")) {
|
||||
sendMail("OpenOrgs: new registration request", message, to.trim());
|
||||
}
|
||||
} else {
|
||||
log.warn("Template is empty (newUserMessage)");
|
||||
}
|
||||
}
|
||||
|
||||
public void sendUpdatedUserMail(final UserView user) {
|
||||
if (StringUtils.isNotBlank(updateUserMessage)) {
|
||||
final String message = prepareMessage(this.updateUserMessage, user);
|
||||
sendMail("OpenOrgs: user updated", message, user.getEmail());
|
||||
} else {
|
||||
log.warn("Template is empty (updateUserMessage)");
|
||||
}
|
||||
}
|
||||
|
||||
private String prepareMessage(final String template, final UserView user) {
|
||||
final String countries = user.getRole().equals(UserRole.ADMIN.toString()) ? "All" : StringUtils.join(user.getCountries(), ", ");
|
||||
|
||||
return template.replaceAll(":email:", user.getEmail())
|
||||
.replaceAll(":fullname:", user.getFullname())
|
||||
.replaceAll(":organization:", user.getOrganization())
|
||||
.replaceAll(":refperson:", user.getReferencePerson())
|
||||
.replaceAll(":reqmessage:", user.getRequestMessage())
|
||||
.replaceAll(":role:", user.getRole())
|
||||
.replaceAll(":countries:", countries);
|
||||
|
||||
}
|
||||
|
||||
private void sendMail(final String subject, final String content, final String to) {
|
||||
if (!enabled) {
|
||||
log.debug("Mail not sent: MailDispatcher is disabled");
|
||||
} else if (StringUtils.isAnyBlank(subject, content, to, fromMail, fromName)) {
|
||||
log.warn("Mail not sent: some fields are empty");
|
||||
log.warn(" - subject: " + subject);
|
||||
log.warn(" - to: " + to);
|
||||
log.warn(" - fromMail: " + fromMail);
|
||||
log.warn(" - fromName: " + fromName);
|
||||
log.warn(" - content: " + content);
|
||||
} else {
|
||||
try {
|
||||
final EmailMessage mail = emailFactory.prepareEmail(subject, content, fromMail, fromName, to);
|
||||
mail.sendMail();
|
||||
} catch (final DnetException e) {
|
||||
log.error("Error sending mail", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -31,7 +31,17 @@ CREATE TABLE sysconf (
|
|||
id text PRIMARY KEY DEFAULT 'default',
|
||||
title text NOT NULL,
|
||||
homepage_msg text NOT NULL DEFAULT '',
|
||||
readonly boolean NOT NULL DEFAULT false
|
||||
readonly boolean NOT NULL DEFAULT false,
|
||||
smtp_enabled boolean NOT NULL DEFAULT false,
|
||||
smtp_host text NOT NULL DEFAULT 'localhost',
|
||||
smtp_port int NOT NULL DEFAULT 587,
|
||||
smtp_user text NOT NULL DEFAULT '',
|
||||
smtp_password text NOT NULL DEFAULT '',
|
||||
smtp_from_mail text NOT NULL DEFAULT 'no-reply@openaire.eu',
|
||||
smtp_from_name text NOT NULL DEFAULT 'OpenOrgs Database',
|
||||
smtp_to_mail_admin text NOT NULL DEFAULT 'openorgs-admin@openaire.eu',
|
||||
smtp_new_user_message text NOT NULL DEFAULT '-- message template here --',
|
||||
smtp_update_user_message text NOT NULL DEFAULT '-- message template here --'
|
||||
);
|
||||
INSERT INTO sysconf(id, title) VALUES ('default', 'OpenOrgs Database');
|
||||
|
||||
|
|
|
@ -4,6 +4,10 @@
|
|||
<div class="row">
|
||||
<div class="col-sm-12 col-lg-10">
|
||||
<form>
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">General</h5>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Title</label>
|
||||
<div class="col-sm-10">
|
||||
|
@ -24,8 +28,102 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mt-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Mail configuration</h5>
|
||||
|
||||
<div class="form-group row">
|
||||
<div class="offset-sm-2 col-sm-10">
|
||||
<label class="col-sm-2 col-form-label">Enabled</label>
|
||||
<div class="col-sm-10">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" ng-model="sysconf.smtpEnabled" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row" ng-show="sysconf.smtpEnabled">
|
||||
<label class="col-sm-2 col-form-label">SMTP host and port</label>
|
||||
<div class="col-sm-5">
|
||||
<input type="text" class="form-control" ng-model="sysconf.smtpHost" placeholder="host" />
|
||||
</div>
|
||||
<div class="col-sm-2">
|
||||
<input type="text" class="form-control" ng-model="sysconf.smtpPort" placeholder="port" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row" ng-show="sysconf.smtpEnabled">
|
||||
<label class="col-sm-2 col-form-label">
|
||||
SMTP authentication
|
||||
<small class="form-text text-muted">Optional</small>
|
||||
</label>
|
||||
<div class="col-sm-5">
|
||||
<input type="text" class="form-control" ng-model="sysconf.smtpUser" placeholder="username" />
|
||||
</div>
|
||||
<div class="col-sm-5">
|
||||
<input type="text" class="form-control" ng-model="sysconf.smtpPassword" placeholder="password" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row" ng-show="sysconf.smtpEnabled">
|
||||
<label class="col-sm-2 col-form-label">SMTP sender</label>
|
||||
<div class="col-sm-5">
|
||||
<input type="text" class="form-control" ng-model="sysconf.smtpFromMail" placeholder="sender email" />
|
||||
</div>
|
||||
<div class="col-sm-5">
|
||||
<input type="text" class="form-control" ng-model="sysconf.smtpFromName" placeholder="sender name" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row" ng-show="sysconf.smtpEnabled">
|
||||
<label class="col-sm-2 col-form-label">
|
||||
Recipients (administrators)
|
||||
<small class="form-text text-muted">Comma separated emails</small>
|
||||
</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" ng-model="sysconf.smtpToMailAdmin" placeholder="recipients" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">
|
||||
Template: registration request
|
||||
<small class="form-text text-muted"><b>Available variables:</b>
|
||||
:email:, :fullname:, :organization:, :refperson:, :reqmessage:, :role:, :countries:
|
||||
</small>
|
||||
</label>
|
||||
<div class="col-sm-10">
|
||||
<textarea class="form-control" ng-model="sysconf.smtpNewUserMessage" placeholder="html message here" rows="5"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">
|
||||
Template: user updated
|
||||
<small class="form-text text-muted"><b>Available variables:</b>
|
||||
:email:, :fullname:, :organization:, :refperson:, :reqmessage:, :role:, :countries:
|
||||
</small>
|
||||
</label>
|
||||
<div class="col-sm-10">
|
||||
<textarea class="form-control" ng-model="sysconf.smtpUpdateUserMessage" placeholder="html message here" rows="5"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<div class="form-group row mt-4">
|
||||
<div class="col-sm-10">
|
||||
<button type="submit" class="btn btn-primary" ng-click="saveConf()">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue