Skip to content

Commit

Permalink
[SHIBUI-1030]
Browse files Browse the repository at this point in the history
Added support for Mailhog. Created service method for sending new user
mail to admins.
  • Loading branch information
Bill Smith committed Jan 11, 2019
1 parent 094cb14 commit d01192d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 15 deletions.
12 changes: 12 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
id 'net.researchgate.release' version '2.6.0'
id 'io.franzbecker.gradle-lombok' version '1.13'
id 'com.palantir.docker' version '0.20.1'
id 'com.avast.gradle.docker-compose' version '0.8.0'
}

apply plugin: 'io.spring.dependency-management'
Expand Down Expand Up @@ -248,4 +249,15 @@ docker {
files tasks.bootJar.outputs
files 'src/main/docker-files/loader.properties'
buildArgs(['JAR_FILE': "shibui-${version}.jar"])
}

/*
* Docker Compose (gradle-docker-compose-plugin) settings.
* Used to start and stop docker containers before running automation tests.
*/
apply plugin: 'docker-compose'
dockerCompose {
useComposeFiles = ['./src/main/docker-files/docker-compose.yml']
captureContainersOutput = true
waitForTcpPorts = false
}
9 changes: 9 additions & 0 deletions backend/src/main/docker-files/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3"
services:

redis:
image: mailhog/mailhog:latest
ports:
- 1025:1025
- 8025:8025
container_name: mailhog
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class EmailConfiguration {
@Setter
private String htmlEmailTemplatePathPrefix = "/mail/html/";

//Configured via @ConfigurationProperties (using setter method) with 'shibui.system.email.address' property and
// default value set here if that property is not explicitly set in application.properties
@Setter
private String systemEmailAddress = "doNotReply@shibui.org";

@Autowired
private ApplicationContext applicationContext;

Expand Down Expand Up @@ -92,6 +97,6 @@ private ITemplateResolver htmlTemplateResolver() {

@Bean
public EmailService emailService() {
return new EmailServiceImpl(javaMailSender, emailMessageSource(), textEmailTemplateEngine(), htmlEmailTemplateEngine());
return new EmailServiceImpl(javaMailSender, emailMessageSource(), textEmailTemplateEngine(), htmlEmailTemplateEngine(), systemEmailAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
* @author Bill Smith (wsmith@unicon.net)
*/
public interface EmailService {
void sendMail(String emailTemplate, String fromAddress, String recipient, String subject, Locale locale) throws MessagingException;
void sendMail(String emailTemplate, String fromAddress, String[] recipients, String subject, Locale locale) throws MessagingException;
void sendNewUserMail(String newUsername) throws MessagingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class EmailServiceImpl implements EmailService {
private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);

private final String systemEmailAddress;
private JavaMailSender emailSender;
private ResourceBundleMessageSource emailMessageSource;
private TemplateEngine textEmailTemplateEngine;
Expand All @@ -26,28 +27,39 @@ public class EmailServiceImpl implements EmailService {
public EmailServiceImpl(JavaMailSender emailSender,
ResourceBundleMessageSource emailMessageSource,
TemplateEngine textEmailTemplateEngine,
TemplateEngine htmlEmailTemplateEngine) {
TemplateEngine htmlEmailTemplateEngine,
String systemEmailAddress) {
this.emailSender = emailSender;
this.emailMessageSource = emailMessageSource;
this.textEmailTemplateEngine = textEmailTemplateEngine;
this.htmlEmailTemplateEngine = htmlEmailTemplateEngine;
this.systemEmailAddress = systemEmailAddress;
}

public void sendMail(String emailTemplate, String fromAddress, String recipient, String subject, Locale locale) throws MessagingException {
public void sendMail(String emailTemplate, String fromAddress, String[] recipients, String subject, Locale locale) throws MessagingException {
Context context = new Context(locale);
// TODO: set things to be replaced in the email template here

MimeMessage mimeMessage = this.emailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true,"UTF-8");
message.setSubject(subject);
message.setFrom(fromAddress);
message.setTo(recipient);
message.setTo(recipients);

String textContent = textEmailTemplateEngine.process(emailTemplate, context);
String htmlContent = htmlEmailTemplateEngine.process(emailTemplate, context);
message.setText(textContent, htmlContent);

// TODO: Uncomment when we're ready to actually send emails
// emailSender.send(mimeMessage);
emailSender.send(mimeMessage);
}

public void sendNewUserMail(String newUsername) throws MessagingException {
String subject = String.format("User Access Request for %s", newUsername);
sendMail("new-user", systemEmailAddress, getSystemAdmins(), subject, Locale.getDefault());
}

private String[] getSystemAdmins() {
return new String[]{"admin1@shibui.org", "admin2@shibui.org"};
}
}
9 changes: 5 additions & 4 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ shibui.nameid-filter-ui-schema-location=classpath:nameid-filter.schema.json
# shibui.metadataProviders.target=file:/opt/shibboleth-idp/conf/shibui-metadata-providers.xml
# shibui.metadataProviders.taskRunRate=30000

# Email configuration
# Email configuration (local mailhog)
spring.mail.host=localhost
spring.mail.port=25
spring.mail.port=1025
spring.mail.username=username
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=false
shibui.system.email.address=doNotReply@shibui.org
4 changes: 4 additions & 0 deletions backend/src/main/resources/mail/html/new-user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<head>New User Email</head>
<body>The user identified in the subject has requested access to SHIBUI.</body>
</html>
4 changes: 0 additions & 4 deletions backend/src/main/resources/mail/html/test.html

This file was deleted.

1 change: 1 addition & 0 deletions backend/src/main/resources/mail/text/new-user.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The user identified in the subject has requested access to SHIBUI.
1 change: 0 additions & 1 deletion backend/src/main/resources/mail/text/test.txt

This file was deleted.

0 comments on commit d01192d

Please sign in to comment.