From c37aa35e579cb2e18559db3f2f2b06816e98e506 Mon Sep 17 00:00:00 2001 From: Everett Sochowski Date: Mon, 28 Apr 2014 15:55:11 +0200 Subject: [PATCH] Send notification emails when a developer registers for api keys --- src/main/scala/bootstrap/liftweb/Boot.scala | 6 +++ .../code/snippet/ConsumerRegistration.scala | 43 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/main/scala/bootstrap/liftweb/Boot.scala b/src/main/scala/bootstrap/liftweb/Boot.scala index a60db5048..1915894a9 100755 --- a/src/main/scala/bootstrap/liftweb/Boot.scala +++ b/src/main/scala/bootstrap/liftweb/Boot.scala @@ -55,6 +55,7 @@ import java.io.FileInputStream import java.io.File import code.model.dataAccess.BankAccountCreationListener import code.snippet.OAuthWorkedThanks +import javax.mail.internet.MimeMessage /** * A class that's instantiated early and run. It allows the application * to modify lift's environment @@ -244,5 +245,10 @@ class Boot extends Loggable{ val useMessageQueue = Props.getBool("messageQueue.createBankAccounts", false) if(useMessageQueue) BankAccountCreationListener.startListen + + Mailer.devModeSend.default.set( (m : MimeMessage) => { + logger.info("Would have sent email if not in dev mode: " + m.getContent) + }) + } } diff --git a/src/main/scala/code/snippet/ConsumerRegistration.scala b/src/main/scala/code/snippet/ConsumerRegistration.scala index 904d47402..0d622687b 100644 --- a/src/main/scala/code/snippet/ConsumerRegistration.scala +++ b/src/main/scala/code/snippet/ConsumerRegistration.scala @@ -39,8 +39,10 @@ import net.liftweb.http.SHtml import net.liftweb.http.RequestVar import net.liftweb.util.FieldError import net.liftweb.util.Helpers +import net.liftweb.util.Props +import net.liftweb.common.Loggable -class ConsumerRegistration { +class ConsumerRegistration extends Loggable { //TODO: for security reasons this snippet and the template must be re-factored //to use the lift build in form function(SHtml._) so we can hide to what @@ -101,6 +103,9 @@ class ConsumerRegistration { key(Helpers.randomString(40).toLowerCase). secret(Helpers.randomString(40).toLowerCase). save + + notifyRegistrationOccurred(consumer) + showResults(consumer) } @@ -151,5 +156,41 @@ class ConsumerRegistration { else registerWithoutWarnings } + + + def notifyRegistrationOccurred(registered : Consumer) = { + import net.liftweb.util.Mailer + import net.liftweb.util.Mailer._ + + val mailSent = for { + // e.g mail.api.consumer.registered.sender.address=no-reply@example.com + from <- Props.get("mail.api.consumer.registered.sender.address") ?~ "Could not send mail: Missing props param for 'from'" + // no spaces, comma separated e.g. mail.api.consumer.registered.notification.addresses=notify@example.com,notify2@example.com,notify3@example.com + toAddressesString <- Props.get("mail.api.consumer.registered.notification.addresses") ?~ "Could not send mail: Missing props param for 'to'" + } yield { + + val thisApiInstance = Props.get("hostname", "unknown host") + val registrationMessage = s"New user signed up for API keys on $thisApiInstance. \n" + + s"Email: ${registered.developerEmail.get} \n" + + s"App name: ${registered.name.get} \n" + + s"App type: ${registered.appType.get.toString} \n" + + s"App description: ${registered.description.get}" + + //technically doesn't work for all valid email addresses so this will mess up if someone tries to send emails to "foo,bar"@example.com + val to = toAddressesString.split(",").toList + val toParams = to.map(To(_)) + val params = PlainMailBodyType(registrationMessage) :: toParams + + //this is an async call + Mailer.sendMail( + From(from), + Subject(s"New API user registered on $thisApiInstance"), + params :_*) + } + + //if Mailer.sendMail wasn't called (note: this actually isn't checking if the mail failed to send as that is being done asynchronously) + if(!mailSent.isDefined) this.logger.warn(s"API consumer registration failed: $mailSent") + + } } \ No newline at end of file